Solved

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

Posted on 2011-09-15
4
464 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
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 …

761 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