Search and Count (Again) - MS Word

Padawan

Active Member
Joined
Apr 9, 2002
Messages
395
Vlad

If you're still out there or anyone else.

I'm still having a little trouble with my project. I need to find existing page numbers and renumber them. Part of the problem is that the word "page" appears on the document in other places than just the headings that I need to change. Also, I need the actual numbers right justified - not simply entered in place. Finally, sometimes there are already numbers in place and sometimes just the word PAGE with no numbers.

I have an if-then that works to insert the numbers. And I think I have figured out how to locate where to insert my numbers. But I need some help with the find/replace and the looping.

The pages are formatted with arrows(?) on each side, so I couldn't figure out how to search for both the arrows AND wildcard numbers. I think this will work.

If anyone can help me to search for the text ">> PAGE", then select the found text AND 8 characters to the right. Then I need to replace that selection with the numbers 1 - (up to around) 6,000. There are alwas 8 spaces between the E in PAGE and the ending << arrows, so I can replace PAGE with 6 spaces then a number is 1-9, 5 spaces then number if 10-99, etc. So for exxample, this is what I need as a finished product:

>> PAGE 6 <<,
>> PAGE 35 <<,
>> PAGE 257 <<,
>> PAGE 2456 <<

(There are always 17 spaces between the first > and the last <)

I've been editing reports all morning, so I hope this post is clear enough to understand.

As always, THANK YOU for all assistance.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi John,
You can try this:
Rich (BB code):
Sub OrderPageNum()
  Dim i As Long
  i = 1
  With ActiveDocument.Content.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .MatchWildcards = True
    .Text = "\> PAGE *\<"
    Do While .Execute( _
              ReplaceWith:="> PAGE " & i & " <", _
              Replace:=wdReplaceOne)
      .Parent.Move Unit:=wdParagraph, Count:=1
      i = i + 1
    Loop
  End With
End Sub
Vlad
 
Last edited:
Upvote 0
Vlad

You're a life saver. I tried using hte backslash for the characters (\>) but couldn't get it to work. I think this will do it, with a little bit of tweaking. I still need a total of 17 spaces between the beginning and ending arrows (< >). I believe this code will do that with a little bit of massaging. I'm posting it here in case it may help anyone else.


If j < 10 Then
.Replacement.Text = ">> PAGE " & j & " <<" '6 spaces
ElseIf j < 100 Then
.Replacement.Text = ">> PAGE " & j & " <<" '5 spaces
ElseIf j < 1000 Then
.Replacement.Text = ">> PAGE " & j & " <<" '4 spaces
Else: .Replacement.Text = ">> PAGE " & j & " <<" '3 spaces
End If

If I have any trouble inserting this into your code I'll post back. It may not be until tomorrow.

A major THANK YOU once again.

John
 
Upvote 0
John,

Instead of this code line:
ReplaceWith:="> PAGE " & i & " <", _

use that one:
ReplaceWith:="> PAGE" & Space(7 - Len(CStr(i))) & i & " <", _

Vlad
 
Upvote 0
Vlad

THANK you so much. I was having trouble getting my if/thens inserted into the code. On top of it, you took my 9 lines of code and reduced it to 1 line.

It works GREAT. I really appreciate this.

John
 
Upvote 0
Happy to help,
Cheers! :beerchug:
 
Upvote 0
Hi Padawan,

Not wanting to be overly critical, but your approach to page numbering is fundamentally flawed. That's because you're ignoring the way apps like Word work.

When Word lays out a document, it interrogates the printer driver to find out what the limitations are of the currently-selected printer. Simply selecting a differrent printer (eg in an networked environment with multiple printers) or changing the printer driver for the same printer (eg when changing Windows versions). Of course, adding/deleting material and/or formatting a document for letter paper and sending it someone who uses A4 (or vice-versa) likewise affects the layout. Any one or more of these scenarios can lead to a situation in which the page numbers typed onto the body of the page will end up on a different page. If you're using hard page breaks to force everything after the page number onto the next page, you could end up with your hard-coded page number at the top of the next page, and nothing else on that page.

It seems to me that you could get the results you're after via some creative field coding in the page footer. The advantage of using field coding in the footer is that it will update the numbering automatically to take account of whatever the printer driver, edits and changes in paper size thow at it.
 
Upvote 0
Macropod

I don't believe I could even consider anyone from this board as overly critical. I am always in awe at the extent everyone helps each other with the posts.

I have no doubt you are 100% correct with your anaylsis of this page numbering routine, the page breaks and a better way to code page breaks through footers. It just doesn't apply to this app.

What I failed to post is the reason I am doing this. My work group receives 20 or 30 reports every month. We actually receive them as text files (.txt). I have the option of using other text editors, but I refer to use Word. Several of these reports are cumulative, so I have to take the current month's reports and "stack it" on top of the previous month's report. We then renumber the pages using the page breaks as referenced in my code. I would be very surprised if they ever matched up to the actual printer page breaks. But these page breaks are already inserted in most of the text files. We also use other objects common to every page as a target to find/replace and insert these pages. The reports are then put through another software that generates a .prf file (Monarch) that when viewed through that report software places the page breaks in the correct places. These reports are then further edited in Excel or sent off as final reports.

Thank you for taking your time to respond to my thread. I sincerely appreciate the feedback and you taking your time to reply to my post.

Best regards,

John
 
Upvote 0
Interestingly, but at answering I assumed that the document has been made by copying of pages from text files with already >> PAGE # << formatted marking and everything that was required is described in a thread.
Seems it was close enough to the real task :)
 
Upvote 0
Hi Padawan,

Fair enough, then. I had gained the (wrong) impression you were working with a Word document, whereas you're actually processing a plain text file.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,400
Messages
6,184,761
Members
453,255
Latest member
excelbit

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top