Syntax Blues - A repost

H

Hari Prasadh

Hi,

I messed up in explaining my requirement in my previous post ("text
replacing based on a true condition") in
the --microsoft.public.word.vba.beginners -- hence rewording the same here.
(am not conversant with word other than manual formatting and using it as a
Scratch application. I work primarily with Excel)

a) Whats' the syntax for programmatically knowing the actual page number of
a document. Like if the cursor is in page 65 , I should be able to get the
value 65.
I tried the following
Msgbox activedocument.selection.pagenumber
but getting a Run-time error "438" - object doesnt support this property or
method.

b) Whats the syntax for programmatically looping through each sheet in the
document. Like I can write the following
For I = 1 to NoOfPagesInTheDocument

But, how would I find the value of NoOfPagesInTheDocument.

Basically I want the value for total no of pages in that document. This
value also gets displayed in the Word taskbar/information area in the
bottom. Like where it says 45/750, which means am at the 45th page and 750
is the NoOfPagesInTheDocument.

c) Whats the syntax for restricting the Search/Find/replace Feature to a
particular page only. Like , if am in a For Loop as described in b) and if I
want to restrict my find/replace only to the present page, then how do I do
it?

Please guide me.

Thanks a lot,
Hari
India
 
H

Helmut Weber

Hi,
Selection.Information(wdNumberOfPagesInDocument)
There are other ways, but some of them
don't work if a document was opened and still
is in normal view, here and now using Word XP.

Sample code:

Dim lPgs As Long
lPgs = Selection.Information(wdNumberOfPagesInDocument)
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=100 ' page 100
Selection.Bookmarks("\page").Select
With Selection.Find
' ...
End With

Plus

Selection.Information(wdActiveEndPageNumber)
Selection.Information(wdActiveEndAdjustedPageNumber)

for the end of the selection.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
H

Hari Prasadh

Hi Helmut,

Thnx a lot for our help. Im able to get the number of pages.

a) One doubt is in the help if I type
Selection.Information(wdNumberOfPagesInDocument) or if i type
dNumberOfPagesInDocument it does not display anything. Why is this not
undocumented in Help. Please tell me where I can find about the same

b) I put the FOR loop in your code and want one additional help in Syntax.

What am doing is if a particular -- wdActiveEndPageNumber -- has a text of
the form "Page ??" or "Page ?" where ? indicates a single digit number, then
I am replacing that text with "page " & i - 31.

In this I need help in .Text = "page ??". Because in the present form it is
searching for *question marks* rather than a number. How to specify
programmatically that ? is a WILDCARD character.

Sub secondtry()

Dim lPgs As Long
Dim i As Integer

lPgs = Selection.Information(wdNumberOfPagesInDocument)

For i = 1 To lPgs
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=i
Selection.Bookmarks("\page").Select

With Selection.Find
.Text = "page ??"
.Replacement.Text = "page " & i - 31
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Next i

'Plus

'Selection.Information (wdActiveEndPageNumber)
'Selection.Information (wdActiveEndAdjustedPageNumber)

'for the end of the selection.

End Sub


Regards,
Hari
India
 
H

Helmut Weber

Hi Hari,
have a look at this one:
There may be complications,
if the text to add would not fit on the page,
that is currently processed. Unlikely, though.
And, it is assumed, that we are not talking about
headers or footers, but about "page xx" in the main story.

Sub Secondtry()
ResetSearch
Dim lPgs As Long
Dim lTmp As Long
lPgs = Selection.Information(wdNumberOfPagesInDocument)
For lTmp = 1 To lPgs
Selection.GoTo _
what:=wdGoToPage, _
which:=wdGoToAbsolute, _
Count:=lTmp
Selection.Bookmarks("\page").Select
With Selection.Find
.Text = "page [0-9]{1,2}"
.Replacement.Text = "page " & lTmp & " - 31"
.Wrap = wdFindStop
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Next
ResetSearch
End Sub

'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more
.Execute
End With
End Sub

There may be other and faster ways, which may have
to be considered, if necessary, once you have a working
solution.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Hari Prasadh

Hi Helmut,

Dont know how to express you my Immense Gratitude. Not because the macro is
working perfectly (it works like a CHARM), BUT more so, you have taught me
that what is typed by a user on a word document is referred to as MAIN
STORY. Since, I didnt know that I couldnt express my requirement properly in
my earlier post to another word group. For many this might be a very basic
stuff, but because of limiting myself to using Word as a Scratch
application, this has been a revelation.

One minor change I made to your macro -- .Replacement.Text = "Page " &
lTmp - 31 --

(Actually am generating page numbers in the Main Story itself and all this
is being done using some other application. That application's ouput is then
opened in word format. Im not and dont want to use Word's inbuilt Header and
Footer feature).

Thanks a lot,
Hari
India
 
H

Helmut Weber

Hi Hari,

Helping makes at least two people feel better.

Have a nive day!

Roger, end and over, see you next time.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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