0

First off: Yes, I know, Word 2007 is "ancient"; I do all my work on a laptop that I bought back in 2012, at a time when I wasn't really keen on trying my luck with the newly released Office 2010 in case it turned out to be riddled with flaws.

Now, putting that aside... After a lot of digging through the Internet and applying the trial and error method, I finally managed to construct a macro in Word 2007 that automatically goes through all Word documents in a folder and resets their styles to match the one in the template they're assigned to, so that I don't have to manually do that for the several hundreds of documents that exist on my computer.

Sub UpdateStylesAllDocuments()
    Dim JName As String

    Dialogs(wdDialogFileOpen).Show
    Application.ScreenUpdating = False
    JName = Dir("*.docx")
    While (JName > "")
        Application.Documents.Open FileName:=JName

        ActiveDocument.UpdateStyles

        ActiveDocument.Close SaveChanges:=wdSaveChanges
        JName = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub

� It's working as intended... except that for some reason, it refuses to touch any subfolders that exist within the folder that it's working on. What should I add to the above code to fix that?

braX
  • 11,506
  • 5
  • 20
  • 33
MarqFJA87
  • 101
  • 3
  • 1
    Possible duplicate of [get list of subdirs in vba](https://stackoverflow.com/questions/9827715/get-list-of-subdirs-in-vba) – braX Mar 06 '18 at 17:34
  • @braX I don't see how it's a duplicate. Sure, it involves subdirectories as well, but other than that, I don't see any connection or overlap. – MarqFJA87 Mar 06 '18 at 20:47

1 Answers1

0

You need to make it recursive. First recurse the sub directories and then process the files. This will ensure there is a thread for every directory. To do this, your wdDialogFileOpen needs to return a string of the path selected.

Sub UpdateStylesAllDocuments()
    Dim JName As String
    Dim thePath As String
    thePath = Dialogs(wdDialogFileOpen).Show

    doSubfolders thePath
End Sub

Sub doSubfolders(sPath As String)
    'The recursive call to self
    Dim sDir As String
    sDir = Dir(sPath, vbDirectory)
    ChDir sPath
    Do Until LenB(sDir) = 0
        doSubfolders sDir
        sDir = Dir
    Loop

    Application.ScreenUpdating = False
    JName = Dir("*.docx")
    While (JName > "")
        Application.Documents.Open Filename:=JName

        ActiveDocument.UpdateStyles

        ActiveDocument.Close SaveChanges:=wdSaveChanges
        JName = Dir()
    Wend
    Application.ScreenUpdating = True
End Sub
HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • I tried that code block, but it suffered a "run-time error 76", with an additional "path not found" comment, and highlighting "ChDir sPath" as the apparent point of where error manifests. – MarqFJA87 Mar 06 '18 at 20:45
  • ... I don't know. I copied the code you provided as is. – MarqFJA87 Mar 06 '18 at 21:25
  • Read this whole article. It will change your life: http://www.cpearson.com/excel/DebuggingVBA.aspx – HackSlash Mar 06 '18 at 23:16
  • I've been meaning to apply your suggested advice, but a series of distractions caused me to just give up, do it manually for as many folders as I had the will to do in one sitting, and forget about this issue for a **whole year**, at which point I had a need for a new mass update. So I checked the article... and I'm more confused than ever before; the only thing I got is that "ChDir sPath" is marked simply because it's the "next line to be executed". – MarqFJA87 Mar 03 '19 at 23:11
  • Ok. I see the problem. At the end of that loop sPath is going to be null. I moved the ChDir up where the original directory is loaded. That makes much more sense. Try it again. Debugging techniques would help you see these problems. That's why I linked the debugging article. – HackSlash Mar 04 '19 at 18:09
  • Ugh, no dice. I'm getting a "runtime error 76: path not found" message, with the same "ChDir sPath" line marked as the "next line to be executed" in the debugger. – MarqFJA87 Mar 31 '19 at 01:51
  • That means the path doesn't exist. Check your path string while debugging. I really can't do it for you. – HackSlash Apr 01 '19 at 15:09