I am writing an Excel VBA macro that copies text to a Word for Windows file and adds formatting to it later.
It uses a .dotx template that contains a logo. In the lower left corner is a textbox with a serial-number. The text of the serial-number is written vertically (from the bottom upwards).
Using trial and error I managed to write one serial-number to the textbox using:
serialnumber = "abc1x"
wdoc.Sections(1).Headers(wdHeaderFooterEvenPages).Shapes(2).TextFrame.TextRange.text
= serialnumber
So I found the right object to write to. Now I get the same serialnumber on every page.
My goal is to get an increasing serialnumber on the pages: The serial-number has the shape:
- On page 1: abc1x
- on page 2: abc2x
- on page 3: abc3x
- ...
- on page 10: abc10x
It's the page number surrounded by 2 strings.
On a different project I did something similar. I wrote "Page 1 of 10" etc. with the following script:
Dim uRange As Object
Dim uneven As Object
Set uneven = wdoc.Sections(1).Footers(wdHeaderFooterPrimary)
Set uRange = wdoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
uRange.Delete
uneven.Range.InsertAfter "Page "
uRange.SetRange Start:=uneven.Range.End + 1, End:=uneven.Range.End + 1
wdoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Add
Range:=uRange, Type:=wdFieldEmpty, text:= _
"PAGE \* Arabic ", PreserveFormatting:=True
uRange.SetRange Start:=uneven.Range.End + 1, End:=uneven.Range.End + 1
uneven.Range.InsertAfter " of "
uRange.SetRange Start:=uneven.Range.End + 1, End:=uneven.Range.End + 1
wdoc.Sections(1).Footers(wdHeaderFooterPrimary).Range.Fields.Add
Range:=uRange, Type:=wdFieldEmpty, text:= _
"NUMPAGES \* Arabic ", PreserveFormatting:=True
How do I insert the text around the Page field in the TextBox?
(Side note: What is the difference between the range and rangetext object?)
Remarks: I will have to apply the solution to the even and uneven pages seperately. That doesn't pose a problem. To make matters more difficult: I am obliged to keep the text-field, because it comes from the hands of the corporate identity folks.