WD98: Changing PaperSize Using VBA Causes Run-time Error

ID: Q177098

The information in this article applies to:

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':

   '<box>

where <box> 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, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

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.

REFERENCES

For more information about the PageSetup property, click the Office Assistant in the Visual Basic Editor, type "PageSetup," click Search, and then click to view "PageSetup Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Word Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office
               Program or Component

For more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications

Additional query words: wordcon vba
Keywords          : kbcode kbprg macword98 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbbug

Last Reviewed: April 5, 1999