save file as zip

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
at this time this is the code
Rich (BB code):
'Save As and Close
    NewWB.SaveAs fileName:=RptFolder & "\" & RptName, FileFormat:=51
    NewWB.Close
    
    ThisWorkbook.Activate
    
    ExportMDCReport = RptFolder & "\" & RptName

how can i save as a zip file
thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
i am doing something wrong it gives me an automation error
do i have to add scripting or something else?
Rich (BB code):
Function ExportExpenseReport(sTabs As String, sParticipant As String, rptMo As Date, StartDate As Date, RanNextMonth As Boolean, ByVal Periods As Variant) As String
    Dim RptFolder       As String:          RptFolder = ExportFolder & "Monthly Expenses " & Format(rptMo, "m-yyyy") & "-" & gRun
    Dim RptName         As String:          RptName = "Expense Report " & sParticipant & "_" & sTabs & "_" & Format(rptMo, "yyyy-m") & "-" & gRun
    Dim ZipFileName     As String:          ZipFileName = RptFolder & "\" & RptName & ".zip"
    Dim ShellApp        As Object
    Dim ws              As Worksheet
    Dim NewWB           As Workbook
    Dim N               As Name
    Dim t               As Long
    Dim tCount          As Long
    Dim wsArray()       As String
    
    On Error GoTo ErrorExit
    
    'Calculate Period Count
    If RanNextMonth = True Then
        tCount = UBound(Periods) - 1
    Else
        tCount = UBound(Periods)
    End If
    
    'Store Global
    SpecificExportPath = RptFolder
    
    'Create Folder (if not exists)
    Call MakeMyFolder(RptFolder)
    
    'Export Worksheets to New Book
    wsArray = GetWSArray(tCount, RanNextMonth)
    Worksheets(wsArray).Copy
    Set NewWB = ActiveWorkbook
    
    'Rename Worksheets
    For Each ws In ActiveWorkbook.Worksheets
        If InStr(ws.Name, "Month") > 0 Then
            ws.Name = Replace(ws.Name, "Expenses Template", "")
            ws.Name = Replace(ws.Name, "Next Month ", Format(ws.Range("H6"), "m-yyyy") & " ")
        Else
            ws.Name = Replace(ws.Name, " Template", "")
        End If
    Next ws
    
    'Rename T Templates
    For t = tCount To 1 Step -1
        Worksheets("T" & t).Name = Format(CDate(Periods(t)), "m-yyyy")
    Next t
    
    
    'Clear Named Ranges
    For Each N In NewWB.Names
        If Left(N.Name, 1) <> "_" Then
            N.Delete
        End If
    Next
    
    'Save As and Close
    NewWB.SaveAs fileName:=RptFolder & "\" & RptName, FileFormat:=51
    NewWB.Close
    
    'Create a Shell Application to zip the Excel file
    Set ShellApp = CreateObject("Shell.Application")
    ShellApp.Namespace(ZipFileName).CopyHere CreateObject("Scripting.FileSystemObject").GetFile(RptFolder & "\" & RptName)
    
    'Wait for the compression process to finish
    Application.Wait Now + TimeValue("00:00:03")
    
    ThisWorkbook.Activate
    
    ExportExpenseReport = RptFolder & "\" & RptName
    
    'Clean Exit
    Exit Function
    
ErrorExit:
    NewWB.Close SaveChanges:=False
    ExportExpenseReport = "Error - Invalid File name: " & RptName
    
End Function
 
Upvote 0
i paid for a consultant to do code for me
thanks
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,088
Members
449,095
Latest member
gwguy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top