View Full Version : Add text to footer
gav12345
07-03-2012, 08:19 AM
Hello,
We're trying to insert an ID number into the footer of a Word document based on certain conditions. The conditions are as follows (we're OK for conditions 1 and 2 - they are just there to give you the whole picture):
1.) That if there is no text currently present in the footer, just insert the ID number (which we are currently doing).
2.) If there is a file path present in the footer, replace the file path with the ID number (which we are currently doing)
3.) If there is existing text in the footer, which is not a valid file path, insert the ID number after the last existing text character.
Can anyone tell me the simplest way to address condition 3? I can find some documentation for counting characters / inserting text for the main body of the document, but not for the footer section (and am fairly new to VBA in any case).
Please let me know if you need more info, otherwise very grateful for any suggestions.
Thanks, Gavin
macropod
07-03-2012, 04:36 PM
Hi Gavin,
Without seeing your code, I can't provide specific advice. However, having tested your first two conditions and found that neiter applies, you can use something along the lines of:
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.InsertAfter "ID"
gav12345
07-06-2012, 07:09 AM
Thanks Paul - I'm hoping to give that a shot on Monday (as usual too many balls in the air) - I'll let you know how it goes.
Gavin
gav12345
07-09-2012, 07:38 AM
Thanks again Paul, very helpful. Because I didn't know the text I was looking for, I used :
Scn2.Footers(wdHeaderFooterPrimary).Range.Characters.Last.InsertAfter (ID)
Gavin
gav12345
07-10-2012, 01:46 AM
Hi Paul - I've come across a wee issue with this. Unfortunately the code above doesn't seem to recognise page numbers as text. The code is currently assuming there is no text present, so then overwriting the page number with the ID string.
Do you know how I can identify whether there is a page number present in the footer, and if so how I might then insert the ID without overwriting it?
Something like :
If Section(1).pagenumber.ispresent = true then
Scn2.Footers(wdHeaderFooterPrimary).Range.Characters.Last.InsertAfter (Section(1).pagenumber) ID
...forgive the gibberish code, but you get the idea...
(I'll post this as a new post as well since you'll likely think this one's closed).
Thanks, Gavin
macropod
07-10-2012, 01:54 AM
Frankly, notwithstanding that having '.Characters.Last' achieves nothing, I can't see how it could overwrite anything. Either version of the code explicitly inserts the ID after whatever's already there.
gav12345
07-10-2012, 02:20 AM
Hi Paul - Didn't expect such a quick response from your part of the world...!
Sorry, I wasn't clear. Because the code doesn't recognise the page number as text, the .Find.Found is false, so the code moves on to the next 'Else' - see below. The next bit of the code just sticks the ID in the report footer and overwrites everything, on the basis that earlier code has found anything that shouldn't be overwritten. Thanks, Gavin
'Else insert the identifying text string and document id after the last existing text character in the report footer
Else
With .Find
.Text = "?"
.MatchWildcards = True
.Execute
End With
If .Find.Found Then
'Insert the ID after the last character in the footer section
Scn.Footers(wdHeaderFooterFirstPage).Range.Characters.Last.InsertAfter vbCrLf & "TM Doc ID: " & idStr
'Else if the footer section contains no text at all then insert the
'identifying text string and doc id from scratch
Else
doc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Text = "TM Doc ID: " & idStr
doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Font.Size = 8
End If
macropod
07-10-2012, 03:33 AM
Well, doing it that way seems rather silly. Doing a Find for '?' will always return a result because as soon as you access the range it'll have a paragraph break to find! In any event, shouldn't you be trying to find if 'TM Doc ID' is already there, not just anything at all? I'd need to see more of your code to really make sense of it, but this should get you out of your bind:
With .Find
.Text = "[?]{2,}"
.MatchWildcards = True
.Execute
End With
If .Find.Found Then
'Insert the ID after the last character in the footer section
Scn.Footers(wdHeaderFooterFirstPage).Range.InsertAfter vbCrLf & "TM Doc ID: " & idStr
'Else if the footer section contains no text at all then insert the
'identifying text string and doc id from scratch
Else
doc.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = False
doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.InsertAfter vbCrLf & "TM Doc ID: " & idStr
doc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs.Last.Font.S ize = 8
End If
gav12345
07-10-2012, 07:15 AM
Thanks Paul - No luck with that unfortunately. However, there is something unusual about the page number on that document - It's an old document and I can't seem to reproduce the issue...when I create a new, numbered, document myself, there's no problem keeping the page number and inserting the ID underneath it. I think I will leave this issue for now.
The code that checks for the Doc ID is actually further up - I generally try to post as small a code snippet as possible on forums, so that I'm not asking for too much help...afraid that may have been a bad strategy in this case. By the time the code has reached the snippet above, it has established that there's no existing ID, and no file path to be overwritten.
(Regarding the '?'...I'm learning as I go :-))
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.