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

Word VBA - How to change the margin if a second page is created

Status
Not open for further replies.

tolanc

IS-IT--Management
Apr 29, 2003
10
GB
Dear all

Does anyone have any ideas on how I can change the margin on a second page (and subsequent pages) but only if a second page is created....

I've created macros which take user input and places that data onto a page (letter). Now this works fine, but now the firm is rebranding and has decided to put logos at the bottom of the first page. Therefore I need the template of the first page to be 2 inches (no problem). However I don't want to create a second page and adjust the margin back to 1 inch because this would be a waste of paper when printing.

So my question is this, if I have a single page with a bottom margin of 2 inches how do I automate the process of the 2nd, and subsequent pages changing to 1 inch (but only if the user types enough text to create a 2nd page document).

Hope this makes sense! Any help gratefully appreciated!

Tolan
 
Hi,

I don't know if this will completely work for you but this should be a start. This code will look for the number of pages you have in your document. If its greater then 1 then it'll set the margin for page 2+ as 1" and leave the first page as 2".

The issue that I can see is that you'll have to run it after you finish creating or editing the document. This may cause a problem because the margins will adjust the format of the text within the document. I normally don't work with VBA in word but I thought I'd give it a shot.

Good luck.

Sub chgmargin()
Dim pagenum As Integer
With ActiveDocument.PageSetup
.TopMargin = InchesToPoints(2)
.BottomMargin = InchesToPoints(2)
End With
If ActiveDocument.BuiltInDocumentProperties(14) > 1 Then
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
End With
With ActiveDocument.Range(Start:=Selection.Start, End:=ActiveDocument. _
Content.End).PageSetup
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1)
End With
Else
End If
End Sub
 
Hi

Many thanks for your help. As you point out the main problem is getting the macro to automatically fire when a second page is created .....

I have some printing macros which deals with the paper trays so I've decided to attack the problem this way. When a user clicks on the printing macro that will then fire the check for the number of pages and whether two adjust the margins or not.

Any ideas gratefully received.

Regards

Tolan
 
Tolan,

Your use of the printing macro may end up wasting paper if the user gets the item after it adjusted the margins and prints it. What if the user doesn't like the way it looks after they get it from the printer?

An option to think about is to provide them with a print preview after it adjusts the margins and before it actually prints the item.

 
Hi,

I don't think you really want to change the margins for the letter. I recommend you turn on different first page in the document (found under file, page setup, layout.)

Then put a page break in the template while you're creating it. In the First Page Header/Footer put the required logos. If they're pre-printed, put empty hard returns in the header/footer to take up the amount of space needed so text typed in the document won't overlap the letterhead.

You can also use paragraph indents to make text in the header/footer wider than the text margins. For example the left/right indents in my business letterhead are -.5 so it's 1/2 inch wider than my 1" margins.

With the page break in the template you can insert any text desired in the header/footer on the 2nd page. This will affect all pages EXCEPT the first. You can also go to File/Page setup/paper source and set printer trays (assuming everyone uses the same type printers.).

When you're done, delete the page break. Any header/footer you created for page 2 on will magically appear if the user types more than one page. The Margins will be consistent throughout the document, but the first page will APPEAR to have different margins due to the text/returns you put in the header and/or footer.

SORRY SO VERBOSE! But, if you got this far, I'm sure you get the picture now! Good luck to you!

 
That's great, thanks for your help Laura, kphu.

Much appreciated.

Tolan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top