Jump to content
Sign in to follow this  

Word UDF - Word version question

Recommended Posts


I have a script that, inter alia, creates a word doc from a template, saves it with a predetermined filename and path, then allows user to add data and save to the set location.

It works fine on Word 2002, 2003 and 2007 but it failed on a PC with Word 2000.

It opened the template rather than a new document based on the template, then failed to save it with a message that it can't save a document to a template format.

Before I spend some time trying to de-bug this [or find another reason specific to that PC for its failure], I want to make sure that it's not just that the word UDF doesn't work fully with that old a version of word.

The relevant bit of code is

$oWordApp = _WordCreate ("")
    Local $Template = "Test3.dot"
    Local $oOpenTemplate = _WordDocOpen ($oWordApp, @ScriptDir & "\" & $Template)

$oDoc = _WordDocGetCollection ($oWordApp, 0)

    _WordDocSaveAs ($oOpenTemplate, $Dir & $filename)
    Winclose ("Document1")

$FullFilename= $Dir & $filename

    ;-------------------------------Newly named document created from template.
Local $email =  IniRead (@ScriptDir & "\ClinDoc.ini","SecEmails", $Secretary, "(insertEmail)")   
Local $TelNo =  IniRead (@ScriptDir & "\ClinDoc.ini","SecTelNo", $Secretary, "(insertTel)")   
Local $FaxNo =  IniRead (@ScriptDir & "\ClinDoc.ini","SecFaxNo", $Secretary, "(insertFax)")

;MsgBox (4096, "email And tel no", $email & " " & $TelNo)
    ;                               Next to add form data
;Local $OurRef = $Author&"/"&$Secretary&"/"&$HSP
    $oFind = _WordDocFindReplace($oOpenTemplate, "99", @MDAY,1)
    $oFind = _WordDocFindReplace($oOpenTemplate, "8888", @YEAR)
    $oFind = _WordDocFindReplace($oOpenTemplate, "Mmm",$Mon)
    $oFind = _WordDocFindReplace($oOpenTemplate, "HHHH", $Header)
    $oFind = _WordDocFindReplace($oOpenTemplate, "RRRR", $Author&"/"&$Secretary&"/"&$HSP)
    $oFind = _WordDocFindReplace($oOpenTemplate, "NNNN", $NHSnumber)    
    $oFind = _WordDocFindReplace($oOpenTemplate, "CCCC", $ClinicCode,1)
    $oFind = _WordDocFindReplace($oOpenTemplate, "Cdcdcd", $ClinicDate) 
    $oFind = _WordDocFindReplace($oOpenTemplate, "Addressee", $sGPaddress)
    $oFind = _WordDocFindReplace($oOpenTemplate, "Doctor", $Recipient)
    $oFind = _WordDocFindReplace($oOpenTemplate, "SURNAME", $LastName)
    $oFind = _WordDocFindReplace($oOpenTemplate, "FIRSTNAME", $FirstName)
    $oFind = _WordDocFindReplace($oOpenTemplate, "DoB", $DoB)
    $oFind = _WordDocFindReplace($oOpenTemplate, "(insertTel)", $TelNo)
    $oFind = _WordDocFindReplace($oOpenTemplate, "(insertFax)", $FaxNo)
    $oFind = _WordDocFindReplace($oOpenTemplate, "(insertEmail)", $email)

;Direct line: 01935 (insertTel)
;Email: (insertEmail)

    ;                               Next to add form metadata
    _WordDocPropertySet ($oOpenTemplate, "title", $LastName & ", " & $FirstName)
    _WordDocPropertySet ($oOpenTemplate, "subject", $Subject)
    _WordDocPropertySet ($oOpenTemplate, "author", $Secretary)
    _WordDocPropertySet ($oOpenTemplate, "manager", $Author)
    _WordDocPropertySet ($oOpenTemplate, "company", "Yeovil District Hospital NHSFT")
    _WordDocPropertySet ($oOpenTemplate, "keywords", $NHSnumber & ", " & $HSP & ", " & $LastName & ", " & $FirstName & ", " & $Author & ", " & $Secretary)
    _WordDocPropertySet ($oOpenTemplate, "template", $Template)

;----------------------new bit for v2.5
$oOpenTemplate.Range.insertAfter (@CR & $sAddress & @CR & @CR )

    _WordDocSave ($oOpenTemplate)

It then goes on to allow user to enter text content before saving again, copying content to use later, and closing the document:

_WordDocSave ($oOpenTemplate)
;$sContent = $oOpenTemplate.Activedocument.Range.Text
$sContent = $oWordApp.Activedocument.Range.Text
    _WordQuit ($oWordApp)



Edit - I've now tried it on Word 97 and it fails on that, too - so I guess it is a version problem.

Edited by saywell

Share this post

Link to post
Share on other sites

in word you can press alt f11 to go into the vba interface then use f2 to open the object browser. then you can see the differences in the method definitions etc if you wanted to tweak your script to work with those versions too. well 2k anyway, i'm not sure on the 97

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.