Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - Inserting formula into header 1

Status
Not open for further replies.

peejaykay

Programmer
Jan 10, 2007
12
US
Word VBA -How do I insert a formula into a header while still using range? I don't know how to start/end the formula field. I'm trying to insert:

Page { =(intPgNo + {PAGE \* MERGEFORMAT })}

The following works by switching to selection, but I have been unable to produce either the wdFieldEmpty or Control F9 combination. r is the range within the header.

Code:
    r.Collapse Direction:=wdCollapseEnd
    r.InsertAfter Text:=frmMain.txtDate _
        & vbTab & vbTab & "Page "
    '------------------------
    'Insert Page Number field
    '------------------------
    r.Collapse Direction:=wdCollapseEnd
    '*******************************************
    r.Select
    W.Selection.Fields.Add Range:=W.Selection.Range, Type:=wdFieldEmpty, _
        PreserveFormatting:=False
    W.Selection.TypeText Text:="=("
    W.Selection.TypeText Text:=intPgNo & " + "
    W.Selection.Fields.Add Range:=W.Selection.Range, Type:=wdFieldPage
    W.Selection.TypeText Text:=")"
    W.Selection.MoveRight Unit:=wdCharacter, count:=2
    W.Selection.TypeText Text:=vbCr & vbTab
    W.Selection.TypeText Text:="File: " & UCase$(FileName$) & "  "
    W.Selection.TypeText Text:="Page "
    W.Selection.Fields.Add Range:=W.Selection.Range, Type:=wdFieldPage
    W.Selection.TypeText Text:=" of pjk"
    '*******************************************

Thank you in advance
pjk
 
This can be done quite easily. But I have mentioned in other posts that you REALLY should use With statements. And that multiple, multiple selection.typetext instructions is poor code.

In fact, I believe I demonstrated how to do what you are asking.

The clue is you are selecting the range again.
Code:
    r.Select
STOP using Selection! You are part of the way there in that you are using r as a Range...why ruin it by Selecting it????? Do exactly ehat you started to do...collapse...add...collapse...add. Add.Fields does need a Range, but it does NOT have to be SDelection.Range.

Go back to your other posts, I showed how to do this already.

Gerry
My paintings and sculpture
 
Gerry,
Thank you for your previous posts. I guess I didn't express myself clearly. The code shown above is what I'm trying to replace, not what I'm trying to use. I shouldn't even have included it in the post.

I'm trying to insert a formula with a embedded PAGE field.

I can add a formula with
Code:
r.Fields.Add Range:=r, Type:=wdFieldFormula, Text:="=(" & intPgNo & " + )"
and can add a page field
Code:
r.Fields.Add Range:=r, Type:=wdFieldPage
but how do I get the Page field embedded after the + sign?

Your previous posts demonstrated many techniques, but not this one. My attempts to isolate the range to the position within the formula have failed. Sorry for asking what appears to be questions with obvious answers, to everyone but me.
 
The formula obviously yields an error because there isn't an integer after the + sign. I've been trying to insert the wdPageField into that position, without success. The results from the above is displayed as
[highlight]
{EQ=(0 + ) \*MERGEFORMAT}
[/highlight]
I know what I want:
Page { =(intPgNo + {PAGE \* MERGEFORMAT })}
but I don't know how to produce it.
 
You are actually getting an error because of intPgNo. While it is true it will put a value into the formula, it can never update because the update will have no idea what intPgNo is.

Try intPgNo + 2...you will still get an error. For example:
Code:
Dim intPgNo As Integer
Dim r As Range

intPgNo = 41
Set r = Selection.Paragraphs(1).Range
    r.Fields.Add Range:=r, Type:=wdFieldFormula, Text:="=(" & intPgNo & " + 23)"
Set r = Nothing
puts

{ EQ = (41 + 23) \* MERGEFORMAT}

into the field, with no errors (in the processing). BUT there IS an error in the field.

I have been playing with this, and hopefully someone else smarter than me will find a way. However, so far, I have not been able to do it.

It can be done manually, but I have not found a way to insert a proper field into a field with VBA. The problem is that you enter the values as text. And you can not use { and } as text for fields. You can do it as AutoText though.

Gerry
My paintings and sculpture
 
Hi peejaykay,

Try something along the lines of:
Code:
Sub InsertPageField()
ActiveWindow.View.ShowFieldCodes = True
With Selection
    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="=" & intPgNo & "+"
    .MoveRight Unit:=wdCharacter, Count:=4 + Len(intPgNo)
    .Fields.Add Range:=Selection.Range, Type:=wdFieldPage, _
    PreserveFormatting:=False
    .Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End With
End Sub
Cheers

[MS MVP - Word]
 
Or even simpler:
Code:
Sub InsertPageField()
ActiveWindow.View.ShowFieldCodes = True
With Selection
    .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
    PreserveFormatting:=False, Text:="=+" & intPgNo
    .MoveRight Unit:=wdCharacter, Count:=3
    .Fields.Add Range:=Selection.Range, Type:=wdFieldPage, _
    PreserveFormatting:=False
    .Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End With
End Sub
Cheers

[MS MVP - Word]
 
Thanks macropod, but I'm trying to accomplish this without using selection, just sticking to range. The inelegant code from the beginning of the post works, but by using selection as well.
 
Hi peejaykay,

To avoid Selection altogether and put the calculated page number in all headers, you could try something like:
Code:
Sub InsertPageField()
Dim oSection As Section
Dim oHeadFoot As HeaderFooter
Dim fRange As RangeActiveWindow.View.ShowFieldCodes = True
For Each oSection In ActiveDocument.Sections
    For Each oHeadFoot In oSection.Headers
        If Not oHeadFoot.LinkToPrevious Then
            Set fRange = oHeadFoot.Range.Characters(1)
            fRange.Fields.Add Range:=fRange, Type:=wdFieldEmpty, _
                PreserveFormatting:=False, Text:="= +" & intPgNo
            Set fRange = oHeadFoot.Range.Characters(4)
            fRange.Fields.Add Range:=fRange, Type:=wdFieldPage, _
                PreserveFormatting:=False
            fRange.Fields.Update
        End If
    Next
Next
ActiveWindow.View.ShowFieldCodes = False
End Sub
Cheers

[MS MVP - Word]
 
Thank you macropod
I kept trying to move the range instead of using the characters collection. Someone else had suggested using the characters collection, but I didn't know how to implement it. Thanks again, pjk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top