![]() |
|
|
|
#1
|
|||
|
|||
|
I have a macro that inserts header text into the document. It works perfectly when running it in a .docm file. When I save as to a .dotm the header is not inserted. No errors, just a blank header. The rest of the macro that creates the body works fine.
Any ideas why it fails as a template? Thanks. Here is the main part of the code: Code:
For Each objFile In objFolder.Files
'get file path, name and date
strPath = objFile.Path
strDate = Int(objFile.DateLastModified)
StrFName = objFile.Name
'Skip Thumbs.db file if exists
If 0 = InStr(strPath, "Thumbs") Then
'Align Left
Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
'Print Header on first loop
If LoopCounter = 0 Then
'Set Variable equal to Header Range
Set HdrRange = ThisDocument.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
'Text to add to Header
HdrText = "Project Name: " & (ufProjectInfo.tbProjectName.Value)
'Add Text To Word Header
HdrRange.Text = HdrText
'Bold Only First Sentence in Header
'Get length of the project name
ProjectNameLength = ufProjectInfo.tbProjectName.TextLength
Set BoldRange = HdrRange.Words(1) 'Get First Word
'Set selection to the first line of header
BoldRange.SetRange Start:=BoldRange.Start, End:=14 + ProjectNameLength
BoldRange.Font.Bold = True 'Bold Entire Sentence
End If
|
|
#2
|
||||
|
||||
|
Your use of 'ThisDocument' points the macro to them template itself, not to any document you might open. Your use of Selection also invites errors.
For more efficient code to update document headers & footers, see: https://www.msofficeforums.com/62942-post2.html
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Changing ThisDocument to ActiveDocument solved the issue.
This code is a combination of several code examples I hacked away at, so I am sure there are much more elegant ways to write this! Thanks so much for the help. God bless you. |
|
| Tags |
| dotm, headers, macro in word |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Quick Access Toolbar saved in Normal.dotm?
|
gogreen | Word | 2 | 10-08-2015 11:47 AM |
| Macro to add title in header is missing text once macro is run | shawnee24 | Excel Programming | 1 | 05-27-2015 11:50 PM |
| Macro to add title in header is missing text once macro is run | shawnee24 | Word VBA | 3 | 05-27-2015 12:35 PM |
macro not saved in normal.dotm
|
silveredge8181 | Word | 8 | 06-09-2014 07:58 PM |
Word fails to save backup of previous version of saved document
|
musawwir | Word | 1 | 11-05-2012 05:20 PM |