Word

Change Header in a Batch of Files

Ease of Use

Intermediate

Version tested with

2000 

Submitted by:

R_Rajesh

Description:

Change a logo or other graphic, or change the entire header in a folder full of Word documents all at once. 

Discussion:

Maybe your company's logo has changed and you need to replace the logo in all the documents in a folder. The first procedure (sub) below opens all the Word documents in a directory. In each file, it replaces the first object (shape/graphic) in the header with the contents of the clipboard. The second procedure (sub) below deletes the entire header and replaces it with the contents of the clipboard. 

Code:

instructions for use

			

Sub ReplaceJustLogo() Dim wrd As Word.Application Set wrd = CreateObject("word.application") wrd.Visible = True AppActivate wrd.Name 'Change the directory to YOUR folder's path fName = Dir("C:\temp\*.doc") Do While (fName <> "") With wrd 'Change the directory to YOUR folder's path .Documents.Open ("C:\Temp\" & fName) If .ActiveWindow.View.SplitSpecial = wdPaneNone Then .ActiveWindow.ActivePane.View.Type = wdPrintView Else .ActiveWindow.View.Type = wdPrintView End If .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader .Selection.WholeStory If .Selection.InlineShapes.Count <> 0 Then .Selection.InlineShapes(1).Delete .Selection.Paste .ActiveDocument.Save .ActiveDocument.Close End With fName = Dir Loop Set wd = Nothing End Sub Sub ReplaceEntireHdr() Dim wrd As Word.Application Set wrd = CreateObject("word.application") wrd.Visible = True AppActivate wrd.Name 'Change the directory to YOUR folder's path fName = Dir("C:\temp\*.doc") Do While (fName <> "") With wrd 'Change the directory to YOUR folder's path .Documents.Open ("C:\Temp\" & fName) If .ActiveWindow.View.SplitSpecial = wdPaneNone Then .ActiveWindow.ActivePane.View.Type = wdPrintView Else .ActiveWindow.View.Type = wdPrintView End If .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader .Selection.WholeStory .Selection.Paste .ActiveDocument.Save .ActiveDocument.Close End With fName = Dir Loop Set wrd = Nothing End Sub

How to use:

  1. Copy the appropriate code above for your needs.
  2. Open a blank Word document.
  3. Hit Alt+F11 to open the Visual Basic Editor.
  4. From the menu, choose Insert-Module.
  5. Paste the code into the code window at right.
  6. Change the paths in the code to work for your specific needs.
  7. Close the VBE and save the file if desired.
 

Test the code:

  1. Copy either a logo or entire header, depending on which code you will run. The object will be copied to the clipboard.
  2. Hit Tools-Macro-Macros and double-click the appropriate macro: either ReplaceJustLogo or ReplaceEntireHeader.
 

Sample File:

No Attachment 

Approved by Anne Troy


This entry has been viewed 143 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express