WordApplication

From Real Software Documentation

Jump to: navigation, search
Class (inherits from OLEObject)

Used to automate Microsoft Word. Supported on the Windows platform only.


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).


Examples

This example 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.

Dim word as WordApplication
Dim doc as WordDocument
Dim style as WordStyle
Dim v as Variant
Dim 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 statictexts with font style, font name, font size info
styletext.text = style.namelocal
stylefontname.text = style.font.name
p=style.font.size
stylefontsize.text = Str(p)

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.

Dim word as WordApplication
Dim doc as WordDocument
Dim 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

Dim oleparam as New OLEParameter

oleparam.value = office.wdReplaceAll
oleparam.Position = 11

find.execute oleparam

TextField1.Text = doc.range.text

Exception err as OLEException
MsgBox err.message


See Also

ExcelApplication, Office, OLEObject, OLEException, PowerPointApplication classes.

Personal tools