FrancescoDiMuro Posted July 6, 2018 Share Posted July 6, 2018 (edited) Good evening everyone I am working with Word UDF ( thanks @water! ), and, especially, with the function _Word_DocFindReplace(). The replace does work everywhere in the document, but, it does not work in Headers or Footers. Am I missing something or am I forced to use the code below? I have already looked in the Help file ( about _Word_DocFindReplace() ), but there are no mentions about replace text in Headers/Footers. Sub FindAndReplaceFirstStoryOfEachType() Dim rngStory As Range For Each rngStory In ActiveDocument.StoryRanges With rngStory.Find .Text = "find text" .Replacement.Text = "I'm found .Wrap = wdFindContinue .Execute Replace:=wdReplaceAll End With Next rngStory End Sub Thanks everyone in advance Best Regards. Edited July 9, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
water Posted July 6, 2018 Share Posted July 6, 2018 Correct. You need to specify the StoryRange to process. To do the find/replace operation everywhere you need to do something like: For $oStoryRange In $oDoc.StoryRanges _Word_DocFindReplace($oDoc, "Find", "ReplaceWith", Default, $oStoryRange) Next FrancescoDiMuro and Earthshine 2 My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 6, 2018 Author Share Posted July 6, 2018 @water Works like a charm Thanks Sir! Have a good day Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
water Posted July 6, 2018 Share Posted July 6, 2018 Thanks Glad the problem could be solved. I think I will add a section about "story" to the Word wiki FrancescoDiMuro 1 My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 6, 2018 Author Share Posted July 6, 2018 @water I specified the range in the _Word_DocFindReplace() function as you suggested to me, but I find out that the replace has been done only in the first page... I'm not so practice with Word object, and, English is not by my side when I try to translate "story" in Italian I was looking at this, and so, I don't know if I have to specify to move to the next "story", in order to apply find/replace to all pages. Can you help me to understand this? Thank you again And... 1 hour ago, water said: I think I will add a section about "story" to the Word wiki For everything helps someone, it will always deserves to be a peace of "story" Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 8, 2018 Author Share Posted July 8, 2018 (edited) @water Bump. I'd like to use this thread instead of open a new one Best Regards. Edited July 8, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
water Posted July 8, 2018 Share Posted July 8, 2018 Can you provide a sample document and a reproducer script so we can play with it? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 8, 2018 Author Share Posted July 8, 2018 Sure @water Here you are: expandcollapse popup#include <MsgBoxConstants.au3> #include <Word.au3> #include <WordConstants.au3> Global $objWord, _ $objWordDocument, _ $strWordDocument = @ScriptDir & "\Word_Document.docx", _ $objStoryRange Global $objWord = _Word_Create(False, False) If @error Then ConsoleWrite("Error while creating the Word object. Error: " & @error & @CRLF) Else $objWordDocument = _Word_DocOpen($objWord, $strWordDocument) If @error Then ConsoleWrite("Error while opening the document and " & $strWordDocument & ". Error: " & @error & @CRLF) Else For $objStoryRange In $objWordDocument.StoryRanges _Word_DocFindReplace($objWordDocument, "ABCD", "Hey!", $WdReplaceAll, $objStoryRange) If @error Then Exit Next _Word_DocSave($objWordDocument) If @error Then ConsoleWrite("Error while saving the document. Error: " & @error & @CRLF) Else _Word_DocClose($objWordDocument) If @error Then ConsoleWrite("Error while closing the document. Error: " & @error & @CRLF) Else _Word_Quit($objWord) If @error Then ConsoleWrite("Error while closing Word application. Error: " & @error & @CRLF) Else MsgBox($MB_ICONINFORMATION, "", "Word Document saved correctly.") EndIf EndIf EndIf EndIf EndIf Word_Document.docx Thanks again for your help Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
AutoBert Posted July 8, 2018 Share Posted July 8, 2018 It works for me without any problem. Link to comment Share on other sites More sharing options...
water Posted July 8, 2018 Share Posted July 8, 2018 On 6.7.2018 at 5:24 PM, FrancescoDiMuro said: I specified the range in the _Word_DocFindReplace() function as you suggested to me, but I find out that the replace has been done only in the first page... The example document you provided only consists of a single page (I checked on Linux using LibreOffice). Can you please provide an example document that you tested and that didn't work? BTW: Which version of Word do you run? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 8, 2018 Author Share Posted July 8, 2018 (edited) @AutoBert For me, it exits... I put an error check after the _Word_DocFindReplace(), and the error code was 3, extended was 0... Edited July 8, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 8, 2018 Author Share Posted July 8, 2018 (edited) @water Yes, I provided a document with just one page, because it isn't working with one page either! By the way, I am working with Microsoft Office 2016. Thanks Water Edited July 8, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
AutoBert Posted July 8, 2018 Share Posted July 8, 2018 3 hours ago, FrancescoDiMuro said: For me, it exits... I put an error check after the _Word_DocFindReplace(), and the error code was 3, extended was 0... Sorry i changed document in the way that in header, body and footer a replacestring exists. Without you have to delete line #21, seems that body is the first range in FindReplace order. I use a 2013 Home & Student version. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 8, 2018 Author Share Posted July 8, 2018 @AutoBert Yes. The body is the first place where the function does the research, and, if it doesn't find anything, it throws @error = 3 and @extendend = 0. But, even if I specify the StoryRange to do the find/replace, it seems to not find anything. So, am I missing something? Thanks for your help. Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Subz Posted July 9, 2018 Share Posted July 9, 2018 You just need to remove the following from with the loop: If @error Then Exit Link to comment Share on other sites More sharing options...
water Posted July 9, 2018 Share Posted July 9, 2018 Here it works the same way as Subz describes. Remove the error check and it does what it is intended to do. I will check where the @error = 3 comes from. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
water Posted July 9, 2018 Share Posted July 9, 2018 That's kind of a bug _Word_DocFindReplace returns @error = 3 when the Word Find.Execute method returns an error AND when the find/replace was not successful. This means: @error = 3 and @extended = 0: Search string was not found and everything else what MS describes as "not successful" @error = 3 and @extended <> 0: A real error happened and @extended gets set to the COM error (HRESULT). So check @extended for a real error My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 9, 2018 Author Share Posted July 9, 2018 (edited) @water Definitely a bug 2 hours ago, water said: @error = 3 and @extended = 0: Search string was not found and everything else what MS describes as "not successful" Search string was not found in the body of the Document, since the text to find is in the Header of the Document. By now, I tried to do the replacement of a string in the Document with more than one page, and it works correctly. But now, I'm trying with the "original" document where to find/replace strings, and it does the replace only in the first sheet of the document... I don't know what to think... Thanks EDIT: This peace of code does the trick, as it is stated in this tutorial: Local $objStoryRange For $objStoryRange In $objWordDocument.StoryRanges Do _Word_DocFindReplace($objWordDocument, "XNOMEX", $arrResult[1][0], Default, $objStoryRange) _Word_DocFindReplace($objWordDocument, "XDESCRIZIONEX", $arrResult[1][1], Default, $objStoryRange) $objStoryRange = $objStoryRange.NextStoryRange Until IsObj($objStoryRange) = 0 ; I don't know if this statement is correct, since I have to loop until objStoryRange Is Nothing Next Best Regards, Edited July 9, 2018 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
water Posted July 9, 2018 Share Posted July 9, 2018 Glad you got it working Just found a german document that explains why you need to use the NextStoryRange property. Will think about how to document this behavior in the wiki FrancescoDiMuro 1 My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted July 9, 2018 Author Share Posted July 9, 2018 @water I will wait for it! Should I mark as "Solved" this thread, or you want to update as soon as you document this thing in the Word UDF Wiki? Thanks for your help Best Regards. Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
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