Sign in to follow this  
Followers 0
big_daddy

Microsoft Word Automation Library

117 posts in this topic

#1 ·  Posted (edited)

There have been several requests for Microsoft Word Automation lately so I decided to take a stab at creating a library. DaleHohm has been kind enough to let me base my code off his IE.au3 library which will undoubtedly save countless hours. Currently it only has basic functions, but I will continue to add new functions and fix bugs as often as possible. Please test and provide as much feedback as possible, this will help keep me proactive with the project.

Included in the beta version as of v3.2.3.9

T1.0-1, 4/14/2007

Word.au3Previous versions 1350+ downloads

---

Help File w/Examples (Updated for T1.0-1, 4/14/2007) - Thanks JdeB

Word_Help_File.zipPrevious versions 620+ downloads

---

See http://www.autoitscript.com/fileman/users/Big_Daddy/libraries/word/history.htm for history.

Edited by big_daddy

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Great news.

Funnily enough, I originally had planned to do this myself, but got caught up in the Excel stuff that people wanted, and ended up just using 4 funcs inside Excelcom to run Word Macros!

I still use these a lot for that purpose; perhaps I can use yours early on if you get the runmacro going.

Func _WordCOM(ByRef $s_FilePath, $s_MEWordCom = "Macro", $s_Macro = "DestinationDirectory", $s_WordValue = "Hi", $s_i_Save = "NoSave", $s_i_Visible = "NotVisible", $s_i_Exit = "NotExit",$i_Suppress = 0)
    Local $o_Word
    If $s_i_Save = "Save" Then $s_i_Save = 1
    If $s_i_Visible = "Visible" Then $s_i_Visible = 1
    If $s_i_Exit = "Exit" Then $s_i_Exit = 1
    ;MsgBox(0,"","$s_i_Visible="&$s_i_Visible)
    ;==================================================if all numbers
    If $s_i_Visible <> 1 Then $s_i_Visible = 0
    If Not FileExists($s_FilePath) Or Not StringInStr($s_FilePath, "doc") Then
        _WordCreateBlank($s_FilePath)
    EndIf
    If Not IsObj ($o_Word) Then
        $o_Word = ObjGet ($s_FilePath) ; Get anWord Object from an existing filename
        If Not IsObj ($o_Word) Then
            Sleep(500)
            $o_Word = ObjGet ($s_FilePath) ; Get anWord Object from an existing filename
            If Not IsObj ($o_Word) Then
                MsgBox(0, "Word File Test", "Error: Could not open " & $s_FilePath & " as an Word Object.")
            EndIf
        EndIf
    EndIf
    If IsObj ($o_Word) Then
        With $o_Word
            Select
                Case StringInStr($s_MEWordCom, "Macro")
                    .Application.Visible = $s_i_Visible
                    $Close = 0
                    .Application.Run ($s_Macro)  ;"Normal.NewMacros.Font4"
                Case StringInStr($s_MEWordCom, "Close")
                    $Close = 1
            EndSelect
        EndWith
        If $s_i_Save = 1 Then
            $o_Word.Save
        EndIf
        If $Close = 1 And $s_i_Exit = 0 Then
            ;$oWord.saved=1
            $o_Word.close (0)
        EndIf
        If $s_i_Exit = 1 Then
            $o_Word.Application.Quit
            While ProcessExists('Winword.exe')
                Sleep(10)
            WEnd
        EndIf
    Else
        MsgBox(0, "Word File Test", "Error: Could not open " & $s_FilePath & " as an Word Object.")
    EndIf
    ;return $var
EndFunc   ;==>_WordCOM
Func _WordMacro(ByRef $s_FilePath, $s_Macro = "DestinationDirectory", $s_WordValue = "Hi", $s_i_Visible = 0 ,$i_Suppress = 0)
    _WordCOM($s_FilePath, "Macro", $s_Macro, $s_WordValue, "NOTSave", $s_i_Visible , "NOTExit")
EndFunc   ;==>_WordMacro
Func _WordClose(ByRef $s_FilePath, $s_i_Save = "NoSave", $s_i_Exit = "NotExit")
    _WordCOM($s_FilePath, "Close", "DestinationDirectory", "Hello", $s_i_Save, "NotVisible", $s_i_Exit)
EndFunc   ;==>_WordClose
Func _WordCreateBlank($s_FilePath)
    $NewName = $s_FilePath
    $WordNew = ObjGet ($NewName)
    If IsObj ($WordNew) Then $WordNew.Application.quit
    $WordNew = ObjCreate ("Word.Application")
    $WordNew.documents.Add
    $WordNew.DisplayAlerts = 0
    ;$WordNew.ScreenUpdating = 0
    If FileExists($NewName) Then
        FileDelete($NewName)
    EndIf
    $WordNew.Activedocument.SaveAs ($NewName)
    $WordNew.DisplayAlerts = -1
    ;$WordNew.ScreenUpdating = -1
    $WordNew.quit
EndFunc   ;==>_WordCreateBlank
Edited by randallc

Share this post


Link to post
Share on other sites

Looks good so far. Hope it goes well in future releases.


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

That's good script!

i542


I can do signature me.

Share this post


Link to post
Share on other sites

It's only a set of functions. You need to use them yourself


My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.

Share this post


Link to post
Share on other sites

I made some small changes to the library which I felt were important enough for a new release, see update history for changes. I also added a basic example script to help the noobs (thanks gafrost).

I'm open to suggestions on what functions should be included, please feel free to add your input. Here's a link that should give you an idea of what can be accomplished through scripting.

Thanks,

Bob

Share this post


Link to post
Share on other sites

I'm open to suggestions on what functions should be included, please feel free to add your input. Here's a link that should give you an idea of what can be accomplished through scripting.

Thanks,

Bob

Well, hate to state the obvious, but make it do everything on that link :whistle:

This is INCREDIBLE, very usefull, been looking for a 'word auto-mater' for like Months,

thanks for the great UDFs

Share this post


Link to post
Share on other sites

New update available! See update history for changes.

Enjoy,

Bob

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

I know that your example doesn't do anything bad but McAfee VirusScan does this Edit:I removed the attachment to save space every time I run your example.

Maybe you should use different ways to write this UDF.

it is complaining about the _Quit, here is the "find out more":

Edit:I removed the attachment to save space

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Share this post


Link to post
Share on other sites

I know that your example doesn't do anything bad but McAfee VirusScan does this every time I run your example.

Maybe you should use different ways to write this UDF.

it is complaining about the _Quit, here is the "find out more":

can u print a word doc?

Share this post


Link to post
Share on other sites

@rbhkamal - Application.Quit is the standard method for closing a word application object. I'm not sure why McAfee is picking up on it.

@crystalburner - Of course. That will be one of the next functions I create, but I'm still trying to turn this into something user friendly. :whistle:

Share this post


Link to post
Share on other sites

@rbhkamal - Application.Quit is the standard method for closing a word application object. I'm not sure why McAfee is picking up on it.

@crystalburner - Of course. That will be one of the next functions I create, but I'm still trying to turn this into something user friendly. :whistle:

shall i just use control send instead of waiting

Share this post


Link to post
Share on other sites

shall i just use control send instead of waiting

Until I have the function finished you can try calling the method manually. Just call it from a document object using the link I provided as a reference. Here is what the syntax should look like, of course you need to change all the parameters to their corresponding values.

$oDoc.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)

Share this post


Link to post
Share on other sites

Until I have the function finished you can try calling the method manually. Just call it from a document object using the link I provided as a reference. Here is what the syntax should look like, of course you need to change all the parameters to their corresponding values.

$oDoc.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)
Wouldn't $oDoc.PrintOut() alone work to print the entire document to the default printer? The printer config on my test machine is hosed at the moment so I can't test.

Dale


Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Share this post


Link to post
Share on other sites

Great job big_daddy!

I used your example and my McAfee VirusScan didn't report any trouble at all.

vim

Share this post


Link to post
Share on other sites

@Dale - Thanks, your right this seems to work until I can complete the function.

#include <Word.au3>

$oWordApp = _WordCreate (@ScriptDir & "\Test.doc")
$oDoc = $oWordApp.ActiveDocument
$oDoc.Range.insertAfter ("This is some text to insert.")
$oDoc.PrintOut ()
_WordQuit ($oWordApp, 0)

Share this post


Link to post
Share on other sites

Until I have the function finished you can try calling the method manually. Just call it from a document object using the link I provided as a reference. Here is what the syntax should look like, of course you need to change all the parameters to their corresponding values.

$oDoc.PrintOut(Background, Append, Range, OutputFileName, From, To, Item, Copies, Pages, PageType, PrintToFile, Collate, ActivePrinterMacGX, ManualDuplexPrint, PrintZoomColumn, PrintZoomRow, PrintZoomPaperWidth, PrintZoomPaperHeight)
hmmm

looks like hard work

why wont this work

ControlSend("test.doc - Microsoft Word", "", "_WwG1","^P")

Share this post


Link to post
Share on other sites

hmmm

looks like hard work

why wont this work

ControlSend("test.doc - Microsoft Word", "", "_WwG1","^P")

Did you not see my post right before yours? I gave an example of how to print the entire document to the default printer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0