Microsoft KB Archive/177098

= WD98: Changing PaperSize Using VBA Causes Run-time Error =

Article ID: 177098

Article Last Modified on 6/17/2005

-

APPLIES TO


 * Microsoft Word 98 for Macintosh

-



This article was previously published under Q177098



SYMPTOMS
When you try to set the page size of a document using the PaperSize property in Microsoft Visual Basic for Applications code, you receive the following error message:

Run-time error '5861':

'

where represents a graphical box.



CAUSE
The following sample code causes the error: ActiveDocument.Sections(1).PageSetup.PaperSize = wdPaperLetter



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. To work around this problem, use one of the following methods:

Method 1: Record Setting the Page Size
Record setting the page size and incorporate the results into your macro.

To record a macro:
 * 1) On the Tools menu, point to Macro, and then click Record New Macro.
 * 2) In the Macro name box, type a name for the macro.
 * 3) In the Store Macro In box, click the template or document in which you want to store the macro.
 * 4) In the Description box, type a description for the macro.
 * 5) If you do not want to assign the macro to a toolbar, a menu, or shortcut keys, click OK to begin recording the macro.

Method 2: Use the Following Sample Visual Basic Procedure
The following sample Visual Basic code sets the page size for all pages within the first section of a document to Legal. If your document contains only one section, this example will work as shown. Sub SetPageSize

' If there are no open documents, exit this procedure. If Application.Documents.Count = 0 Then Exit Sub

' Set Page size for the first section. With ActiveDocument.Sections(1).PageSetup

' Set page orientation to portrait. .Orientation = wdOrientPortrait

' Set page size to Legal. .PageWidth = InchesToPoints(8.5) .PageHeight = InchesToPoints(14)

End With

End Sub

Note: You will want to set margins or other attributes applicable to the page size. In this case, using Method 1 (found earlier in this article) would provide a much simpler approach to work around this problem.



STATUS
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

