![]() |
|
#1
|
|||
|
|||
![]()
I have zero experience in Word VBA. I am looking for a way to change the header in one document and update multiple documents in a specified folder.
We have specification documents that need to have their headers updated quite regularly. I'd like to be able to use a Word document as the template where any changes that need to take place will occur in the document. I would then like to use that document to change multiple documents automatically. These other documents would be located in a folder. Ideally, the process flow would be like this: Open "template.docx" Open Header/add date "March 1, 2018" Close Header Save document Run macro - select folder location of all documents that need the header changed. Currently we copy/paste the updated header into each document separately which involves opening document/open header/paste/close header/save/close document repeat 100+ times. This process takes place for one project, extrapolate to the 100s of projects my company produces...you get the picture. I've stumbled upon this thread and code but have not been able to understand it: https://www.msofficeforums.com/word-...html#post62942 It appears as if this code should work but I must be missing something as to I don't understand how to make it grab the folder location and institute the changes made to my template document. Version: Word 2013 Thank you for your time, Last edited by cspande; 02-09-2018 at 10:13 AM. Reason: Removed name |
#2
|
||||
|
||||
![]()
Try:
Code:
Sub UpdateDocumentHeaders() Application.ScreenUpdating = False Dim strFolder As String, strFile As String, StrTxt As String Dim wdDocTgt As Document, wdDocSrc As Document strFolder = GetFolder If strFolder = "" Then Exit Sub StrTxt = InputBox("Text to add to headers? e.g. ""March 1, 2018""") If Trim(StrTxt) = "" Then Exit Sub Set wdDocSrc = ActiveDocument strFile = Dir(strFolder & "\*.doc", vbNormal) While strFile <> "" If strFolder & "\" & strFile <> wdDocSrc.FullName Then Set wdDocTgt = Documents.Open(FileName:=strFolder & "\" & strFile, _ AddToRecentFiles:=False, Visible:=False) With wdDocTgt .Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertAfter StrTxt .Close SaveChanges:=True End With End If strFile = Dir() Wend Set wdDocSrc = Nothing: Set wdDocTgt = 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
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Macropod,
Thanks! That macro works great. I'm curious though, (1.)how can I manipulate it to include more than one line of text and various spacing? (2.)Or is it possible for me to create a custom header in a document(headertemplate.docx) that can be referenced when the script runs and implement it into the documents in the selected folder? (1.) For example, my header looks like this: Code:
Project Location Date Project Name Drawing Set Contract Number Issuance Date Code:
Project Location Date Project Name Drawing Set Contract Number Issuance Date March 1, 2018 I'll do some research into other scripts on how to read a "template" file and try to put something together. Thank you for spending the time helping me! cspande |
#4
|
||||
|
||||
![]()
I'd have thought you'd want either 'Date' or 'Issuance Date' replaced with your actual date (e.g. March 1, 2018), or your actual date insert after 'Date' or 'Issuance Date', not simply to have your actual date inserted after what's already in you header. What you're getting, though, is what you specified.
Any of the individual items in the header can be replaced or appended to, or even the whole header can be overwritten. You need to be clear about what your requirements are.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
I see that I have been unclear. Ideally, the whole header would be re-written.
For example: Template Code:
Building Name, Complex Name Original Start Date Project Name Drawing Set Company Contract Information Issuance Date Code:
Warehouse ABC - Industrial Complex 1 20 December 2017 Rooftop AHU Retrofit Bid Set Company B Contract No. 1234-456-89 Reissued 13 February 2018 |
#6
|
||||
|
||||
![]()
In that case, if you run the macro from a document containing the required header, you could simply delete:
Code:
StrTxt = InputBox("Text to add to headers? e.g. ""March 1, 2018""") If Trim(StrTxt) = "" Then Exit Sub .Sections(1).Headers(wdHeaderFooterPrimary).Range. InsertAfter StrTxt to: Code:
With .Sections(1).Headers(wdHeaderFooterPrimary).Range .FormattedText = wdDocSrc.Sections(1).Headers(wdHeaderFooterPrimary).Range.FormattedText .Characters.Last.Delete End With
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
That works flawlessly. We removed the pop up that asks what to add to the header. Thanks for your time and effort. I could not have even begun to develop it on my own, but I am enjoying the process.
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word 2016 Multi-file Macro issues | IneedHelpWithWord | Word VBA | 1 | 08-08-2017 09:29 PM |
![]() |
DrDress | Word | 4 | 04-17-2017 06:39 PM |
![]() |
aaronbauer1980 | Excel Programming | 1 | 04-15-2016 05:53 PM |
![]() |
BriMan83 | Mail Merge | 1 | 04-24-2013 11:35 PM |
![]() |
Evgeniy | Word | 1 | 02-04-2012 01:36 PM |