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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 tab paragraph and insert linebreak per page 1

Status
Not open for further replies.

sfchas

Technical User
Nov 15, 2006
3
US
Hello all,

I work with a web application that generates correspondence/letters as a Word document. I'm trying to create a macro that would tab (4") the first paragraph of each page as well as insert line breaks/carriage returns(let's say 10) after that first paragraph.

The document is a random number of pages and each page needs the above formatting. I can easily create a macro that does the above for one page, but I'm having trouble repeating it for each page.

I read some of the earlier posts and think I may be able to execute this with the predefined bookmarks. I would appreciate any suggestions you might have. Thanks!
 
Please describe your requirements more precisely.

Random number of pages: Ok, that is fine.

How are the page breaks done? Page Break, or Section (Next Page) Break?

First paragraph tabbed: Ok, are there other paragraphs on the page?

The 10 line feed/carriage returns: Is there a reason for this? If it is just to add space, then use a Style that will have the space. A Style could also take care of the Tab issue.

What if the 10 new paragraphs (the line feed/carriage returns) pushes the text onto the following page? Is that possible? If it DID, then the next page first paragraph would no longer be the first paragraph.

There are a few ways to go about it, but one way would be:

1. Go to the start of the document
2. Set an integer of the number of page breaks in the document.
3 Make a Range object of the first paragraph (using predefined bookmark "\page").
For i To number of page breaks do:
4. Set LeftIndent for Range object to 4".
5. Insert the 10 line feed/carriage returns
6. Move the Range End to the next page break
7. Collapse the Range
8. Move the Range one character - which moves it the top of next page
9. Expand the Range to the paragraph (first paragraph of the page)

Like this:
Code:
Sub EachPageParagraphOne()
Dim rParagraph As Range
Dim intPagebreaks As Integer
Dim i As Integer
Dim var
Selection.HomeKey Unit:=wdStory
intPagebreaks = ActiveDocument.Range. _
        Information(wdNumberOfPagesInDocument)
Set rParagraph = ActiveDocument. _
    Bookmarks("\page").Range.Paragraphs(1).Range
On Error Resume Next
For var = 1 To intPagebreaks
    With rParagraph
        .ParagraphFormat.LeftIndent = InchesToPoints(4)
        For i = 1 To 10
            rParagraph.InsertAfter Text:=vbCrLf
        Next
        .MoveEndUntil Cset:=Chr(12)[COLOR=red] ' the page break[/color red]
        .Collapse Direction:=wdCollapseEnd
        .Move Unit:=wdCharacter, Count:=1
        .Expand Unit:=wdParagraph
    End With
Next var
End Sub
Now this will work properly IF, and only if, there are page breaks between the pages, AND there is room on the pages for those 10 paragraph marks you are inserting.

If the pages are dynamically made by text - the next page is the next page because text spills over to MAKE that page - then the insertion of those new paragraphs will move things. The first paragraph of the next page will not be the first paragraph anymore.

So it depends. The key is the use of MoveEndUntil on the Range object using Cset = Chr(12) - a page break.

Hope this helps.

Gerry
My paintings and sculpture
 
Wow! Thanks for the well thought-out response. You're right, I should have mentioned the page breaks, and yes, there are page breaks between each letter/page. I'll have to double-check on whether the breaks are Page Breaks or Section (Next Page) Breaks.

The paper that the letter goes on is z-fold 8.5 x 14 form, so a name and address appears at the top (first third, first paragraph; tabs used to place address towards center of letter while still being left-aligned) of the form and the content appears in the remainder (lower two-thirds). So, the line breaks or 10 new paragraphs would be to put distance between the address and the content of the letter. I don't believe I've had a problem with text being pushed onto the next page.

I'm going to work with what you've suggested and see how it goes. Thanks again for the help. - Chas

P. S. Your stone and wood carvings are awesome!

 
Actually, don't worry about whether it is a Page Break or a Section (Next Page) Break. Both are Chr(12). So it doesn't really matter. I was thinking of something else, where it depended on whether there was a Section, or not.

I have to say again that using a bunch of paragraph marks (the line feed/carriage returns) is not a good way to put space. Unless you need the paragraphs to put text into, it is better to use a Style for the space. You could make a Style for the letter contents that has the big space you want as Space Before = 60pts (or something like that).

Glad I could help. And thank you, doing the carvings is fun.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top