Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

737 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