Jump to content

Recommended Posts

Posted

Hi, there.

I've just started dabbling with the Word UDF, as I'm hoping to use it to batch populate a document.

The code below works absolutely fine for a dummy Word Document I've created, but not for the actual template document I need.

#include <word.au3>

ProcessWordDoc("TestDocument.docx")
ProcessWordDoc("OfficialDocument.docx")
Msgbox(0, "", "Done")
EXIT

Func ProcessWordDoc($F)
$FirstName = "Han"
$Surname = "Solo"
$Building = "Millenium Falcon"
$ID = "TK421"
$File = @ScriptDir & "\" & $F

; Create application object
Global $oWord = _Word_Create()
Global $oDoc = _Word_DocOpen($oWord, $File)

    For $oStoryRange In $oDoc.StoryRanges
        For $Loop = 1 to 1
        _Word_DocFindReplace($oDoc, "FIRSTNAME", $FirstName, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "SECONDNAME", $Surname, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "BUILDINGNAME", $Building, Default, $oStoryRange)
        _Word_DocFindReplace($oDoc, "000000000", $ID, Default, $oStoryRange)
        Next
    Next
EndFunc


I don't know much about Word, so bear with me here..

It looks like the official document has page 1 configured differently from other pages.
It has the "000000000" id text on page 1 in a text box, and then in the footer of subsequent pages.
Page 1 also has text boxes with the First name/surname/building fields (which I've populated with the FIRSTNAME/SECONDNAME/BUILDINGNAME strings for search/replace.


In Word, I can replace all of the Search/replace fields no problem (If I use CTRL H/Search/Replace/All).

With the above script, page 1 isn't being touched. The footer gets replaced, but nothing else.

I'm hoping it's something REALLY obvious that I'm missing, here?

I don't really want to attach the official document here - for fairly obvious reasons. I realise that doesn't help with troubleshooting.
 

All advice gratefully received!

Posted

I would start with collecting information about the document and adding error checking. Something like:

Consolewrite("Number of Stories:" & $oDoc.StoryRanges & @CRLF)
    For $oStoryRange In $oDoc.StoryRanges
        Consolewrite(@CRLF & "Type of Story:" & $oStoryRange.StoryType & @CRLF)
;        For $Loop = 1 to 1 ; Not needed at the moment
        _Word_DocFindReplace($oDoc, "FIRSTNAME", $FirstName, Default, $oStoryRange)
        Consolewrite("@error of Replace1: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "SECONDNAME", $Surname, Default, $oStoryRange)
        Consolewrite("@error of Replace2: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "BUILDINGNAME", $Building, Default, $oStoryRange)
        Consolewrite("@error of Replace3: " & @error & @CRLF)
        _Word_DocFindReplace($oDoc, "000000000", $ID, Default, $oStoryRange)
        Consolewrite("@error of Replace4: " & @error & @CRLF)
 ;       Next
    Next

The wdstorytype enumeration can be found here: https://learn.microsoft.com/en-us/office/vba/api/word.wdstorytype

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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
  • Recently Browsing   0 members

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