From Xojo Documentation

You are currently browsing the old Xojo documentation site. It will go offline as of October 2, 2023. Please visit the new Xojo documentation site! - you will be redirected shortly...

Class (inherits from OLEObject)

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.


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.

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.Value = style.NameLocal
FontNameLabel.Value = style.Font.Name
p = style.Font.Size
FontSizeLabel.Text = p.ToString

Exception err As OLEException

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.

Var word As WordApplication
Var doc As WordDocument
Var find As WordFind

word = New WordApplication
doc = word.ActiveDocument

find = word.Selection.Find


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


TextField1.Value = doc.Range.Text

Exception err As OLEException

See Also

UserGuide:Office Automation Overview, ExcelApplication, Office, OLEObject, OLEException, PowerPointApplication classes.