Solved

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

Posted on 2011-09-15
4
445 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
  • 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now