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
Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

828 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