Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-27-2021, 02:51 PM
ITSteve ITSteve is offline Macro that inserts header text fails when saved as .dotm Windows 10 Macro that inserts header text fails when saved as .dotm Office 2016
Novice
Macro that inserts header text fails when saved as .dotm
 
Join Date: Apr 2021
Posts: 2
ITSteve is on a distinguished road
Default Macro that inserts header text fails when saved as .dotm

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
Reply With Quote
  #2  
Old 04-27-2021, 03:01 PM
macropod's Avatar
macropod macropod is offline Macro that inserts header text fails when saved as .dotm Windows 10 Macro that inserts header text fails when saved as .dotm Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

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]
Reply With Quote
  #3  
Old 04-28-2021, 06:16 AM
ITSteve ITSteve is offline Macro that inserts header text fails when saved as .dotm Windows 10 Macro that inserts header text fails when saved as .dotm Office 2016
Novice
Macro that inserts header text fails when saved as .dotm
 
Join Date: Apr 2021
Posts: 2
ITSteve is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
dotm, headers, macro in word

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro that inserts header text fails when saved as .dotm 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 that inserts header text fails when saved as .dotm macro not saved in normal.dotm silveredge8181 Word 8 06-09-2014 07:58 PM
Macro that inserts header text fails when saved as .dotm Word fails to save backup of previous version of saved document musawwir Word 1 11-05-2012 05:20 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:28 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