WordApplication

From Xojo Documentation

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.

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 TextField on the form, TextField1.

The code is in a PushButton's Action 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.Value

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
MsgBox(err.Message)

This example does a search and replace operation on the text in TextField1, using the text in the TextFields, 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.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.Value = doc.Range.Text

Exception err As OLEException
MsgBox(err.Message)

See Also

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