简体   繁体   中英

Avoiding 'Save As' dialog box in Excel VBA script

I have cobbled together a VBA script that loops through a list of data, changing the value of a single cell on a summary page. That cell drives a number of formulas. After each iteration, the cell range of interest is saved off as a PDF.

I am looking to avoid having to manually hit enter every time the 'save as' dialog box is generated on each loop. Once I deploy this script, I could be looking at 1k+ iterations.

Sub AlterID()

Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String

Set ws = Worksheets("Summary Data")

For Each c In Worksheets("Data").Range("A2:A11").Cells   
    Worksheets("Summary Data").Range("B1").Value = c.Value  

    strFile = ws.Range("D3").Value  
    strFile = ThisWorkbook.Path & "\" & strFile

    myFile = Application.GetSaveAsFilename _
    (InitialFileName:=strFile, _
        FileFilter:="PDF Files (*.pdf), *.pdf", _
        Title:="Select Folder and FileName to save")

    If myFile <> "False" Then
        ws.Range("D3:H9").Select  
        Selection.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=myFile, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
    End If

    Next

End Sub
Sub AlterID()

    Dim ws As Worksheet, c As Range
    Dim strFile As String

    Set ws = Worksheets("Summary Data")

    For Each c In Worksheets("Data").Range("A2:A11").Cells

        ws.Range("B1").Value = c.Value  

        strFile = ThisWorkbook.Path & "\" & ws.Range("D3").Value

        ws.Range("D3:H9").ExportAsFixedFormat _
                         Type:=xlTypePDF, _
                         Filename:=strFile, _
                         Quality:=xlQualityStandard, _
                         IncludeDocProperties:=True, _
                         IgnorePrintAreas:=False, _
                         OpenAfterPublish:=False
    Next

End Sub

Have you tried turning off application alerts?

Application.DisplayAlerts = False 'before your code
Application.DisplayAlerts = True  'after your code

Edit 1

Here is a sub I use to save a file to a PDF

Sub SaveAsPDF()
Dim myValue As Variant
Dim mySheets As Variant

mySheets = Array("Report")
For Each sh In mySheets
    Sheets(sh).PageSetup.Orientation = xlLandscape
Next

uid = InputBox("Enter your UID")
uid = StrConv(uid, vbProperCase)

Application.PrintCommunication = False
    With Sheets("Report").PageSetup
        .FitToPagesWide = 1
        .FitToPagesTall = False
    End With
    Application.PrintCommunication = True

Dim fName As String
fName = "HMB SOX report for week ending " & ActiveSheet.Range("H4").Text
Call selectPage("Report")
With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
          "C:\Users\" & uid & "\Desktop\" & fName, :=xlQualityStandard, _
         IncludeDocProperties:=True, IgnorePrintAreas:=False, Publish:=False
End With    

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM