Jump to content

MS Word object


 Share

Recommended Posts

Hi all,

I currently have a working script to add autotext entries into a template, but I am using a combination of a word macro and an autoit script. I believe that I can accomplish the same with just autoit, leaving no need for the word macro.

Currently I have the following:

Au3

#include <File.au3>
#include <Word.au3> 
$template = FileOpenDialog( "Select *.doc template to modify", @ScriptDir, "Template (*.dot)", 3 )
$wordlist = FileOpenDialog( "Select *.txt text file containing words to add", @ScriptDir, "Text File (*.txt)", 3 )
$oWordApp = _WordCreate( $template, 0, 0 )
$eof = _FileCountLines( $wordlist )
If $eof < 1 Then Exit
For $avar = 1 To $eof
    $ss = StringSplit( FileReadLine( $wordlist, $avar ), "|" )
    _WordMacroRun( $oWordApp, "test", $ss[1], $ss[2])
Next
_WordDocSave( $oWordApp )
_WordQuit( $oWordApp )

Macro

Sub test(name As String, text As String)
    Selection.text = text
    Activedocument.AttachedTemplate.AutoTextEntries.Add name:=name, Range:=Selection _
        .Range
    Selection.Delete
End Sub

Could somebody help me convert this to a pure autoit solution?

Link to comment
Share on other sites

  • Moderators

See if this does what you need...

#include <File.au3>
#include <Word.au3>

$template = FileOpenDialog("Select *.doc template to modify", @ScriptDir, "Template (*.dot)", 3)
$wordlist = FileOpenDialog("Select *.txt text file containing words to add", @ScriptDir, "Text File (*.txt)", 3)
$oWordApp = _WordCreate($template, 0, 0)
$oDoc = _WordDocGetCollection($oWordApp, 0)
$eof = _FileCountLines($wordlist)
If $eof < 1 Then Exit
For $avar = 1 To $eof
    $ss = StringSplit(FileReadLine($wordlist, $avar), "|")
    _WordDocAutoTextEntryAdd($oDoc, $ss[1], $ss[2])
Next
_WordDocSave($oWordApp)
_WordQuit($oWordApp)


Func _WordDocAutoTextEntryAdd($o_Doc, $s_Name, $s_Text)
    $o_Doc.Application.Selection.text = $s_Text
    $o_Doc.AttachedTemplate.AutoTextEntries.Add($s_Name, $o_Doc.Application.Selection.Range)
    $o_Doc.Application.Selection.Delete
EndFunc   ;==>_WordDocAutoTextEntryAdd
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...