Looping through pages and sections?

M

ML

Is it possible to loop through each section and each page of a document in
VBA macro? I want to walk through each page, look for specific markers and
set a variable based on this if possible.

Is there a pages collection that an be looped that give the section and page
number and the contents on that page?
 
G

Greg

ML,

If you are using Word2003 you might have a look at "Panes" and
"Rectangles" in the VBA help.
 
G

Greg Maxey

ML,

Here is an example of using the Pane and Rectangle to set a range to the
text of a page. According to Word Help this is not the intended purpose of
these methods, but might get you what you need:

Sub ScrathMacro()

Dim MyPane As Pane
Dim MyPage As Page
Dim MyRect As Rectangle
Dim PageCount As Long
Dim RectCount As Long
Dim i As Long
Dim j As Long
Dim myRng As Range

Set MyPane = ActiveWindow.ActivePane
PageCount = MyPane.Pages.Count
For i = 1 To PageCount
Set MyPage = MyPane.Pages(i)
RectCount = MyPage.Rectangles.Count
Set MyRect = MyPage.Rectangles(1)
For j = 1 To RectCount
Set MyRect = MyPage.Rectangles(j)
If MyRect.RectangleType = wdTextRectangle Then
Set myRng = MyRect.Range
With myRng.Find
.Text = "Test"
.Replacement.Text = "Found"
.Execute Replace:=wdReplaceAll
End With
Set myRng = Nothing
End If
Next
Next
End Sub
 
K

kiln

Is it possible to loop through each section and each page of a document in
VBA macro? I want to walk through each page, look for specific markers and
set a variable based on this if possible.

Is there a pages collection that an be looped that give the section and page
number and the contents on that page?
I'm not at all good with Word programming but below is an adaptation of
some code that I found on this ng that might be something of starting
point for you (note it's a reduction of some working code, does compile
but isn't tested)

Public Function KillAllImages()
' The following code will delete Shapes and InlineShapes throughout
the Document.
Dim rngStory As Range
Dim shpShape As shape
Dim shpInline As InlineShape
Dim hdrHeader As HeaderFooter
Dim ftrFooter As HeaderFooter
Dim i As Integer

With ActiveDocument
'Delete InlineShapes from all Stories
For Each rngStory In .StoryRanges
For Each shpInline In rngStory.InlineShapes
shpInline.Delete
Next shpInline
Next rngStory
'Shapes only supported in headers/footers/body
For Each shpShape In .Shapes
shpShape.Delete
Next shpShape
For i = 1 To .Sections.Count
For Each hdrHeader In .Sections(i).Headers
For Each shpShape In hdrHeader.Shapes
shpShape.Delete
Next shpShape
Next hdrHeader
Next i
For i = 1 To .Sections.Count
For Each ftrFooter In .Sections(i).Headers
For Each shpShape In ftrFooter.Shapes
shpShape.Delete
Next shpShape
Next ftrFooter
Next i
End With
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top