Consulting

Results 1 to 9 of 9

Thread: Add text to footer

  1. #1
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location

    Add text to footer

    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

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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"
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location
    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

  4. #4
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location
    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

  5. #5
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location
    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

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location
    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

    [VBA] '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[/VBA]

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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:
    [vba]
    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[/vba]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Regular
    Joined
    May 2012
    Posts
    16
    Location
    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 :-))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •