HOWTO: Have a VB Program Change a Word Document's Page Layout

Last reviewed: September 29, 1997
Article ID: Q115782

The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows version 3.0
- Microsoft Word, version 6.0

SUMMARY

The stretch setting of the SizeMode property re-sizes an OLE server's image to best fit the dimensions of the MSOLE2.VBX control. OLE servers, such as Word for Windows, are responsible for the display of the image inside an OLE container. OLE servers will create a display based on a unit defined by the server. For example, the display of a Word for Windows version 6.0 document is based on the Page Layout dimensions of the document and the amount of vertical text being displayed. This article gives two techniques you can use to alter the appearance of a Word object in the OLE control.

MORE INFORMATION

The ChangePageSize Sub procedure defined in the following example is a generic routine that accepts three parameters (the OLE control, a string for page height, and a string for page width). The ChangePageSize Sub procedure redefines the page height and width based on the parameters passed to it. Both strings (page height and page width) must include a unit of measure. The following example uses inches as the unit of measure. The ChangePageSize Sub procedure expects the second and third parameters to represent the page height and page width.

Steps to Create Example Program for Changing a Page Size

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. Add a command button (Command1) to Form1.

  3. Create a control array of three Command1 buttons. From the Edit menu, choose Copy. Then from the Edit menu, choose Paste. You will be asked if you want to create a control array of Command1 buttons. Choose the Yes button. Continue this process for a total of three Command1 buttons. Change the caption on the first button to 100%, the caption on the second button to 75%, and the caption on the third button to 50%.

  4. Add the following code to Command1_Click (Index As Integer) event procedure:

          Sub Command1_Click (Index As Integer)
             ' OLE1 is the name of the OLE control.
             Select Case Index
                Case 0 '100%
                   Call ChangePageSize(OLE1, "", "")
                Case 1 '75%
                   ' Enter the following two lines as one, single line:
                   Call ChangePageSize(OLE1, CStr(11 * .75) & """",
                      CStr(8.5 * .75) & """")
                Case 2 '50%
                   ' Enter the following two lines as one, single line:
                   Call ChangePageSize(OLE1, CStr(11 * .5) & """",
                      CStr(8.5 * .5) & """")
             End Select
          End Sub
    
    

  5. Add the following Sub procedure to the General Declarations section of Form1:

          ' Enter the following two lines as one, single line:
    
          Sub ChangePageSize (cOLE As OLE, strPageHeight As String,
             strPageWidth As String)
             ' Declare the variables to be used:
             Dim WB As Object
             ' Initialize Page Definition variables for default:
             If strPageHeight = "" Then strPageHeight = "11.0"""
             If strPageWidth = "" Then strPageWidth = "8.5"""
             ' Activate OLE container:
             cOLE.Action = 7
             ' Use WB as an Alias for the WordBasic Object:
             Set WB = CreateObject("Word.Basic")
             ' Use OLE Automation to define the embedded documents page def.
             ' Change width and height, ,, will leave settings as is.
             WB.FilePageSetup , , , , , , , strPageWidth, strPageHeight
             ' If object is linked, save, close, and activate Form:
             If cOLE.SourceDoc <> "" Then
                WB.FileSave
                WB.FileClose
                AppActivate Me.Caption
             End If
          End Sub
    
    

  6. Add an MSOLE2.VBX control to Form1 and select the Create from File option. Then choose the Browse button and find the desired document to load. Next select the Display as Icon option, and choose the OK button.

  7. Run the sample. Click the 50% button to see the document displayed at 50% of the page size. Then click the 75% button to see the document displayed at 75% of the page size. Finally, click the 100% button to see the document displayed at 100% of the page size.

Steps to add to the Example Program for Changing the Margin Size

The ChangeMargins Sub procedure listed in the following example is a generic routine that excepts five parameters (the OLE control and four string type parameters for the four margin settings).

The ChangeMargins Sub procedure redefines the page margins (top, bottom, left, and right) based on the parameters passed to it. All four of the margin strings must include a unit of measure, which for the following example is inches. In the ChangeMargins Sub procedure, the second, third, fourth and fifth parameters set the margins. The second parameter represents the value for the top margin, the third represents the value for the bottom margin, the fourth represents the value for the left margin, and the fifth represents the value for the right margin.

  1. Add a command button (Command2) to Form1.

  2. Create a control array of four Command2 buttons. From the Edit menu, choose Copy. Then from the edit menu, choose Paste. When asked if you want to create a control array of Command1 buttons, choose the Yes button. Continue this process for a total of four Command2 buttons. Change the first button's caption to Default, the second button's caption to No Margins, the third button's caption to 1/2 Inch, and the fourth button's caption to 1 Inch.

  3. Add the following code to the Command2_Click (Index As Integer) event procedure:

          Sub Command2_Click (Index As Integer)
             Dim strTop As String, sBottom As String
             Dim sRight As String, sLeft As String
    
             Select Case Index
                Case 0 'Default Settings:
                   ' Do nothing
                Case 1 ' No Margins:
                   strTop = "0.00" & """"
                   sBottom = "0.00" & """"
                   sRight = "0.00" & """"
                   sLeft = "0.00" & """"
                Case 2 ' 1/2 Inch all the way around:
                   strTop = "0.5" & """"
                   sBottom = "0.5" & """"
                   sRight = "0.5" & """"
                   sLeft = "0.5" & """"
                Case 3 ' 1 Inch all the way around:
                   strTop = "1.00" & """"
                   sBottom = "1.00" & """"
                   sRight = "1.00" & """"
                   sLeft = "1.00" & """"
             End Select
             Call ChangeMargins(OLE1, strTop, sBottom, sRight, sLeft)
          End Sub
    
    

  4. Add the following Sub procedure to the General Declarations section of Form1:

          ' Enter the following three lines as one, single line:
    
          Sub ChangeMargins (cOLE As OLE, strTopMargin As String,
             strBottomMargin As String, strLeftMargin As String,
             strRightMargin As String)
             ' Declare the variables to be used:
             Dim WB As Object
    
             ' Initialize Page Definition variables and defaults:
             If strTopMargin = "" Then strTopMargin = "1.0"""
             If strBottomMargin = "" Then strBottomMargin = "1.0"""
             If strLefMargin = "" Then strLefMargin = "0.5"""
             If strRightMargin = "" Then strRightMargin = "0.5"""
    
             cOLE.Action = 7  ' Activate OLE container.
             ' Use WB as an Alias for the WordBasic Object:
             Set WB = CreateObject("Word.Basic")
    
             ' Use OLE Automation to define the embedded documents page def.
             ' Enter the following two lines as one, single line:
             WB.FilePageSetup , , strTopMargin, strBottomMargin,
                strLefMargin, strRightMargin
             If cOLE.SourceDoc <> "" Then
                WB.FileSave
                WB.FileClose
                AppActivate Me.Caption
             End If
          End Sub
    
    

  5. Press the F5 key to run the application. Click the 1 Inch button to see the document displayed with a one-inch margin. Click the 1/2 Inch button to see the document displayed with a one-half-inch margin. Click the No Margins button to see the document displayed with no margin. Click the Default button to use the Microsoft Word default margin setting.

You can use these techniques to produce a zoom or PagePreview effect and control the effect within your Visual Basic program.


Additional query words: OLE Automation OA OLE2 Kbmacro
Keywords : IAPOLE vbwin GnrlVb kbprg
Technology : kbole kbvba
Version : WINDOWS:3.0
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.