HOWTO: Have a VB Program Change a Word Document's Page Layout |
Q115782
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.
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.
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
' 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
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
' 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 Additional query words: OLE Automation OA OLE2
Keywords : kbmacro kbVBp300 kbDSupport IAPOLE
Issue type : kbhowto
Technology :
|
Last Reviewed: October 14, 1999 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |