gerardsweeney Posted May 8 Posted May 8 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!
water Posted May 8 Posted May 8 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
gerardsweeney Posted May 8 Author Posted May 8 (edited) Thanks for the suggestion. Here's the results from the SciTE console.. I updated the script to include the @extended error as well, in case it helped. I'm assuming error 3 means there was nothing found to replace. It replaced for Story 9 - which is the primary footer. So that ties in with what I'm seeing. expandcollapse popupNumber of Stories: Type of Story:1 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:5 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:6 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:7 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:8 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:9 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 0 (Extended: 0) Type of Story:10 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:12 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:13 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:15 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) Type of Story:16 @error of Replace1: 3 (Extended: 0) @error of Replace2: 3 (Extended: 0) @error of Replace3: 3 (Extended: 0) @error of Replace4: 3 (Extended: 0) >Exit code: 0 Time: 9.668 Edited May 8 by gerardsweeney
Solution gerardsweeney Posted May 15 Author Solution Posted May 15 Water had very kindly looked at a blank copy of the document, and concluded the same as me - it's horrific I've cobbled together a script to use SEND to trigger CTRL H to do the search/replacing.. Nowhere near as elegant as using the functions, but still infinitely preferable to doing it manually
gerardsweeney Posted May 23 Author Posted May 23 (edited) To revive this slightly! The following code that I nicked from Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($oWord, $File, Default, Default, False) Global Const $wdMove = 0 $FindText = "*FORENAME*" $ReplaceText = "Han" With $oWord.Selection.Find .Replacement.Text = $ReplaceText .Forward = True .ClearFormatting .Wrap = 1 ;.Wrap could be $wdFindContinue which is 1 .Execute($FindText) EndWith ALMOST works. It populates the Find and Replace boxes with "*FORENAME*" and "Han". It just doesn't execute the replace all. What the heck am I missing?! This is on Word 2016, BTW. In case that has any bearing - I know Microsoft like to change things! Edited May 23 by gerardsweeney
Nine Posted May 23 Posted May 23 I believe the way AutoIt interacts with Word Com object, you will need to specify the ReplaceWith argument of the execute method. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
water Posted May 23 Posted May 23 (edited) Have a look at the source of the Word UDF, function _Word_DocFindReplace to see how it works Edited May 23 by water 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now