Return page containing word

P

Pete

Hi, I posted a question asking how to search for a word then select/range
the page the word is within. Doug Robbins kindly replied saying I could use
the built-in \Page bookmark. I've tried this and it fails. However - it
does work with other built-in bookmarks like \Para. So maybe this is a
bug - or my code is screwy? It's as if it ignores the page breaks in the
doc entirely. Any ideas would be great... thanks!

Dim pageText As String
Dim oPageRange As Range
Dim oPageBookmark As Bookmark
Dim oSel As Selection

Set oSel = Selection
oSel.Find.ClearFormatting
oSel.Find.ClearAllFuzzyOptions
oSel.Find.MatchCase = False
oSel.Find.Forward = True
oSel.Find.Wrap = wdFindContinue
oSel.Find.Text = "SomeTextToFind"
oSel.Find.Execute

If oSel.Find.Found = True Then
Set oPageBookmark = oSel.Bookmarks.Item("\Page")
Set oPageRange = oPageBookmark.Range
pageText = oPageRange.Text
MsgBox ("Found page: " & pageText)
End If
 
J

Jonathan West

Hi Pete

It might be that Word hasn't had a chance to catch up with where the page
breaks are. Just before the line that references to bookmark, include this

ActiveDocument.Repaginate
 
P

Pete

Wow, yes that sounds very likely in this particular instance. I'll try it
first thing tomorrow.

Thanks for the fast response !
 
P

Pete

Yep - that was it ! Thanks!


Pete said:
Wow, yes that sounds very likely in this particular instance. I'll try it
first thing tomorrow.

Thanks for the fast response !
 

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