Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2019, 11:06 PM
digitalelise digitalelise is offline Adding text to the header and footer without removing all other content in the header and footer Mac OS X Adding text to the header and footer without removing all other content in the header and footer Office 2016
Novice
Adding text to the header and footer without removing all other content in the header and footer
 
Join Date: Oct 2019
Posts: 2
digitalelise is on a distinguished road
Question Adding text to the header and footer without removing all other content in the header and footer

Hi Folks,



I am new to macros and VBA but muddling my way through.

I am trying to add some text to the documents header and footer but want the two images that are in the template to stay.

When ever I hit the button SetClassification it adds the text but removes all other content in the header and footer.

Any assistance in making this work would be appreciated. I think I am at the edge of my knowledge but so close to having it work perfectly.


Code:
Private Sub cmdSetClassification_Click()

' prepare header footers..
'Dim sHeadFooter As String
'sHeadFooter = Me.txtFurtherInfo.Text

With ActiveDocument.Sections(1)

 .Headers(wdHeaderFooterPrimary).Range.Text = sHeaderFooter
 .Headers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
 .Headers(wdHeaderFooterPrimary).Range.Paragraphs.LeftIndent = 76
 .Headers(wdHeaderFooterPrimary).Range.Font.ColorIndex = wdRed
 .Headers(wdHeaderFooterPrimary).Range.Font.Name = "Gotham Book"
 .Headers(wdHeaderFooterPrimary).Range.Font.Size = 10
 
 .Footers(wdHeaderFooterPrimary).Range.Text = sHeaderFooter
 .Footers(wdHeaderFooterPrimary).Range.Paragraphs.Alignment = wdAlignParagraphCenter
 .Footers(wdHeaderFooterPrimary).Range.Font.ColorIndex = wdRed
 .Footers(wdHeaderFooterPrimary).Range.Font.Name = "Gotham Book"
 .Footers(wdHeaderFooterPrimary).Range.Font.Size = 10
 
End With

Unload Me

End Sub
Attached Files
File Type: dotm template_V02.dotm (22.8 KB, 10 views)
Reply With Quote
  #2  
Old 10-23-2019, 12:30 AM
Guessed's Avatar
Guessed Guessed is offline Adding text to the header and footer without removing all other content in the header and footer Windows 10 Adding text to the header and footer without removing all other content in the header and footer Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Your template doesn't have any graphics in the header so not sure about that part of your request, however when you set the .Range.Text you are replacing whatever used to be there.

Because you are using a template, it should already have the placeholder text sitting in the header/footers and it should already be styled. This dramatically reduces the things you need to do with your code. You can also use Linked Content Controls to further minmise the code. For instance, if your Content Control was linked to the document's Keywords Property you could just do...
Code:
Private Sub cmdSetClassification_Click()
  ' prepare header footers..
  Dim sHeadFooter As String
  sHeadFooter = Me.txtFurtherInfo.Text
  ActiveDocument.BuiltInDocumentProperties("Keywords") = sHeadFooter
  Unload Me
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 10-23-2019, 02:58 AM
gmayor's Avatar
gmayor gmayor is offline Adding text to the header and footer without removing all other content in the header and footer Windows 10 Adding text to the header and footer without removing all other content in the header and footer Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,106
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Using content controls as hinted by Andrew is certainly a sensible approach. e.g. you could put a content control in the header of the template and title it (say) 'HeaderText' then use the following to populate it

Code:
Private Sub cmdSetClassification_Click()
' prepare header footers..
Dim oCC As ContentControl
Dim sHeadFooter As String
    Set oCC = ActiveDocument.SelectContentControlsByTitle("HeaderText").Item(1)
    sHeadFooter = txtFurtherInfo.Text
    oCC.Range = sHeadFooter
    Unload Me
End Sub
or you could write to the start or the end of the header range e.g.
Code:
Private Sub cmdSetClassification_Click()
' prepare header footers..
Dim oHeader As Range
Dim sHeadFooter As String
    Set oHeader = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
    oHeader.Collapse 1    'the start of the range or
    'oHeader.Collapse 0 'the end of the range
    sHeadFooter = txtFurtherInfo.Text
    oHeader.Text = sHeadFooter
    Unload Me
End Sub
which answers the original question regarding the overwriting of range content, but that is nowhere near as convenient should you wish to recall the userform to edit the document

You could also use bookmarks to locate the text and you will find the FillBM macro on my web site to write to the bookmarks and remark the text so that it may be recalled to the userform. I prefer content controls as they are less likely to be accidentally deleted, but that is less of an issue with headers and footers.

You may find Insert Content Control Add-In useful
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Tags
header and footer



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing header from one page but not footer smithurmann Word 1 12-15-2018 11:24 AM
Removing header and footer on two pages in my document oliboi Word 1 11-01-2016 05:27 PM
Header and footer aligned in the footer area ashiqghfr Word 2 07-23-2015 01:14 AM
Adding text to the header and footer without removing all other content in the header and footer Created VBA to Find and Replace in Body, Header and Footer with Highlighting the replacement text QA_Compliance_Advisor Word VBA 11 09-23-2014 04:40 AM
Field codes appearing in header/footer/TOC instead of actual text hgilberg Word 2 03-16-2012 02:52 PM

Other Forums: Access Forums

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