Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-20-2019, 07:24 AM
terenceyip terenceyip is offline Run a macro on multiple docx. files Windows 10 Run a macro on multiple docx. files Office 2019
Novice
Run a macro on multiple docx. files
 
Join Date: Mar 2019
Posts: 3
terenceyip is on a distinguished road
Question Run a macro on multiple docx. files

Hello there. A complete novice at coding, I tried to adapt the code created by macropod in https://www.msofficeforums.com/word-...ocx-files.html for my own purpose, but could not get it to work. Would there be anyone who could take a look and tell me what is wrong? Many thanks.




Code:
Dim FSO As Object, oFolder As Object, StrFolds As String
 
Sub Main()
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder
StrFolds = vbCr & TopLevelFolder
If FSO Is Nothing Then
  Set FSO = CreateObject("Scripting.FileSystemObject")
End If
'Get the sub-folder structure
Set TheFolders = FSO.GetFolder(TopLevelFolder).SubFolders
For Each aFolder In TheFolders
  RecurseWriteFolderName (aFolder)
Next
'Process the documents in each folder
For i = 1 To UBound(Split(StrFolds, vbCr))
  Call UpdateDocuments(CStr(Split(StrFolds, vbCr)(i)))
Next
End Sub
 
 
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFolds = StrFolds & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub


Sub UpdateDocuments(oFolder As String)
Application.ScreenUpdating = False
Dim strInFolder As String, strOutFold As String, strFile As String, wdDoc As Document
strInFolder = oFolder
If strInFolder = "" Then Exit Sub
strFile = Dir(strInFolder & "\*.docx", vbNormal)
'Check for documents in the folder - exit if none found
If strFile <> "" Then strOutFold = strInFolder & "\Output\"
'Test for an existing outpfolder & create one if it doesn't already exist
If Dir(strOutFold, vbDirectory) = "" Then MkDir strOutFold
strFile = Dir(strInFolder & "\*.docx", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strInFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  With wdDoc



' copy the styles
ActiveDocument.CopyStylesFromTemplate ("C:\Users\Terence Yip\AppData\Roaming\Microsoft\Templates\normal.dotm")

' replace headings
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 8")
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 5")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 7")
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 4")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    
        Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 9")
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Heading 6")
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    
    End With




    'Save and close the document
    .SaveAs FileName:=strOutFold & .Name, AddToRecentFiles:=False
    .Close
  End With
  
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Last edited by macropod; 03-20-2019 at 05:29 PM. Reason: Added code tags & split to new thread
Reply With Quote
  #2  
Old 03-20-2019, 01:52 PM
macropod's Avatar
macropod macropod is offline Run a macro on multiple docx. files Windows 7 64bit Run a macro on multiple docx. files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by terenceyip View Post
I tried to adapt the code created by macropod for my own purpose, but could not get it to work. Would there be anyone who could take a look and tell me what is wrong?
Answering that question depends on what you expect the code to do that it's not doing.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 03-20-2019, 04:54 PM
terenceyip terenceyip is offline Run a macro on multiple docx. files Windows 10 Run a macro on multiple docx. files Office 2019
Novice
Run a macro on multiple docx. files
 
Join Date: Mar 2019
Posts: 3
terenceyip is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Answering that question depends on what you expect the code to do that it's not doing.

Hello Macropod, thanks for your reply. What I intended to do was to copy the styles from a dotm file to hundreds of word files located in different subfolders, as well as replacing all heading 8 to heading 5, heading 7 to heading 4 and heading 9 to 6.


To this end, I simply copied the code from #16 and replaced the part between "With .Range.Find" and " .Execute Replace:=wdReplaceAll". But when I ran the macro, I ran into a "Compile error: Invalid or unqualified reference", and the ".Name" in the line ".SaveAs FileName:=strOutFold & .Name, AddToRecentFiles:=False" was highlighted.
Reply With Quote
  #4  
Old 03-20-2019, 05:23 PM
macropod's Avatar
macropod macropod is offline Run a macro on multiple docx. files Windows 7 64bit Run a macro on multiple docx. files Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Simply change all of your:
Code:
  With wdDoc
  ...
  End With
block with:
Code:
  With wdDoc
    ' copy the styles
    .CopyStylesFromTemplate ("C:\Users\Terence Yip\AppData\Roaming\Microsoft\Templates\normal.dotm")
    ' replace headings
    With .Range.Find
      .Find.ClearFormatting
      .Replacement.ClearFormatting
      .Text = ""
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindContinue
      .Format = True
      .Style = "Heading 7"
      .Replacement.Style = "Heading 4"
      .Execute Replace:=wdReplaceAll
      .Style = "Heading 8"
      .Replacement.Style = "Heading 5"
      .Execute Replace:=wdReplaceAll
      .Style = "Heading 9"
      .Replacement.Style = "Heading 6"
      .Execute Replace:=wdReplaceAll
    End With
    'Save and close the document
    .Close True
  End With
You can also delete all the strOutFold code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 03-20-2019, 05:56 PM
terenceyip terenceyip is offline Run a macro on multiple docx. files Windows 10 Run a macro on multiple docx. files Office 2019
Novice
Run a macro on multiple docx. files
 
Join Date: Mar 2019
Posts: 3
terenceyip is on a distinguished road
Default

Works like a charm. Thank you very much, Macropod!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a macro on multiple docx. files Run a macro on multiple docx. files Peter Carter Word VBA 27 12-15-2022 04:10 PM
Macro for multiple RTF files papapaleo Word VBA 1 07-28-2017 04:56 AM
Run a macro on multiple docx. files Seach ''WORD'' in multiple doc/docx files (different folders!) at the same time Abcde Word VBA 22 06-01-2017 10:33 PM
Run a macro on multiple docx. files Macro to change all text color to black in all docx files in a selected folder joewoods Word VBA 13 05-16-2016 06:29 PM
Run a macro on multiple docx. files looking for macro for multiple files bolk Word 3 05-03-2011 05:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft