Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert page break above every string in MS Word

Status
Not open for further replies.

BeckyMack

Programmer
Apr 15, 2008
5
US
I'm in MS Word VBA. I'm looping through documents and folders to open a Word file and then apply formatting (margins etc). This all works fine. But the documents are disorganized downloads from a mainframe where the headers for sections are halfway between pages etc.

I'd like to be able to loop through the document, search for a specific string and then insert a page break above that string each time it's found. I can get VBA to insert 1 string at the top of the document but I can not get VBA to loop the entire document and continue on even though I've used variations of the find command. Any suggestions?

Becky
 



Hi,

It is the CURSE of reports that get imported into Word or Excel -- headings that get repeated of each "PAGE".

Best thing to do is put the heading into the HEADING, and delete all other occurrences of the heading. The mainframes definition of "PAGE" is very different than a word processor's page.

I almost ALWAYS use Excel.

FIRST I number all the rows, so I can put things back into the original sequence.

Then I SORT to gather ALL the nutty heading information together, so I can make ONE row of headings and DELETE everything else, including EMPTY ROWS.

Then I sort back into order and VOLA!

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
I'll run it by management but our clients are very set in their ways and I'm not sure they'll go for that since these were Word Perfect reports that I'm converting. If there are any other options I'd love to hear about it. It's 150 reports all going to different users so that requires a lot of agreement!
 



Use Find/Replace

Find on the STRING that defines the FIRST ROW of the heading.

Replace with
[tt]
^P
[/tt]
and then paste the string.

So every heading first row will be perceeded with a ^p (PARAGRAPH)

Macro record to automate.

Skip,

[glasses]Have you heard that the roundest knight at King Arthur's round table was...
Sir Cumference![tongue]
 
I don't see my last post that said thank you for all the help. I'm not sure what happened to it but that did help. I also stopped connecting to word.application and connected to word.document.8 and that appears to be working better.

I have one final question that will hopefully finish off this project. If I get it all working I'll post it!

I used a findtext to get to a specific string in the activedocument. I did a little tweaking to the text to put it back. But now I want to activate the previous page and I can't for the life of me figure out how even with the pages collection.

How can I move to the previous page from the active page regardless of what the page number is? (dynamically).

Becky
 
Hi becky, you can determine the current page number with code like:
Code:
Selection.Information(wdActiveEndPageNumber)
From that you can go to the previous page with code like:
Code:
ThisPage = Selection.Information(wdActiveEndPageNumber)
Selection.GoTo What:=wdGoToPage, Name:=ThisPage - 1
You'll need error checking in case you're already on Page 1!

Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top