Share via

VBA Problem

Anonymous
2017-01-31T17:39:42+00:00

I am cleaning up the formatting of a large existing document for its next revision.  I am using VBA to do a lot of the heavy lifting but I am having a problem with setting the overall page layout.  Specifically, the code below:

      With ActiveDocument.PageSetup

            .HeaderDistance = InchestoPoints (0.2)

            .FooterDistance = InchestoPoints (0.2)

      End With

returns the error:

      Run-time Error '4608'

      Value out of range

Now this doesn't happen all the time, just most of the time.  I don't really know what the difference is between the times it does work and the times it doesn't.  So if anyone knows why this would happen, I would really like to know.

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
    2017-02-01T02:05:30+00:00

    Try

    Dim i as Long

    With ActiveDocument

        For i = 1 To .Sections.Count

            With .Sections(i).PageSetup

                .HeaderDistance = InchesToPoints(0.2)

                .FooterDistance = InchesToPoints(0.2)

            End With

        Next i

    End With

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2017-02-02T13:28:40+00:00

    Doug,

    I have had a lot of problems getting the "With Sections" or "With Paragraphs" or "With anything" to work and this helped to solve that unstated problem as well.  

    Thanks,

    Jay

    Was this answer helpful?

    0 comments No comments
  3. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2017-02-01T02:03:56+00:00

    The discussion in this thread may help:

    https://social.msdn.microsoft.com/Forums/en-US/34c01167-369f-4df9-b78e-5d653801824c/word-macro-error-runtime-error-4608-value-out-of-range?forum=worddev

    The first thing to try is looping through each Section in the document, setting the PageSetup values for just that section:

    For Each sec in ActiveDocument.Sections

       With sec.PageSetup

           ' ....

       End With

    Next sec

    Was this answer helpful?

    0 comments No comments