Solved

How to include VBA variables and field codes in a Word 2003 footer

Posted on 2011-09-15
4
474 Views
Last Modified: 2012-08-13
I need to include a variable and a field code in a footer.

The var is called strReference
The field code is PAGE of NUMPAGES

so the final thing in the footer looks like this

Q1000023
Page 1 of 6

I can get the variable in OK, but I cannot figure out how to append the field code.

right justified

Thanks

Rich
0
Comment
Question by:STOCRIC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:vsudip
ID: 36542162
0
 

Author Comment

by:STOCRIC
ID: 36542191
I had looked at this before I posted the question, it doesn't address how to use a variable with a field code, only how to put nested field codes.

this is what I'm doing right now

'sets the footers for section 2 (all but the covering letter)
    With ActiveDocument.Sections(2).Footers(wdHeaderFooterPrimary).Range
        .Text = "KSB Reference: " & strKSB_Ref
   
    End With

I need the Page X of Y on the next line of the footer

Thanks
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 36542224
There is an Autotext entry for the field codes, however here is a more basic way.

It can be tricky to mix positioning text and fields, so I prefer to insert the text and then the fields
Sub AddFields()
    Dim rng As Range
    Dim strReference As String
    Dim fld As Field
    strReference = "Some Text"
    
    Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
    rng.Text = strReference & vbCr & " of "
    rng.Select
    
    Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs.Last.Range
    rng.Collapse wdCollapseStart
    Set fld = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Add(rng, wdFieldPage, , False)
    
    Set rng = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Paragraphs.Last.Range
    rng.Collapse wdCollapseEnd
    Set fld = ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Add(rng, wdFieldNumPages, , False)
    
End Sub

Open in new window

0
 

Author Closing Comment

by:STOCRIC
ID: 36542434
Enjoy the cigar.

thanks very much indeed.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question