Share via


Word macro error - Runtime Error 4608, Value out of range

Question

Monday, March 21, 2016 3:13 PM

We have a macro we run to format the page for our publisher.  There are several documents that use this macro.  For smaller documents the macro runs without error, for larger documents we receive the error in the subject line of this thread.

Small document - <= 256KB

Large document - >= 500KB

For the documents that have the error I can open them in Word and manually make the settings without a problem.

Here is the macro

Sub pagestuffB()
'
' Format for Publisher
'
'
    With Application
        .Options.Pagination = False
        .ScreenUpdating = False
        With .ActiveDocument.PageSetup
            .PageWidth = InchesToPoints(8.5) ' << Runtime Error 4608, Value out of range here
            .PageHeight = InchesToPoints(11)
            .Orientation = wdOrientPortrait
            .LineNumbering.Active = False
            .TopMargin = InchesToPoints(1.34)
            .HeaderDistance = InchesToPoints(0.98)
            .BottomMargin = InchesToPoints(1)
            .FooterDistance = InchesToPoints(0.8)
            .LeftMargin = InchesToPoints(1.61)
            .RightMargin = InchesToPoints(1.4)
            .Gutter = InchesToPoints(0)
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            .SectionStart = wdSectionContinuous
            .OddAndEvenPagesHeaderFooter = True
            .DifferentFirstPageHeaderFooter = True
            .VerticalAlignment = wdAlignVerticalTop
            .SuppressEndnotes = False
            .MirrorMargins = True
            .TwoPagesOnOne = False
            .BookFoldPrinting = False
            .BookFoldRevPrinting = False
            .BookFoldPrintingSheets = 1
            .GutterPos = wdGutterPosLeft
        End With
    End With
End Sub

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

All replies (12)

Wednesday, March 30, 2016 8:04 AM ✅Answered

Hi dbasnett,

With your document, I could get the same error. I think this issue is related with there are multiple sections in your document. And I would agree with you to format each section individually. For long time, it might be related with your large document.

I suggest you try to use OpenXML sdk to format this document to check whether it will spend long time.

>> Insert file would be great if I could just figure out how to preserve the format of the document being inserted.

I found you have post a new thread for this issue, I suggest you keep follow this issue in the new thread.

Best Regards,

Edward

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Monday, March 21, 2016 7:10 PM

To get an idea of the magnitude of the problem I made the following changes to the macro. (I'll post the results when it is done, early results 1 good, 3 bad, 37 to go)

Function pagestuffB() As String
'
' Format for Publisher
'
'
Dim rv As String
rv = ""
On Error GoTo ErrorHndlr:
    With Application
        .Options.Pagination = False
        .ScreenUpdating = False
        With .ActiveDocument.PageSetup
            .PaperSize = wdPaperLetter
'            .PageWidth = InchesToPoints(8.5)
'            .PageHeight = InchesToPoints(11)
            .Orientation = wdOrientPortrait
            .MirrorMargins = True
            .TopMargin = InchesToPoints(1.34)
            .HeaderDistance = InchesToPoints(0.98)
            .BottomMargin = InchesToPoints(1)
            .FooterDistance = InchesToPoints(0.8)
            .LeftMargin = InchesToPoints(1.61)
            .RightMargin = InchesToPoints(1.4)
            .Gutter = InchesToPoints(0)
            .SectionStart = wdSectionContinuous
            .OddAndEvenPagesHeaderFooter = True
            .DifferentFirstPageHeaderFooter = True
            .LineNumbering.Active = False
            
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            .VerticalAlignment = wdAlignVerticalTop
            .SuppressEndnotes = False
            .TwoPagesOnOne = False
            .BookFoldPrinting = False
            .BookFoldRevPrinting = False
            .BookFoldPrintingSheets = 1
            .GutterPos = wdGutterPosLeft
        End With
    End With
    pagestuffB = rv
Exit Function
    
ErrorHndlr:
    If rv = "" Then
        rv = "Macro error " & Err.Number
        Select Case Err.Number
            Case Else
        End Select
    End If
        
    Resume Next
End Function

I have two of the documents, one where the macro ran successfully, one where it didn't.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 12:07 AM

Some of the PageSetup settings can be finicky as to the order in which they're executed when a new parameter isn't valid for the document as defined up to that point in the code's execution. Try:
    .Orientation = wdOrientPortrait
    .PaperSize = wdPaperLetter
and omitting:
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)

Cheers
Paul Edstein
[MS MVP - Word]


Tuesday, March 22, 2016 1:10 AM

Some of the PageSetup settings can be finicky as to the order in which they're executed when a new parameter isn't valid for the document as defined up to that point in the code's execution. Try:
    .Orientation = wdOrientPortrait
    .PaperSize = wdPaperLetter
and omitting:
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)

Cheers
Paul Edstein
[MS MVP - Word]

FWIW - In the second version the Width / Height are commented out.  I will try changing the order, making Orientation first.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 5:57 AM

Hi, dbasnett

>>> For smaller documents the macro runs without error, for larger documents we receive the error in the subject line of this thread.

Small document - <= 256KB

Large document - >= 500KB

>>I have try to execute your macro with word 2013 and 2016.

>>I also use documents that have size of <=256 KB and it is working correctly. I did not get run time error and any other error.

>>I try to execute above macro with word document that size of >=4.5 MB and then also I did not get any run time error or any other error. Document get formatted correctly as per the macro. Here I did not reproduce your issue on my side.

>> As macropod has suggested to make some change in your macro. Did you try that? Did that changes solve your issue?

.Orientation = wdOrientPortrait
    .PaperSize = wdPaperLetter

   ‘ .PageWidth = InchesToPoints(8.5)
   ‘.PageHeight = InchesToPoints(11)

Regards

Deepak

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.


Tuesday, March 22, 2016 11:56 AM

I am not at work, but when I get there I plan on trying that.  Two days spent on this and looking forward to the third.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 1:53 PM

Some of the PageSetup settings can be finicky as to the order in which they're executed when a new parameter isn't valid for the document as defined up to that point in the code's execution. Try:
    .Orientation = wdOrientPortrait
    .PaperSize = wdPaperLetter
and omitting:
    .PageWidth = InchesToPoints(8.5)
    .PageHeight = InchesToPoints(11)

Cheers
Paul Edstein
[MS MVP - Word]

This had no effect other than to change where the error occurred.  It is now on the mirror line.  I think the answer might be to loop through each section and format them individually.  If I were a VBA guru that would've been done before I posted this ;)  Off to read the documentation.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 4:02 PM

The good new is that formatting each section individually looks like it will work.  The bad news is that it may take a month to run.  Here is the big picture:

41 large Word documents (Titles) that I am trying to create.

About 700 chapters in total.  Each of the 41 Titles gets some of the chapters.

About 31,000 paragraphs that are spread out(not evenly) among the chapters.

Here is the macro as it is currently.

Function pagestuffB() As String
'
' Format for Publisher
'
'
Dim rv As String
rv = ""
On Error GoTo ErrorHndlr:
    With Application
        .Options.Pagination = False
        .ScreenUpdating = False
        .WindowState = wdWindowStateMinimize
    End With
    Dim oSec As Section
    For Each oSec In Selection.Sections
        With oSec.PageSetup
            .Orientation = wdOrientPortrait 'moved per macropod
            .PaperSize = wdPaperLetter
            .MirrorMargins = True
            .TopMargin = InchesToPoints(1.34)
            .HeaderDistance = InchesToPoints(0.98)
            .BottomMargin = InchesToPoints(1)
            .FooterDistance = InchesToPoints(0.8)
            .LeftMargin = InchesToPoints(1.61)
            .RightMargin = InchesToPoints(1.4)
            .Gutter = InchesToPoints(0)
            .SectionStart = wdSectionContinuous
            .OddAndEvenPagesHeaderFooter = True
            .DifferentFirstPageHeaderFooter = True
            
            .LineNumbering.Active = False
            .FirstPageTray = wdPrinterDefaultBin
            .OtherPagesTray = wdPrinterDefaultBin
            .VerticalAlignment = wdAlignVerticalTop
            .SuppressEndnotes = False
            .TwoPagesOnOne = False
            .BookFoldPrinting = False
            .BookFoldRevPrinting = False
            .BookFoldPrintingSheets = 1
            .GutterPos = wdGutterPosLeft
        End With
    Next oSec
    
    pagestuffB = rv
Exit Function
    
ErrorHndlr:
    If rv = "" Then
        rv = "Macro error " & Err.Number
        Select Case Err.Number
            Case Else
        End Select
    End If
        
    Resume Next
End Function

Here is the run report for just the first two Titles, some of the smallest.

Tuesday, March 22, 2016  10:28:32 AM

Table of Contents

Title I

<<<<<< Chap. 1
...............................
>>>>>> Chap.  1  done  00:00:17.7

<<<<<< Chap. 2
..........
>>>>>> Chap.  2  done  00:00:30.2

<<<<<< Chap. 3
....................
>>>>>> Chap.  3  done  00:00:54.3

Title done \\TESTSERVER\PubDocs\2016\book\Title_I.docx  at 00:00:54.8  total elapsed.
Number of pages in Title is 19, running total is 19.

Title II

<<<<<< Chap. 7
........
>>>>>> Chap.  7  done  00:00:59.4

<<<<<< Chap. 8
..................................................................................................................................................
>>>>>> Chap.  8  done  00:08:36.9

<<<<<< Chap. 9
.....................................................
>>>>>> Chap.  9  done  00:11:46.3

<<<<<< Chap. 10
............................
>>>>>> Chap.  10  done  00:13:44.3

<<<<<< Chap. 11
..........
>>>>>> Chap.  11  done  00:14:30.4

<<<<<< Chap. 12
............
>>>>>> Chap.  12  done  00:15:26.8

<<<<<< Chap. 14
.......
>>>>>> Chap.  14  done  00:16:01.7

Title done \\TESTSERVER\PubDocs\2016\book\Title_II.docx  at 00:16:03.2  total elapsed.
Number of pages in Title is 84, running total is 103.

End
00:16:03.2
Started Tuesday, March 22, 2016  10:28:32 AM
Ended   Tuesday, March 22, 2016  10:44:36 AM

The '.'s represent each paragraph.

My boss doesn't know about this, yet.  After months of extolling the virtues of Word over WordPerfect I am not looking forward to the conversation I will have to have.

I am going to post Title 1 and 2 unformatted somewhere in the off chance someone can help.  I'll post a link soon.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 4:18 PM

Link for docs, RSMo Unformatted

Here is the run report when the macro is NOT ran.

Tuesday, March 22, 2016  11:09:40 AM

Table of Contents

Title I

<<<<<< Chap. 1
...............................
>>>>>> Chap.  1  done  00:00:08.6

<<<<<< Chap. 2
..........
>>>>>> Chap.  2  done  00:00:11.5

<<<<<< Chap. 3
....................
>>>>>> Chap.  3  done  00:00:16.1

Title done \\TESTSERVER\PubDocs\2016\book\Title_I.docx  at 00:00:16.6  total elapsed.
Number of pages in Title is 64, running total is 64.

Title II

<<<<<< Chap. 7
........
>>>>>> Chap.  7  done  00:00:19.7

<<<<<< Chap. 8
..................................................................................................................................................
>>>>>> Chap.  8  done  00:01:19.2

<<<<<< Chap. 9
.....................................................
>>>>>> Chap.  9  done  00:01:44.3

<<<<<< Chap. 10
............................
>>>>>> Chap.  10  done  00:01:54.9

<<<<<< Chap. 11
..........
>>>>>> Chap.  11  done  00:01:59.3

<<<<<< Chap. 12
............
>>>>>> Chap.  12  done  00:02:04.3

<<<<<< Chap. 14
.......
>>>>>> Chap.  14  done  00:02:07.8

Title done \\TESTSERVER\PubDocs\2016\book\Title_II.docx  at 00:02:09.0  total elapsed.
Number of pages in Title is 277, running total is 341.

End
00:02:09.0
Started Tuesday, March 22, 2016  11:09:40 AM
Ended   Tuesday, March 22, 2016  11:11:49 AM

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Tuesday, March 22, 2016 9:23 PM

As I said, some of the PageSetup settings can be finicky as to the order in which they're executed. What you may need to do when working with multiple documents whose layout you don't know is to first set all margins etc. to 0, then do the paper size & orientation, followed by setting all margins etc. to the values you want. You'll still need to do those in the right order, but such an approach should eliminate the errors.

As for the larger project, this would be far easier if all the documents were based on the same template, with the same page layouts & Styles, etc. Even when they're not, though, Word has other ways of incorporating content from one document into another (e.g. INCLUDETEXT fields and Insert>Text From File, neither of which require any code or VBA's .FormattedText method for replicating content without using copy/paste).

Cheers
Paul Edstein
[MS MVP - Word]


Wednesday, March 23, 2016 10:00 AM

As I said, some of the PageSetup settings can be finicky as to the order in which they're executed. What you may need to do when working with multiple documents whose layout you don't know is to first set all margins etc. to 0, then do the paper size & orientation, followed by setting all margins etc. to the values you want. You'll still need to do those in the right order, but such an approach should eliminate the errors.

As for the larger project, this would be far easier if all the documents were based on the same template, with the same page layouts & Styles, etc. Even when they're not, though, Word has other ways of incorporating content from one document into another (e.g. INCLUDETEXT fields and Insert>Text From File, neither of which require any code or VBA's .FormattedText method for replicating content without using copy/paste).

Cheers
Paul Edstein
[MS MVP - Word]

I will have to check but I thought that all of the documents were from the same template.

Each of the 31,000'ish paragraphs (they are actually called 'Sections' in our world) are stored as XML.  The program that converts them to Word documents has a template with the correct settings.  I verified the documents have the correct settings.

The program that consolidates them does so by creating Chapters by inserting the files that are then added to Titles.  At this point I am hoping that one of the intermediate documents doesn't have the correct page layout.  If everything has the same page layout I may not need the macro at all.

This will be day four of fun with VBA...

Do you know how long it takes for Microsoft to respond to Bug reports?

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Wednesday, March 23, 2016 8:31 PM

Insert file would be great if I could just figure out how to preserve the format of the document being inserted.  I kept looking for the option on insert file that allowed you to keep (or not keep) the format of the file.

Here is another one I can't figure out

                                Dim newPara As Word.Paragraph = docAChap.Paragraphs.Add
                                With newPara
                                    .Range.Text = "yuck" 'The range cannot be deleted. error here
                                    .Range.Font.Bold = 1
                                    .Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
                                End With

Just adding a paragraph to the end...  Geez.

It really feels like MS doesn't want you to use Interop...

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.