Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-05-2020, 03:00 PM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default Batch change the footer in multiple files with footer from another file

Hi to all.


I have many many word files inside a folder and subfolders and i would like to change their footer.

I would like to change/replace the footer to all these files with the footer i use in a specific word file (let's say this specific word file is my guide).

So I was wondering if there is a macro to choose the folder i want and then change the footer in all word documents at once (in that folder and it's subfolders).

i know how to use custom process from Document Batch Processes, so if there is a macro for Document Batch Processes it would be great.

Thanks in advance for your time and help.
Reply With Quote
  #2  
Old 02-05-2020, 09:18 PM
macropod's Avatar
macropod macropod is offline Batch change the footer in multiple files with footer from another file Windows 7 64bit Batch change the footer in multiple files with footer from another file 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

Try running the following macro from the document containing the footer you want to replicate:
Code:
Option Explicit
Dim FSO As Object, oFolder As Object, StrFldrs As String
Dim DocSrc As Document, DocTgt As Document, Rng As Range
Dim StrPth As String, StrNm As String, StrSrc As String
 
Sub Main()
Application.ScreenUpdating = False
Dim TopLevelFolder As String, TheFolders As Variant, aFolder As Variant, i As Long
TopLevelFolder = GetFolder: If TopLevelFolder = "" Then Exit Sub
Set DocSrc = ActiveDocument: StrSrc = DocSrc.FullName
Set Rng = DocSrc.Sections.First.Footers(wdHeaderFooterPrimary).Range
StrFldrs = 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(StrFldrs, vbCr))
  Call UpdateDocuments(CStr(Split(StrFldrs, vbCr)(i)))
Next
Set Rng = Nothing: Set DocTgt = Nothing: Set DocSrc = Nothing
Set TheFolders = Nothing: Set FSO = Nothing
Application.ScreenUpdating = True
End Sub

Sub UpdateDocuments(StrPth As String)
StrNm = Dir(StrPth & "\*.doc", vbNormal)
While StrNm <> ""
  If StrPth & "\" & StrNm <> StrSrc Then
    Set DocTgt = Documents.Open(FileName:=StrPth & "\" & StrNm, AddToRecentFiles:=False, Visible:=False)
    With DocTgt
      With .Sections.First.Footers(wdHeaderFooterPrimary).Range
        .FormattedText = Rng.FormattedText
        .Characters.Last.Text = vbNullString
      End With
      .Close True
    End With
  End If
  StrNm = Dir()
Wend
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
 
Sub RecurseWriteFolderName(aFolder)
Dim SubFolders As Variant, SubFolder As Variant
Set SubFolders = FSO.GetFolder(aFolder).SubFolders
StrFldrs = StrFldrs & vbCr & CStr(aFolder)
On Error Resume Next
For Each SubFolder In SubFolders
  RecurseWriteFolderName (SubFolder)
Next
End Sub
For PC macro installation & usage instructions, see: Installing Macros
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-06-2020, 12:22 AM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default

Unfortunately it doesn't work, I get compile error: variable not defined.

I have only docx files, so i changed the line

StrNm = Dir(StrPth & "\*.doc", vbNormal)

to

StrNm = Dir(StrPth & "\*.docx", vbNormal)

but again i get the same error.

P.S. I don't know if it's important to mention, but the footer i want to copy to all other files, contains text, a table (one row) and an image.
Reply With Quote
  #4  
Old 02-06-2020, 12:28 AM
macropod's Avatar
macropod macropod is offline Batch change the footer in multiple files with footer from another file Windows 7 64bit Batch change the footer in multiple files with footer from another file 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

I can't see how that could be possible if you're using the variables as defined in the posted code - StrNm & StrPth are quite clearly defined.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-06-2020, 02:25 AM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I can't see how that could be possible if you're using the variables as defined in the posted code - StrNm & StrPth are quite clearly defined.
let me describe you what i did:

1) Opened the word file that contains the footer i want.
2) Pressed Alt+F11, and choose the file from left panel.
3) From "Insert UseForm" icon, i choose module.
4) I paste the code you wrote here.
5) I pressed F5.
6) Opened a popup window to choose the folder i want and i choose it.
7) After pressing OK button from popup window i get the error i told you.
Reply With Quote
  #6  
Old 02-06-2020, 03:52 AM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default

Ok an update...
I put the file that has the footer i need outside the folder i want the word files to be changed (i put it in my desktop because it was inside the folder i wanted to work with). I run the macro again and now i get another error:

Run-time error '91:
Object variable or With block variable not set.


When i press debug i got the following line with yellow background:

With .Sections.First.Footers(wdHeaderFooterPrimary).Ran ge
Reply With Quote
  #7  
Old 02-06-2020, 05:06 AM
macropod's Avatar
macropod macropod is offline Batch change the footer in multiple files with footer from another file Windows 7 64bit Batch change the footer in multiple files with footer from another file 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

The code must be run against an active document containing the relevant footer. If you're getting an error message on that line, that suggests the active document lacks the relevant footer.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 02-06-2020, 08:35 AM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default

Ok here is the file that contains the footer i want to take and to replace to all other word files.
When i run the macro this file is active.
Attached Files
File Type: docx 2.docx (87.1 KB, 7 views)
Reply With Quote
  #9  
Old 02-06-2020, 02:11 PM
macropod's Avatar
macropod macropod is offline Batch change the footer in multiple files with footer from another file Windows 7 64bit Batch change the footer in multiple files with footer from another file 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

I have run the code with your document as the active document and do not get that error. I suggest you re-copy the code and paste into your attachment's 'ThisDocument' code module - and ensure your attachment is the active document when you run the code. You might then even change:
Set DocSrc = ActiveDocument
to:
Set DocSrc = ThisDocument
so that your attachment does not need to be the active document when you run the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 02-10-2020, 12:40 AM
kalagas kalagas is offline Batch change the footer in multiple files with footer from another file Windows 10 Batch change the footer in multiple files with footer from another file Office 2019
Novice
Batch change the footer in multiple files with footer from another file
 
Join Date: Nov 2019
Posts: 9
kalagas is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
I have run the code with your document as the active document and do not get that error. I suggest you re-copy the code and paste into your attachment's 'ThisDocument' code module - and ensure your attachment is the active document when you run the code. You might then even change:
Set DocSrc = ActiveDocument
to:
Set DocSrc = ThisDocument
so that your attachment does not need to be the active document when you run the code.
Ok i managed to make it work.
Thanks a lot for your help and patience.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch replace Header and Footer and QuickStyle Artmax Word VBA 14 01-11-2024 05:36 PM
Batch change the font style in multiple word files inside folder and subfolders kalagas Word VBA 11 10-05-2023 05:13 AM
Batch change the footer in multiple files with footer from another file Batch applying a macro to remove Header and Footer using Batch Auto Addin Edszx Word VBA 2 05-27-2019 11:16 PM
I have 20 page word document with a footer. Can i change page # 10 footer only? aligahk06 Word 2 10-25-2017 04:53 AM
Batch change the footer in multiple files with footer from another file Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:14 PM.


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