vendredi 6 avril 2018

VBA Word - Nested IF Function, A Macro Running Other Macros in all Documents in a Folder

I'm a beginner at coding. So I would like to know how I could use nesting to code a macro (for VBA Word) that runs multiple other macros in all documents in a specified folder. I am trying to employ nesting by having the outer loop open all the documents in a folder (a user will input the location of the folder using InputBox), and within this loop, all the macros will be applied.

So far I know that this is what works perfectly (the code opens all documents in the specified folder);

Sub nestingMacro()

    Dim currentFile As String
    Dim location As String
    location = InputBox("Location of folder")
    If Right(location, 1) <> "\" Then location = location + "\"
    currentFile = Dir(location & "*.doc*")
    Do While (currentFile <> "")
        Documents.Open FileName:=location & currentFile
        currentFile = Dir()
    Loop

End Sub

I tried adding the following;

Sub nestingMacro()

    Dim currentFile As String
    Dim location As String
    location = InputBox("Location of folder")
    If Right(location, 1) <> "\" Then location = location + "\"
    currentFile = Dir(location & "*.doc*")
    Do While (currentFile <> "")
        Documents.Open FileName:=location & currentFile
        currentFile = Dir()

    If currentFile <> "" Then

    'the name of the macros below
    Call findReplaceStyle
    Call countErrorsQuality
    Call saveClose

    End If

    Loop

End Sub

Yes, it opens all documents in a folder, however, it runs the macros only on two of the documents then nothing happens to the others. How can I solve this?

Is there a better way to write the function IF, in order to run the macros on all documents using nesting?

Also is there a way to run the macros without actually calling their names?

Thank you!

Aucun commentaire:

Enregistrer un commentaire