How to get current page number

V

vasuki.ramakrishna

using vbscript I am trying to serach for a specific text in a word
file and if the text is found it will highlighted. But i also want to
know the page number where the text is found.

I am using the wdActiveEndPageNumber object to acheive this But for
some reason it is returing the value "1" even if the text is found at
5th page. Can anyone please help me with this

currentPageNumber = objSelection.Information(wdActiveEndPageNumber)
 
D

Doug Robbins - Word MVP on news.microsoft.com

We would need to know what objSelection is to help. My guess is that it is
an area on Page 1 of the document.

Show us all of your code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
V

vasuki.ramakrishna

Thanks for the response. Below is the complete Code.

Const wdReplaceAll = 2
Const wdActiveEndPageNumber= 3
Const wdStatisticPages = 2


Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open("C:\Script\new.doc")
Set objSelection = objWord.Selection
arrWords = Array("Vasuki", "Ganesh")

intPages = intPages + objDoc.ComputeStatistics(wdStatisticPages)
WScript.Echo "Total No of pages is :" &intPages


For Each strWord in arrWords
currentPageNumber = objSelection.Information(wdActiveEndPageNumber)
objSelection.Find.Text = strWord
objSelection.Find.Forward = TRUE
objSelection.Find.MatchWholeWord = TRUE
objSelection.Find.Replacement.Highlight = TRUE
objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll
WScript.Echo "Page No :" &currentPageNumber
Next

objDoc.Save
objWord.Quit
 
D

Doug Robbins - Word MVP on news.microsoft.com

Use

WScript.Echo "Page No : " & objSelection.Information(wdActiveEndPageNumber)

Nothing in the code following

currentPageNumber = objSelection.Information(wdActiveEndPageNumber)

would cause the currentPageNumber to be updated with the page number on
which the word is located

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
V

vasuki.ramakrishna

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.
 
V

vasuki.ramakrishna

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.
 
V

vasuki.ramakrishna

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.
 
V

vasuki.ramakrishna

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.
 
V

vasuki.ramakrishna

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.
 
A

Art H

Thanks. I made the chnages as per your advise but still it shows the
value as "1" even though the word found is at page 3.

Doesn't wdReplaceAll in the Find.Execute line cause the insertion
point to return to the beginning of the document? or, at least, does
not cause the insertion point to move?

Do a replace all (Ctrl+H) manually within a Word document.
 
D

Doug Robbins - Word MVP on news.microsoft.com

Here is the code as I used it:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Dim frange As Range
Dim i As Long
i = 1
With Selection.Find
Do While .Execute(FindText:="Mayor", Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
MsgBox Selection.Information(wdActiveEndPageNumber)
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
V

vasuki.ramakrishna

I am completely lost. tried all the posibilities but cannot figure out
where is the issue. Can anyone please help me where i am wrong. Any
help will be greatly appreciated
 
G

Graham Mayor

In order to ascertain the page number at a given location, the selection
must actually be at the location in question before you ask of it the page
number. In order to do that you must record the selection during the search
routine eg

While .Execute = True
With Selection.Range 'the found text
MsgBox .Information(wdActiveEndPageNumber)
'do whatever else you want to the found text e.g.
'.Style = "Heading 1"
'.Font.Size = "18"
End With
Wend


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP on news.microsoft.com

From what application are you trying to run this code?

Why don't you just do it from Word itself?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
V

vasuki.ramakrishna

I am trying to do this rom VBscript. The reason being is i need to
search lot of word files for a particular text and if tha match is
found in the word document then the output should be written to a text
file along with the page number where the match is found.
 
D

Doug Robbins - Word MVP on news.microsoft.com

That can be done just as well from Word using VBA.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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