PDA

View Full Version : Page margins change



jwise
06-06-2012, 02:43 PM
I developed an Excel macro a few years ago which used Word 2003 to print a summary sheet for each data row in Excel. This summary sheet was a Word template which was printed then deleted. The loop acquired the template again until the rows were exhausted.

This logic works fine for a limited number of rows, but creates havoc with large numbers of rows. I decided to modify this code to produce a single Word file which I could "send" to Adobe to create a PDF where I could limit the number of pages I printed at one session.

When I do this, the document no longer formats correctly. If you compare the two pages side by side, the new version uses larger page margins (indentation is increased) causing the document to overflow its one page per row design. Both macros use the exact same template.

Here are code snippets:
' Original version
Set wrdApp = CreateObject("Word.Application")
For i = 3, lastRow
Set wrdDoc = wrdApp.Documents.Open(Filename:=C:\recap.dot")
' Process bookmarks
wrdApp.Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=False, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
Set wrdDoc = Nothing
Set rngDoc = Nothing
wrdApp.ActiveDocument.Close SaveChanges:=False
Next i

'================New Snippet=========

Set wrdApp = CreateObject("Word.Application")
wrdApp.Documents.Add
For i = 3, lastRow
rngDoc.InsertFile Filename:="C:\recap.dot"
'Do bookmark processing
rngDoc.InsertBreak Type:=wdPageBreak
next i
wrdApp.Application.PrintOut FileName:="", Range:=wdPrintAllDocument, Item:= _
wdPrintDocumentContent, Copies:=1, Pages:="", PageType:=wdPrintAllPages, _
Collate:=True, Background:=False, PrintToFile:=False, PrintZoomColumn:=0, _
PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0

Set rngDoc = Nothing
Set wrdDoc = Nothing
wrdApp.Quit SaveChanges:=False
Set wrdApp = Nothing


The fault again is that the document when printed with the original version matches the 8.5x11 page perfectly, but the second version (which uses the same template) uses larger indentation, meaning some lines in the template no longer fit on a single line. The document doesn't fit on a single page anymore.

I admit to some experience with Excel macros and very limited exposure to Word

jwise
06-06-2012, 05:36 PM
I've narrowed this problem and attempted a fix. By changing the Word to visible, I was able to verify the document indeed had changed its margins. Normally this document has 1 inch right and left, but for unknown reasons, it was using 1.25 inches on both sides.

I attempted to use:

wrdApp.ActiveDocument.RightIndent = 72
wrdApp.ActiveDocument.LeftIndent = 72
but this gets runtime errors. I think I need something else after the "ActiveDocument" and before the "Right(Left)Indent".

Any ideas? (72 is points is 1 inch)

Tinbendr
06-07-2012, 03:59 AM
wrdApp.ActiveDocument.PageSetup.RightMargin = 72

Frosty
06-07-2012, 05:59 AM
In general, the best way to learn this kind of stuff is to simply record a macro in Word which does the action you want, and then see if you can find what you're looking for in that recording.