The Word UDF offers functions to control and manipulate Microsoft Word documents.
This page describes the Word UDF that comes with AutoIt 184.108.40.206 or later.
- 1 Features
- 2 Concepts
- 3 Functions
- 4 Script breaking changes after AutoIt version 220.127.116.11
- 4.1 General
- 4.2 Function _WordCreate/_Word_Create
- 4.3 Function _WordDocPropertyGet/-
- 4.4 Function _WordDocPropertySet/-
- 4.5 Function _WordErrorHandlerDeRegister/_DebugCOMError
- 4.6 Function _WordErrorHandlerRegister/_DebugCOMError
- 4.7 Function _WordErrorNotify/-
- 4.8 Function _WordMacroRun/-
- 4.9 Function _WordPropertyGet/-
- 4.10 Function _WordPropertySet/-
New versions of Microsoft Office have been released since the last changes were made to the Word UDF. The new extensions (e.g. docx) were not (fully) supported, new functions were missing etc. The current version of the Word UDF lifts this limitations.
- Works with as many instances of Word as you like - not just one
- Works with any document - not just the active one
- Only does what you tell it to do - no implicit "actions"
- Works with ranges and lets you move the insert marker
- Supports reading/writing of tables
- Support for every file format Word supports
The UDF only covers basic user needs. Single line functions (like getting document properties) or functions with too many parameters (like running a macro) are not covered by this UDF. You need to use the Word COM yourself.
Good reading: Working with Word document content objects
A Range is a block made of one or more characters that Word treats as a unit. The functions of the UDF mainly work with ranges. A range - unlike a selection - is not visible on the screen.Example:
Global $oWord = _Word_Create() Global $oDoc = _Word_DocAdd($oWord) Global $oRange = _Word_DocRangeSet($oDoc, 0) ; Use current selection $oRange.Insertafter("INSERTED TEXT") ; Insert Text $oRange = _Word_DocRangeSet($oDoc, $oRange, $WdCollapseEnd) ; Collapse the start of the range to the end position (create an insertion mark)
This function supports wildcards.
The following document is a good reading regarding wildcards.
To find and replace a string not only in the main text but everywhere in the document (including headers, footnotes etc.) please have a look at this article
Script breaking changes after AutoIt version 18.104.22.168
New versions of Microsoft Office have been released since the last changes were made to the Word UDF. New file types and new functions needed to be supported, hence the UDF was complete rewritten.
Some functions/parameters have been removed or renamed, new functions/parameters have been added. A detailed list of changes can be found here.
All function names have been changed from _Word* to _Word_*.
@extended no longer contains the number of the invalid parameter. The code returned in @error tells exactly what went wrong.
The following list shows the old/new function/parameter name (a "-" is shown if the function/parameter has been removed) and some example scripts how to mimic the behaviour of the "old" UDF. If there is no entry for a removed function/parameter then there is no need for this functionality.
It's mandatory now to call function _Word_Create before any other function. You could have used _WordCreate or _WordAttach in the old Word UDF. @extended is set if Word was already running.
Optional parameter to specify the file to open upon creation. Use _Word_DocOpen or _Word_DocAdd now.
Retrieves builtin document properties.
Word object model reference on MSDN for Word 2010:
Global $oWord = _Word_Create() Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.doc") Global $wdPropertyAuthor = 3 Global $sAuthor = $oDoc.BuiltInDocumentProperties($wdPropertyAuthor).Value ; Retrieves the Author of the document
Sets builtin document properties.
For links to the Word object model reference on MSDN see function _WordDocPropertyGet.Example code:
Global $oWord = _Word_Create() Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.doc") Global $wdPropertyAuthor = 3 $oDoc.BuiltInDocumentProperties($wdPropertyAuthor).Value = "PowerUser" ; Sets the Author of the document
The default COM error handler has been moved to the Debug UDF. See _WordErrorHandlerRegister for details.
The default COM error handler has been moved to the Debug UDF. But you can still create a custom COM error handler by using ObjEvent.
#include <Debug.au3> _DebugSetup("Word Debug Window", True, 1, "", True) _DebugCOMError(1) ; Register a default COM error handler to grab Word COM errors and write the messages to the Debug window ; Do Word processing here _DebugCOMError(0) ; DeRegister the default COM error handler
The Word UDF no longer creates text error messages and writes them to the Console.
You need to check the macros @error and @extended after you called a function. The returned values are documented in the help file.
A macro can now be run by a single line.
Global $oWord = _Word_Create() Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.doc") $oWord.Run("macro_name")
Retrieves application and document properties. Many of the properties for the Options object correspond to items in the Options dialog box.
Word object model reference on MSDN for Word 2010:
Global $oWord = _Word_Create() $bVisible = $oWord.Visible ; Returns True when the Word application is visible, else False $bUpdatePrint = $oWord.Options.UpdateFieldsAtPrint ; True if Microsoft Word updates fields automatically before printing a document
Sets application and document properties. Many of the properties for the Options object correspond to items in the Options dialog box.
For links to the Word object model reference on MSDN see function _WordPropertyGet.Example code:
Global $oWord = _Word_Create() $bVisible = $oWord.Options.SaveInterval = 5 ; Sets Word to save AutoRecover information for all open documents every five minutes