water Posted August 14, 2017 Share Posted August 14, 2017 I will check as soon as I return to my office. At the moment I'm on vacation. So please stay tuned - but don't hold your breath 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...
ahha Posted August 15, 2017 Author Share Posted August 15, 2017 water, No hurry - enjoy your vacation Link to comment Share on other sites More sharing options...
water Posted August 16, 2017 Share Posted August 16, 2017 This works for me (uncomment one or the other of the following method so they do not interfere): expandcollapse popup#include <Word.au3> Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $oWord = _Word_Create() Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx") Global $oRange1 = _Word_DocFind($oDoc, "TestRange1") $oRange1.Underline = True MsgBox(0, "", "Range1 found and underlined") Global $oRange2 = _Word_DocFind($oDoc, "TestRange2") $oRange2.Underline = True MsgBox(0, "", "Range2 found and underlined") #cs ; This works $oRange1.Copy() $oRange2.Paste() MsgBox(0, "", "Replaced Range2 with Range1") #ce #cs ; This works as well Global $wdFormatOriginalFormatting = 16 ; Preserves original formatting of the pasted material. $oRange1.Copy() $oRange2.PasteAndFormat($wdFormatOriginalFormatting) #ce ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc Test.docx 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...
ahha Posted August 17, 2017 Author Share Posted August 17, 2017 water, Thanks. Will try in detail later. One quick question, okay two 1) do .Copy and .Paste and .PasteAndFormat all generate COM errors if they don't work and 2) how do I simulate an object error so that I can test any recovery code I might write? Link to comment Share on other sites More sharing options...
water Posted August 17, 2017 Share Posted August 17, 2017 Every method you call, every property you access will generate a COM error in case of a problem. Just call a non-existing method like $oExcel.ThisIsANonExistingFunction 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...
ahha Posted August 18, 2017 Author Share Posted August 18, 2017 water, Thanks for the COM code I'm beginning to understand it. I was testing for speed of the two approaches and they don't behave the same. The code below when run shows how the .Paste replaces the range, while the .PasteAndFormat replaces the range initially and then seems to extend it. I'm not sure what's going on. expandcollapse popup;from https://www.autoitscript.com/forum/topic/189731-word-udf-_word_docrangeset-affecting-a-different-range/?page=2 ;Posted Wednesday at 12:57 AM · Report post ;This works for me (uncomment one or the other of the following method so they do not interfere): #include <Word.au3> Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Global $oWord = _Word_Create() Global $oDoc = _Word_DocOpen($oWord, @ScriptDir & "\test.docx") Global $oRange1 = _Word_DocFind($oDoc, "TestRange1") $oRange1.Underline = True MsgBox(0, "", "Range1 found and underlined") Global $oRange2 = _Word_DocFind($oDoc, "TestRange2") $oRange2.Underline = True MsgBox(0, "", "Range2 found and underlined") #cs ;this approach takes ~120ms per copy/paste $hTimer = TimerInit() $rep = 10 for $i = 1 to $rep ; This works $oRange1.Copy() $oRange2.Paste() Next MsgBox(0, "Time/copy and paste = ", TimerDiff($hTimer)/$rep & "ms") MsgBox(0, "", "Replaced Range2 with Range1") #ce #cs ;this approach actually is more like an extend $hTimer = TimerInit() $rep = 10 Global $wdFormatOriginalFormatting = 16 ; Preserves original formatting of the pasted material. for $i = 1 to $rep ; This works as well $oRange1.Copy() $oRange2.PasteAndFormat($wdFormatOriginalFormatting) Next MsgBox(0, "Time/copy and paste = ", TimerDiff($hTimer)/$rep & "ms") #ce ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError) ; Do anything here. ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _ @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _ @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _ @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _ @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _ @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _ @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _ @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _ @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _ @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc ;==>_ErrFunc Any thoughts? Link to comment Share on other sites More sharing options...
water Posted August 18, 2017 Share Posted August 18, 2017 Is processing time really an issue? 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...
ahha Posted August 18, 2017 Author Share Posted August 18, 2017 Actually yes. I plan on replacing many places (more than 1 replacement) in large documents (100+ pages - possibly 100-1000 replacements), and eventually doing possibly 20-100 different files one at a time. Thus my concern on coding for errors. I can have it run overnight if needed but it needs to be pretty bulletproof. Link to comment Share on other sites More sharing options...
water Posted August 18, 2017 Share Posted August 18, 2017 Start Word invisible and make it visible again when all processing has been done. Cuts off about 30% processing time. Global $oWord = _Word_Create(False) ; Start Word invisible ... ... $oWord.Visible = True ; Make Word visible again 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...
ahha Posted August 19, 2017 Author Share Posted August 19, 2017 water, Thanks will try the invisible. What about the behavior of .PasteAndFormat in the loop? Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 How fast is the PasteAndFormat approach? How did you notice that the range is extended when using PasteAndFormat? 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...
ahha Posted August 19, 2017 Author Share Posted August 19, 2017 water, Can't get a true read on PasteAndFormat approach because when you run it in a loop it seems to keep extending Range2. Here's a sample after 10 iterations. TestRange1TestRange1TestRange1TestRange1TestRange1TestRange1TestRange1TestRange1TestRange1TestRange1 Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 Does it really matter? The first replace works as expected. You wouldn't do a replace multiple times with your "real" documents, would you? 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...
Jury Posted August 19, 2017 Share Posted August 19, 2017 (edited) If I may butt - in learning from both of your examples I'd think that this would u-Bold any Bold text and paragraph marker in the test file but it doesn't. Something I'm not seeing (often the case) Global $oWord = _Word_Create() Global $sTestfile = $processing & "Test.docx" ConsoleWrite($sTestfile & @CRLF) Global $oDoc = _Word_DocOpen($oWord, $sTestfile) With $oWord.Selection.Find .Text = "TestRange^#^p" .Replacement.Font.Bold = False .Replacement.Text = "^&" .Forward = True .Wrap = $wdFindContinue .Format = True .MatchCase = True .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False EndWith _Word_DocFindReplace($oDoc, $oWord.Selection.Find.Text, $oWord.Selection.Find.Replacement.Text, $wdReplaceAll) $oWord.Application.Browser.Next my aim is to get rid of any font formatting (Bold, Italics) of all stand alone paragraph markers (those that have no text proceeding it) Edited August 19, 2017 by Jury Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 This won't work as you are just passing the find and replacement text to _Word_DocFindReplace. But passing the find object won't work as well as the function does not support an object as parameter. I suggest to use _Word_DocFindReplace as a template and create your own function. I have very little spare time at the moment so can't help very much with this issue. 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...
Jury Posted August 19, 2017 Share Posted August 19, 2017 Thanks for the hint Water - given your skills I can appreciate you have very little spare time. Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 (edited) I would try the following approach: Search for two adjacent paragraph characters (WordDocFind with "^p^p^") To remove the formatting of the second paragraph move the start of the found range ($oRange) one character to the right. To remove all formatting use Repeat until all occurrences have been processed $oRange.Select $oDoc.Selection.ClearFormatting Edited August 19, 2017 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 Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 Here is an even better example: http://wordmvp.com/FAQs/MacrosVBA/DeleteEmptyParas.htm 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...
ahha Posted August 19, 2017 Author Share Posted August 19, 2017 (edited) water, Thanks for the _Word_Create(False) suggestion. And yes the .PasteAndFormat approach does the first replace correctly which is what is needed. I ran many tests and can confirm invisible is about 30% faster than visible and .PasteAndFormat is about 25% slower than .Paste alone. So the fastest is .Copy .Paste running invisible, which running interpreted on my machine is about 90ms/replacement. By the way I noticed that _Word_Create(False) makes any open .doc windows not visible not just the one you are creating. water - I want to thank you for all your effort and responses, I'm probably still on the coding tricycle but I'm peddling much faster now Edited August 19, 2017 by ahha correction Link to comment Share on other sites More sharing options...
water Posted August 19, 2017 Share Posted August 19, 2017 1 hour ago, ahha said: By the way I noticed that _Word_Create(False) makes any open .doc windows not visible not just the one you are creating. If Word is already up and running when you call _Word_Create then the script attaches to this running instance. The parameter then sets this instance to invisible which includes all already opened documents. Set parameter two to True and a new instance of Word will be created leaving the already running instance/documents intact. Glad to be of service! Always happy to answer new questions 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