I'm trying to calculate the left position of a picture shape in a Word document so that I can center the image on the page. To do that, I need to know the left margin value and the width of the usable page between the left and right margins.
My problem is that when my macro runs, an error occurs because the value of ActiveDocument.PageSetup.PageWidth
is 9999999 (some kind of MaxValue, I assume).
I'm thinking that maybe this has occurred because my document has multiple sections, some of which are 2 or 4 column sections, and some which are landscape sections (so the PageSetup values will change from the normal portrait-only document style).
Does anyone have some advice for:
1) How to properly recover from such a situation in my macro? For example, can I test the values and reset them from the PageSetup values in one of the very first portrait sections of the document?
2) How to manage the document as I write it to avoid making the PageSetup values "undefined" (not the right word) from the values that I original set for the document and for when I manually set the margins for a landscape section?
Thank you.
Some code, for those who are interested. My macro triggers an error on the PageWidth value with something like ".PageWidth = < Object variable or With block variable not set.>" when I mouseover PageWidth in the debugger.
Dim usablewidth As Single
With ActiveDocument.PageSetup
usablewidth = .PageWidth - .leftmargin - .RightMargin
End With