3

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

3 Answers 3

6

The value 999999(undefined) occurs when the object you query has more than one possible value. As you describe, this is the case when a document has more than one section with differing page widths.

To solve this, query the Section properties, rather than the document's properties. For example, to get the page width of the first section:

ActiveDocument.Sections(1).PageWidth

That said, it should be possible to set an image's relative horizontal position to be centered on the page... Simplest would be to use an InlineShape and set it's Range.ParagraphAlignment to centered.

5
  • Thank you for explaining the 999999; it was very helpful for understanding. Wow, that's an excellent idea about setting the ParagraphAlignment property to centered - and simple too! (But it didn't work for me because my shapes are not inline). I did access the current section number to get the Sections(1).PageWidth value as you described. It worked! Thank you!
    – Kevin
    Commented Sep 24, 2018 at 14:53
  • Glad you've worked it out @Kevin. I wasn't at a machine where I could check what I was suggesting about the positioning and mis-remembered what's possible for a Shape, so I removed that part of my Answer. - just FYI :-) Commented Sep 24, 2018 at 15:16
  • not to worry - Your kind answer got me where I wanted to go, and I appreciate it! I don't know how anyone can ever learn all of these details! :-)
    – Kevin
    Commented Sep 24, 2018 at 22:54
  • 2
    Almost 30 years of working with the software... and helping others with problems :-) Commented Sep 25, 2018 at 5:04
  • Here I am again on this issue 3 years later, and find that I asked a variation three years ago. Thank you again for your help. I learned somewhere along the line to use the page size to do my centering because the page size is always fixed regardless of sections. (This assumes that page-center is the goal, not center-within-section-margins).
    – Kevin
    Commented Jul 24, 2021 at 18:25
1

Just in case someone wonders how to always get for example the page width at the cursor location if you have multiple sections in the document with different page widths:

Instead of

ActiveDocument.PageSetup.pageWidth

use

Selection.Sections.PageSetup.pageWidth

0

Here's my weard work around:

Sub myTableCreatingCode()
'My long code
'
'
'
'In the end, call the sub "setAutoFit"
Call setAutoFit
End Sub

Private Sub setAutoFit()
Selection.Tables(1).AutoFitBehavior wdAutoFitWindow
End Sub

I can't explain why it works, but it do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.