|
-
May 4th, 2011, 04:05 AM
#1
Thread Starter
New Member
Using the find method to find a style and recording the page it was found on
Hi,
I am trying to write a macro that will search through the active document, find all instances of a style, then print out the text and the page number the text is on. I am using the following code snippet to test this:
vb Code:
With ActiveDocument.Content.Find
.ClearFormatting
.Style = "Table Title"
'The Do...Loop statement repeats a series of
' actions each time this style is found.
Do While .Execute(Forward:=True, Format:=True) = True
strCode = .Parent & vbTab & Selection.Information(wdActiveEndAdjustedPageNumber)
MsgBox strCode
.Parent.Move Unit:=wdParagraph, Count:=1
Loop
End With
Which seems to work OK, however, since the Content returns a range object, it does not cause the selection to change and hence the page number reported is always the LAST page of the document, rather than the page on which the text was found (this text is from the help on the find property: Note When this property is used with a Selection object, the selection is changed if the find operation is successful. If this property is used with a Range object, the selection isn't changed unless the Select method is applied.
So what I want to know is how to "select" this line such that the page number reports correctly.
thanks in advance!
-
May 4th, 2011, 08:18 PM
#2
Re: Using the find method to find a style and recording the page it was found on
you could try like
vb Code:
.Parent.select strCode = .Parent & vbTab & Selection.Information(wdActiveEndAdjustedPageNumber)
i have not tested, you also might want to set application.screenupdating to false, when changing the selection
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 9th, 2011, 08:27 PM
#3
Thread Starter
New Member
Re: Using the find method to find a style and recording the page it was found on
Hi,
Thanks for the reply.
I had tried this, but what happens is the entire document is selected and then the page number reported is the end of the selection, i.e. the last page of the document - this is exactly what happens when nothing is selected as well.
I think I may need to move the cursor, or more specifically move the selection to the result of the find operation. Essentially, I am trying to make the range object 'Content' behave like a selection object would in regard to the find method. (well according to the ms help snippet I posted in my previous post).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|