WD98: Changing PaperSize Using VBA Causes Run-time ErrorLast reviewed: March 3, 1998Article ID: Q177098 |
The information in this article applies to:
SYMPTOMSWhen 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.
CAUSEThe following sample code causes the error:
ActiveDocument.Sections(1).PageSetup.PaperSize = wdPaperLetter WORKAROUNDMicrosoft 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 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 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/default.aspTo work around this problem, use one of the following methods:
Method 1: Record Setting the Page SizeRecord setting the page size and incorporate the results into your macro. To record a macro:
Method 2: Use the Following Sample Visual Basic ProcedureThe 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 SubNote: 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.
STATUSMicrosoft 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.
REFERENCESFor 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 ComponentFor 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: vba
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |