pseakins Posted August 27, 2022 Share Posted August 27, 2022 Example1 for _Word_DocFindReplace from the AutoIt help works as promised. However, when I change the search string from "test document" to "udf" the replaced text is all uppercase. ie; from this _Word_DocFindReplace($oDoc, "test document", "test document with replaced text") to this _Word_DocFindReplace($oDoc, "udf", "test document with replaced text") I can't see any reason why this would happen, as far I know the text "UDF" at the end of the first line in the source document "\Extras\Test.doc" is just normal text that happens to be upper case. There's no reason (that I can see) that the replacement text should become all upper case. Am I missing something? #include <MsgBoxConstants.au3> #include <Word.au3> ; Create application object Local $oWord = _Word_Create() If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Open test document read-only Local $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\Extras\Test.doc", Default, Default, True) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ "Error opening '.\Extras\Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended) ; Change "test document" to "test document with replaced text". _Word_DocFindReplace($oDoc, "test document", "test document with replaced text") If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", _ "Error replacing text in the document." & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Text successfully replaced.") Phil Seakins Link to comment Share on other sites More sharing options...
Subz Posted August 27, 2022 Share Posted August 27, 2022 Find and replaced doesn't modify the formatting of the word, you'll notice that UDF is all caps in the example document. Link to comment Share on other sites More sharing options...
Subz Posted August 27, 2022 Share Posted August 27, 2022 Forgot to mention that if you want to change the case, you need to use $bMatchCase, for example: _Word_DocFindReplace($oDoc, "UDF", "test document with replaced text", $WdReplaceAll, 0, True) Link to comment Share on other sites More sharing options...
Solution pseakins Posted August 28, 2022 Author Solution Share Posted August 28, 2022 From the AutoIt helpfile; $bMatchCase [optional] If True the find is case sensitive (default = False) $bMatchCase is used to mofify the search parameters and should have nothing to do with the "format" of the replaced characters. As far as I know, upper and lower case in Word is not a "format'. While you can modify the case of a word using Shift-F3 there is no actual property (like bold or underline) that specifies that the entire word is one case or another. If you make a word bold, for example, and then manually edit the word by typing extra characters, those new characters become bold because that is the format that is in use at the time.. The same does not apply if the word were all uppercase and then you insert extra characters. The new characters would go in with whatever case has been selected by the keyboard. Having said all that it turns out that Microsoft Word does in fact use the search match case flag to perform some quite unintuitive magic. After a bit of googling I found this Microsoft answer; Replace requires "Match case" to be selected if one wants to Replace preserving the case of the replacement text. (I edited the subject so it made more sense.) As one of the respondents said "This is an anomaly you will have to live with. " My test case code will behave as required by changing it to; _Word_DocFindReplace($oDoc, "UDF", "text replaced here", 1, Default, 1) Phil Seakins Link to comment Share on other sites More sharing options...
water Posted August 28, 2022 Share Posted August 28, 2022 Added a link to this thread to the AutoIt wiki. pseakins 1 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 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