Class
WordApplication
Description
Used to automate Microsoft Word. Supported on the Windows platform only. You will need to copy the MSOfficeAutomation plugin (located in the Extras folder of the installation) to the Plugins folder before you can use this class.
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
copy As OLEObject |
|||
ProgramID As String |
|||
NameOfFunction As String |
|||
PropertyName As String |
|||
Events
Name |
Parameters |
Returns |
---|---|---|
Property descriptions
WordApplication.Handle
Handle As Ptr
Returns a pointer to the IDispatch interface that is being used.
Method descriptions
WordApplication.Constructor
Constructor(copy As OLEObject)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a copy of the OLEObject.
WordApplication.Constructor
Constructor(ProgramID As String)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a new OLEObject using the passed ProgramID is the COM server's program ID as stored in the registry. It can also be the Class ID (in curly braces). This constructor will try to find a previous instance of the COM server if it is running. Otherwise, it will create a new instance.
WordApplication.Constructor
Constructor(ProgramID As String, NewInstance As Boolean)
Note
Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.
Creates a new OLEObject using the passed ProgramID is the COM server's program ID as stored in the registry. The NewInstance parameter specifies whether to create a new instance of the COM server (True) or try to use an existing one if it is running (False).
The following example automates Internet Explorer.
Try
Var obj As OLEObject
Var v As Variant
Var params(1) As Variant
obj = New OLEObject("InternetExplorer.Application", True)
obj.Value("Visible") = True
params(1) = "httsp://www.xojo.com"
v = obj.Invoke("Navigate", params)
Catch err As OLEException
MessageBox(err.Message)
End Try
WordApplication.Invoke
Invoke(NameOfFunction As String) As Variant
Invokes a method of the COM server, and passes the array of parameters to the method.
Make sure to correctly dimension the array, as this will determine the number of parameters that get passed to the method. The first parameter begins at 1.
WordApplication.TypeName
TypeName As String
Returns a String that provides Variant subtype information about the object.
WordApplication.Value
Value(PropertyName As String) As Variant
Used to get or set a value of the object.
The parameter PropertyName is the name of the property to assign a new value to or to get the value. The value property can optionally take a list of properties when assigning a value, i.e.,
OLEObject.Value(NameOfProperty As String, params() As Variant) = value
If the optional parameter ByValue is True, property assignment is by value.
WordApplication.ValueArray
ValueArray(Name As String, Parameters() As Variant) As Variant()
Used to get or set a value of the object.
The Name parameter is the name of the property to assign a new value to or to get the value. ValueArray can accept a list of parameters to pass to the automation object. The parameters array is assumed to be 1-based.
WordApplication.ValueArray2D
ValueArray2D(Name As String, Parameters() As Variant) As Variant()
Used to get or set a value of the object for two-dimensional arrays.
The Name parameter is the name of the property to assign a new value to or to get the value. ValueArray2D can accept a list of parameters to pass to the automation object. The Parameters array is assumed to be 1-based.
Event descriptions
WordApplication.EventTriggered
EventTriggered(NameOfEvent As String, Parameters() As Variant) As Variant
Occurs when the OLEObject receives an event from the automation server. The event name is passed as the first parameter and the parameters for the event are passed as an array of variants.
Notes
The language that you use to automate Microsoft Office applications is documented by Microsoft and numerous third-party books on Visual Basic for Applications (VBA). Microsoft Office applications provide online help for VBA. To access the online help, choose Macros from the Tools Menu of your MS Office application, and then choose Visual Basic Editor from the Macros submenu. When the Visual Basic editor appears, choose Microsoft Visual Basic Help from the Help menu. The help is contextual in the sense that it provides information on automating the Office application from which you launched the Visual Basic editor.
If VBA Help does not appear, you will need to install the help files. On Windows Office 2003, Office prompts you to install the VBA help files when you first request VBA help. You don't need the master CD.
Microsoft has additional information on VBA at http://msdn.microsoft.com/vbasic/ and have published their own language references on VBA. One of several third-party books on VBA is "VB & VBA in a Nutshell: The Language" by Paul Lomax (ISBN: 1-56592-358-8).
Sample code
This code creates a Word document from the text entered into a DesktopTextField on the form, TextField1.
The code is in a DesktopButton's Pressed event handler. It also sets the font and font size of the text.
Try
Var word As WordApplication
Var doc As WordDocument
Var style As WordStyle
Var v As Variant
Var p As Integer
word = New WordApplication
word.Visible = True
doc = Word.Documents.Add
doc.Range.Text = TextField1.Text
style = doc.Paragraphs(1).Style
' populate 3 labels with font style, font name, font size info
Label1.Text = style.NameLocal
FontNameLabel.Text = style.Font.Name
p = style.Font.Size
FontSizeLabel.Text = p.ToString
Catch err As OLEException
MessageBox(err.Message)
End Try
This example does a search and replace operation on the text in TextField1, using the text in the DesktopTextFields, FindText and ReplaceText. The Word document is updated after the search and replace.
Try
Var word As WordApplication
Var doc As WordDocument
Var find As WordFind
word = New WordApplication
doc = word.ActiveDocument
find = word.Selection.Find
find.ClearFormatting
find.Replacement.ClearFormatting
Find.Text = FindText.Text ' Find field
Find.Replacement.Text = ReplaceText.Text ' replace with field
find.Forward = True
find.Wrap = Office.wdFindContinue
find.Format = False
find.MatchCase = False
find.MatchWholeWord = False
find.MatchWildcards = False
find.MatchSoundsLike = False
find.MatchAllWordForms = False
Var oleParam As New OLEParameter
oleParam.Value = office.wdReplaceAll
oleParam.Position = 11
find.Execute(oleParam)
TextField1.Text = doc.Range.Text
Catch err As OLEException
MessageBox(err.Message)
End Try
Compatibility
All project types on all supported operating systems.