vbams-wordbatch-processingexport-to-pdf

Save dialog pops, despite SaveChanges:=wdDoNotSaveChanges


I have a macro that converts (exports) Word documents inside a folder into PDF. Word keeps popping up the save dialog, which kills the idea of a batch operation.

The command

ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

helped me on other occasions.

Sub Loop_through_files()
    Dim cDocuments As New Collection
    Dim sPath As String, sFilter As String
    Dim sCurrentDocName As String, sFullname As String
    Dim i As Long
    Dim xNewName As String
    Dim xIndex As Integer

    sPath = "C:\Users\xxxxxx\Desktop\ConvertPDF"
    sFilter = "*.DOC*"
    Set cDocuments = Nothing

    sCurrentDocName = Dir(sPath & "\" & sFilter)

    Do Until sCurrentDocName = ""
        cDocuments.Add Item:=sCurrentDocName
        sCurrentDocName = Dir
    Loop
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    For i = cDocuments.Count To 1 Step -1              '
        sFullname = sPath & "\" & cDocuments(i)
        xIndex = InStr(cDocuments(i), ".")
        xNewName = Left(cDocuments(i), xIndex) + "pdf"
        Documents.Open FileName:=sFullname, _
          ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
          PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
          WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
          wdOpenFormatAuto, XMLTransform:=""
        ActiveDocument.ExportAsFixedFormat OutputFileName:=sPath & "\" & xNewName, _
          ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
          wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
          Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
          CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
          BitmapMissingFonts:=True, UseISO19005_1:=False
        ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

        ActiveWindow.Close
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

Solution

  • ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
    

    In the Close method calls you need to specify the OriginalFormat parameter which is represented by the WdOriginalFormat enumeration:

    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges, OriginalFormat:=wdOriginalDocumentFormat