Add Text and Text box on every page

N

Natalie Dang

Hi all,

I'm trying to add a line of text and a text box on every page, but they
only show on the first page. Please find my code below:

Dim ileft As Single
Dim iTop As Single
Dim iTotalPages As Integer
Dim iPage As Integer
Dim sText As String
Private objWord As Word.Application
Private objDoc As Word.Document
Dim oTextBox As Word.Shape
Dim oPageStart As Word.Range

Set objWord = New Word.Application
objWord.Documents.Open App.Path & "\test.doc"

Set objDoc = objWord.ActiveDocument

sText = "PRINT DATE: " & FormatDateTime(Now, vbShortDate)

iTop = objDoc.PageSetup.PageHeight - 100
ileft = objDoc.PageSetup.LeftMargin
iTotalPages = objDoc.ComputeStatistics(wdStatisticPages)

For iPage = 1 To iTotalPages

Set oPageStart = objDoc.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute,
Count:=iPage)

iCount = oPageStart.Sections.Count
iTop = oPageStart.PageSetup.PageHeight - 100
ileft = oPageStart.PageSetup.LeftMargin



oPageStart.Document.Shapes.AddTextEffect msoTextEffect2, sText,
"Arial", 14, msoCTrue, msoFalse, ileft, iTop
Set oTextBox =
oPageStart.Document.Shapes.AddTextbox(msoTextOrientationHorizontal, _
Left:=ileft
+ 400, Top:=50, _
Width:=100,
Height:=70)

oTextBox.TextFrame.TextRange.Font.Name = "Point Arial"
oTextBox.TextFrame.TextRange.Font.Size = "10"
oTextBox.TextFrame.TextRange = "Work Order number " & vbCrLf &
vbCrLf & " TESTFY"
Next iPage

objWord.Visible = True

objWord.Quit wdDoNotSaveChanges

Could any one help?

Many thanks in advance
 
L

Luke Skywalker

After running your code, all text boxes and wordarts are created in the
first page. To achieve the goal, I suggest you using the browser object. I
have added the code below for your reference and done a test. It should
work.

For more information about the browser object, refer to the VBA help in
Word.

Hope this helps.

Modified Code
==============
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim oTextBox As Word.Shape
Dim oPageStart As Word.Range
Dim oBrowser As Word.Browser

Set objWord = New Word.Application
objWord.Documents.Open App.Path & "\test.doc"

Set objDoc = objWord.ActiveDocument

sText = "PRINT DATE: " & FormatDateTime(Now, vbShortDate)

iTop = objDoc.PageSetup.PageHeight - 100
ileft = objDoc.PageSetup.LeftMargin
iTotalPages = objDoc.ComputeStatistics(wdStatisticPages)

Set oBrowser = objDoc.Application.Browser

For iPage = 1 To iTotalPages

Set oPageStart = objDoc.GoTo(what:=wdGoToPage, Which:=wdGoToAbsolute, _
Count:=iPage)

iCount = oPageStart.Sections.Count
iTop = oPageStart.PageSetup.PageHeight - 100
ileft = oPageStart.PageSetup.LeftMargin

oPageStart.Document.Shapes.AddTextEffect msoTextEffect2, sText, _
"Arial", 14, msoCTrue, msoFalse, ileft, iTop
Set oTextBox =
oPageStart.Document.Shapes.AddTextbox(msoTextOrientationHorizontal, _
Left:=ileft + 400, Top:=50, Width:=100, Height:=70)

oTextBox.TextFrame.TextRange.Font.Name = "Point Arial"
oTextBox.TextFrame.TextRange.Font.Size = "10"
oTextBox.TextFrame.TextRange = "Work Order number " & vbCrLf & vbCrLf & "
TESTFY"

If iPage <> iTotalPages Then
oBrowser.Target = wdBrowsePage
oBrowser.Next
End If

Next iPage

objWord.Visible = True

objWord.Quit wdDoNotSaveChanges

Regards,
Luke Skywalker
 
Top