Hello Greg,
thank you for this extensive revised code.
Does the below look correct, I simply replaced the macro name.
The compiler said ambiguous name detected FormatParagraphs.
Option Explicit
Private oDoc As Document
Private FSO, oFolder, oFile
Sub LoopThroughFolder()
Dim strMainFolder As String
strMainFolder = BrowseForFolder()
Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(strMainFolder)
On Error Resume Next
For Each oFile In oFolder.Files
Set oDoc = Documents.Open(oFile.path, , , False, , , , , , , , False)
FormatParagraphs oDoc
oDoc.Close wdSaveChanges
Next
'Get subdirectories
RecursiveFolder oFolder
Set FSO = Nothing
Set oFolder = Nothing
Set oFile = Nothing
lbl_Exit:
Exit Sub
End Sub
Sub RecursiveFolder(xFolder)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
For Each oFile In SubFld.Files
Set oDoc = Documents.Open(oFile.path, , , False, , , , , , , , False)
FormatParagraphs oDoc
oDoc.Close wdSaveChanges
Next
RecursiveFolder SubFld
Next
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim oShell As Object
'Create a file browser window at the default folder
Set oShell = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = oShell.self.path
On Error GoTo 0
Set oShell = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
Sub FormatParagraphs(oDocPassed As Word.Document)
MsgBox oDocPassed.Name
End Sub
Sub FormatParagraphs()
Dim oPara As Paragraph
For Each oPara In ActiveDocument.Paragraphs
oPara.Range.Shading.BackgroundPatternColor = RGB(220, 180, 250)
Next
End Sub
Or Should I put my macro in another module. I apologize for the newbie questions - , it would be nice if that VBA editor gave me an alternative suggestion to the error - here is hoping.
Thank you
Saphire