Visual Studio Development Bookmark and Share   
 index > Visual Studio Tools for Office > How to read / set the margins of a Word document
 

How to read / set the margins of a Word document

OK - I fumbled around, and can create my individual docs using the Select.Goto.

The next challenge is that the original doc has 1" margins, and the new individual docs are created with 1.25" margins, so I select 3 pages, but create a 4-page new doc.

How can I read the original margin settings and set the new doc to match?

Thanks,

Richard Atwater

RMAtwater  Monday, March 03, 2008 6:09 PM

Margin settings are a property of the Document.PageSetup object. So...

Dim mrgBottom as Single = docOriginal.PageSetup.BottomMargin

docNew.PageSetup.BottomMargin = mrgBottom

docNew.PageSetup.LeftMargin = docOriginal.PageSetup.LeftMargin

Cindy Meister  Tuesday, March 04, 2008 3:23 PM

Margin settings are a property of the Document.PageSetup object. So...

Dim mrgBottom as Single = docOriginal.PageSetup.BottomMargin

docNew.PageSetup.BottomMargin = mrgBottom

docNew.PageSetup.LeftMargin = docOriginal.PageSetup.LeftMargin

Cindy Meister  Tuesday, March 04, 2008 3:23 PM

Cindy -

Your previous answer was very helpful, and I finished the application. However, . . .

I have run across a problem now, where the original document I am reading from shows nonsense values for the PageSetup values.

If I open the file in Word, it shows margins of 1" right and left, 0.8" top, and 0.5" bottom.

When I read docOriginal.PageSetup, all the margins are set to 9999999.0.

docOriginal is an 804 page output from a mailmerge (3 pages * 268 records).

While I was developing, I cut/pasted the first 9 pages, and the last 12 pages into test documents, which did not have the problem (showed good PageSetup values).

Testing today, I deleted all but the last 30 pages, and it worked as expected.

I can hard-code the margins, but would much rather read them from the original file.

Do you have any suggestions?

TIA,

Richard Atwater

RMAtwater  Tuesday, May 06, 2008 2:53 PM
RMAtwater wrote:

I have run across a problem now, where the original document I am reading from shows nonsense values for the PageSetup values.

If I open the file in Word, it shows margins of 1" right and left, 0.8" top, and 0.5" bottom.

When I read docOriginal.PageSetup, all the margins are set to 9999999.0.

docOriginal is an 804 page output from a mailmerge (3 pages * 268 records).

It's always best to use the PageSetup property with the SECTION object, not the Document object. Because the margins (and some other things) can be set differently for each section of a document. If a document contains more than one section with differing settingsand/or it's very long, querying on the entire document will return the value "indeterminate" (9999999).

As Mail Merge uses a section break between the result for each mail merge record, the document in question here has (at least) 268 sections. And 800+ pages is also quite long. So it's not surprising Word is telling you, "<yawn>I dunno."

If you can be sure that all sections have the same margins (a fairly safe bet, in this case) then use

docOriginal.Sections(1).PageSetup...

Cindy Meister  Tuesday, May 06, 2008 3:43 PM

You can use google to search for other answers

Custom Search

More Threads

• VSTO Tools Add In, Outlook 2003
• Recovering the bounding box of a character
• How to install Office 2007 + VSTO2 + VSTO SE
• How to send user defined properties over mail.
• Cannot attach VSTO Customization via Custom Properties when doc has "No Changes" Protection turned on
• Save only External File attachments
• How to access controls inserted in a word document template( .dot ).
• Fixed panel at left in a excel-worksheet
• Excel - Problems adjusting column width
• Invalid file name when inserting image into Word document