Search code examples
vbams-wordfilepicker

Need to call other Word macros in mass update tool


Quick summary: I need to call additional macros to modify documents inside of an existing code that mass converts Word files to PDF (if possible).

Longer story: I have code from a long time ago (credit to wherever I found it a decade ago). It uses a file dialog to allow selecting multiple files, and then converts those selected files to PDF. I have almost 1,800 Word documents that I need to process (using the code @timothyrylatt helped with here: How to find table column, then move down and replace the cell's content IF it is "N/A") and then convert to PDF afterwards. I tried using the 'Call' feature to call the "Demo" macro, and changing the save settings but the files only convert to PDF without calling the other "Demo" macro. I tried calling it in different areas as well, but to no avail.

Note: If it is not possible to add to this existing code, is there still a way to at least select the multiple files, run the Demo macro, then save and close in a similar manner?

Thank you in advance for any assistance!

Sub MassUpdate()

Dim wDoc As Word.Document
Dim FoundFile As Variant
 
Dim wDialog As FileDialog
Set wDialog = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
wDialog.AllowMultiSelect = True
 
If wDialog.Show <> -1 Then
    Exit Sub
End If
 
For Each FoundFile In wDialog.SelectedItems
     
    Set wDoc = Documents.Open(FoundFile, ReadOnly:=False, Visible:=False)
     
     Call Demo

     
    wDoc.ExportAsFixedFormat _
    OutputFileName:=wDoc.Path & "\" & Left(wDoc.Name, InStrRev(wDoc.Name, ".")) & "pdf", _
    ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, _
    OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, _
    Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
    CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
    BitmapMissingFonts:=True, UseISO19005_1:=False
     
    wDoc.Close SaveChanges:=True
     
Next
         Dim Answer
    Answer = MsgBox("Update more files?", vbYesNo, "Run Macro?")
    If Answer = vbYes Then
    Call MassUpdate
    End If
 End Sub

Solution

  • I would suspect that the two routines may be targeting different documents. Try modifying your Demo routine to take a document as an input argument:

    Sub Demo(targetDoc as Document)
       Application.ScreenUpdating = False
       Dim r As Long, c As Long
       With targetDoc.Range
    

    You would then modify the Call line to (there is no need to use Call):

    Demo wDoc