Text replacing based on a true condition

H

Hari Prasadh

Hi,

I have a very large word document running to around 850 pages (5 MB).

The document has been created by exporting data from some other software
(WinCross - A tabulation software).

Actually the original software provides its own numbering at the Bottom
Middle and Top Right and we dont use the automatic numbering facility
provided by MS Word. The software which provides the numbering excludes the
Table of contents etc. Therefore page 1 embedded in Bottom Middle and Top
Right of the present document is Page 32 as per Word. Basically Word shows
31 pages ahead of the document page numbering logic.

Due to some problem with our software, numbering is not alright for the last
for the last 100 pages or so. Like for page number 775 (as per MS word) the
actual document shows Page 744 but for page 776 (as per MS word) the actual
document shows "Page 1" and for page 780 (as per MS word) the actual
document shows "Page 25". Basically the page numbering is mixed up after
page 775 (as per MS word)

Hence, I want a For Loop kind of macro which goes to all the pages in the
word and checks whether that page contains any instance of "Page X". if yes
then "Page X" is changed to "Page I-31" where X could be any number (1
digit, 2 digit) and I is the Actual page number as per word.

Im giving my crude version here.

Sub ReplaceTextWhichAreActuallyPageNumbers()

For I = 1 to TotalNumberOfPagesAsPerWord
If page(I).Searchfor("Page " &"?") or page(I).Searchfor("Page "
&"??") = True then
Replace("Page " &"?") or ("Page " &"??") = "Page "& I-31
End if
Next I

End Sub

(I program a little with Excel but not conversant with word.)

(I hope I have posted in the right group. Though this is a problem with
numbering, but since I think this requires VB, so I didnt post in the
Word.Numbering group. Also, Iam not as such using word's numbering
feature.Please tell me if I should post somewhere else)

Please guide me.

Thanks a lot,
Hari
India
 
D

Dave Lett

Hi Hari,

You might be making this more complicated than it needs to be. It sounds
like you might have multiple sections in your document and that the section
has its "Start at" property set to "1" and maybe the next section the
property is "25". I'm not real sure. However, you can easily find out by
running the following routine:

Dim oSec As Section
Dim sPgNums As String
For Each oSec In ActiveDocument.Sections
sPgNums = sPgNums & "Section " & oSec.Index & _
vbTab & "starts at page " & _
oSec.Headers(wdHeaderFooterPrimary).PageNumbers.StartingNumber &
vbCrLf
Next oSec
MsgBox sPgNums

If you get something like
....
Section 4 starts at page 1
....
Section 8 starts at page 25

then you can change the Continue from section property to true (as shown
here):

Dim oSec As Section
For Each oSec In ActiveDocument.Sections

oSec.Headers(wdHeaderFooterPrimary).PageNumbers.RestartNumberingAtSection =
False
Next oSec
'''!!!! Given your description, though, you might want to restart number
_after_ your table of contents

Then, if you still want a the Word page number (the one that includes the
TOC) and the Document page number (the one starting after the TOC), then you
can use fields instead of VBA. For example, in each section after the TOC
you could add the nested field { = 31 + {Page} }

HTH,
Dave
 
H

Hari Prasadh

Hi Dave,

Im sorry for writing a verbose and unclear mail.

The fact is that Im not using Word's page numbering feature anywhere in my
document. The other software I mentioned (Wincross) itself creates the page
numbers in its output. I save that output in *.Rtf format. Then I OPEN this
*.Rtf file in MS word for reviewing and if it is fine this *.Rtf file is
used for all purposes. When I say that other software itself creates page
numbers, I mean that I can manually go to Bottom Middle or Top Left and edit
each page number in to something else (If I use MS word's page numbering
feature then manually one cannot edit page numbers.)

What happened is that we were creating a big report in Wincross and it
started giving errors. So we broke up our report in to small chunks and fed
it in to wincross chunk by chunk ( breaking up in to small chunks didnt
create problems). The outputs from each of these chunks were then opened in
Word and they were copy pasted together to have a consolidated report in
Word.

Now, the problem is that since the reports were generated in Wincross
separately so it made TOC and INITIALIZED the page numbers following TOC
for EACH of those chunks. One chunk was of around 744 pages so numbering
till that point is fine. After that due to copy paste of different chunks
all the page numbers after that starts with page 1 for each chunk and ends
with let's say Page 7 or Page 25 depending on how big the chunk is.

Hence, I want to programmatically do a REPLACE TEXT on my consolidated
document.

In this case FIND WHAT is "Page ?" or "Page ??"
And if the above find is true then REPLACE WITH is "Page " & Actual Word
page number - 31

Please tell me if I have been able to express my need in a better manner
this time.

Sorry for the trouble caused to you.

Thanks a lot,
Hari
India

news:%[email protected]...
 
H

Hari Prasadh

Hi Dave,

Forgot to add this

I tried your module
Sub try()
Dim oSec As Section
Dim sPgNums As String
For Each oSec In ActiveDocument.Sections
sPgNums = sPgNums & "Section " & oSec.Index & vbTab & "starts at page "
& oSec.Headers(wdHeaderFooterPrimary).PageNumbers.StartingNumber & vbCrLf
Next oSec
MsgBox sPgNums
End Sub

And I get the message that secion 1 starts at page 1 and then the Macro
Ends.

Thanks a lot,
Hari
India
 
D

Dave Lett

Hi Hari,

<snip> I get the message that secion 1 starts at page 1 and then the Macro
</snip>
Excellent. That means that you have only one section in your Word document.
In this case, instead of using a macro, I recommend that you simply view the
Header/Footer (whereever you want the page number to appear) and insert the
page field. Then move to the end of your TOC, instead a Next Page break,
making sure that you don't Link the Headers to Previous and insert a nested
field { = {Page} - 31 }. If you need more help with
Sections/Headers-Footers/Page Numbering, then I recommend that you spend
some time browsing the pertinent areas of www.word.mvps.org

HTH,
Dave
 
H

Hari Prasadh

Hi David,

I am sorry, actually, Im not using Word's numbering feature and in the
present case due to the nature of the document I cannot/ do not want to use
word's numbering feature.

Let's say in middle of a page there is a text which says " The present page,
which is page 9 gives information regarding water properties" or some other
page might have "The present page, which is page 25 gives information
regarding water's peculiarities".

What I want to do is replace the above page 9 or page 25 with actual word
page number MINUS 31.

So, if the first text appeared in page 86 and second text appeared in page
99 then I want a macro which does a Replace with of the above text and the
final result would like :

" The present page, which is page 55 gives information regarding water
properties"
"The present page, which is page 68 gives information regarding water's
peculiarities"

55 has come from 86-31
68 has come from 99-31.

So I want a for loop which goes through whole of the document and searches
for "Page ?" or "Page ??" and if it is found then replace it with -- "Page "
& X -- where X is the actual word page number minus 31.

I just need the syntax for

a) programmatically finding the pagenumber of a particular page (I will use
this in If condition) and
b) programmatically looping sheet by sheet (will use this in the FOR loop).

Please guide me.

Thanks a lot,
Hari
India
 
H

Hari Prasadh

Hi David,

I got my solution (resorted to Multi-posting and Helmut W helped me out).

Actually now I have come to realize that text which I was trying to replace
was part of *Main Story*.

I hope next time I have an issue, Iam able to use some technical jargon
pertaining to word in order to express my needs in a better manner.

Thnx a TON for your help.

Regards,
Hari
India
 

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