viptnn Posted September 16, 2017 Posted September 16, 2017 I have a document of 100 pages. i need to replace a lot of characters and words For example ∂,∆, π, lee ≈ with 'de', 'trang', 'P', 'loo' accordingly. _Word_DocFindReplace($oDoc, "∂", "de") can do ok, but replace single word and take too long time. How i can i search multiple letters(or even words) and replace it? Please help. I found VBA code maybe do work, if could pls translate to autoit thanks. Sub MultiReplace() Dim StrFind As String, StrRepl As String Dim i As Long StrFind = "∂,∆,π,lee " StrRepl = "de,trang,P,loo" Set RngTxt = Selection.Range For i = 0 To UBound(Split(StrFind, ",")) Selection.HomeKey wdStory Selection.Find.ClearFormatting With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = Split(StrFind, ",")(i) .Replacement.Text = Split(StrRepl, ",")(i) .Format = False .MatchWholeWord = True .MatchAllWordForms = False .MatchWildcards = False .Execute Replace:=wdReplaceAll End With Next i End Sub
Developers Jos Posted September 16, 2017 Developers Posted September 16, 2017 6 hours ago, viptnn said: I found VBA code maybe do work, if could pls translate to autoit thanks. What did you try as the code should look pretty similar, or did you want to pay me for doing it for you? Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
water Posted September 16, 2017 Posted September 16, 2017 Did you have a look at the Word UDF that comes with AutoIt? 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
viptnn Posted September 16, 2017 Author Posted September 16, 2017 1 hour ago, Jos said: What did you try as the code should look pretty similar, or did you want to pay me for doing it for you? Jos i could to pay you if you have time to help me complete my program.
Developers Jos Posted September 16, 2017 Developers Posted September 16, 2017 Just now, viptnn said: i could to pay you if you have time to help me complete my program. Doubt that So you better take the cheap option and give the suggestions a try yourself. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
viptnn Posted September 16, 2017 Author Posted September 16, 2017 1 hour ago, water said: Did you have a look at the Word UDF that comes with AutoIt? i looked but no func can replace multi word at same time, i have a lot of word need to replace so i want replace all as fast as well
viptnn Posted September 16, 2017 Author Posted September 16, 2017 (edited) 8 minutes ago, Jos said: Doubt that So you better take the cheap option and give the suggestions a try yourself. Jos Thank Jos, pls show the suggestions, i ll try myself Edited September 16, 2017 by viptnn
water Posted September 16, 2017 Posted September 16, 2017 With multiple words to replace I would call _Word_DocFindReplace in a loop. 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
viptnn Posted September 16, 2017 Author Posted September 16, 2017 3 minutes ago, water said: With multiple words to replace I would call _Word_DocFindReplace in a loop. i known that way but i need to give an example for you to understand _Word_DocFindReplace($oDoc,"word1","one") It take 0.5 second, very fast alright? but if we loop to 100-1000 time, it take 50-500 second too long if we compare with vba script
water Posted September 16, 2017 Posted September 16, 2017 Did you try to translate the VBA code to AutoIt? 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
viptnn Posted September 16, 2017 Author Posted September 16, 2017 1 minute ago, water said: Did you try to translate the VBA code to AutoIt? No, i didn't i study only Autoit code :(, if you could pls help me, i dont want my program slower than VBA code
water Posted September 16, 2017 Posted September 16, 2017 Something for you to start with: #include <Word.au3> Global $oWord ; Set Word application object here and assign object Global $oDoc ; Open Word document here and assign object Func MultiReplace() Local $aFind = StringSplit("∂,∆,π,lee", ",") Local $aReplace = StringSplit("de,trang,P,loo", ",") Local $oRange = $oWord.Selection.Range For $i = 1 To $aFind[0] $oWord.Selection.HomeKey($wdStory) With $oWord.Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = $aFind[$i] .Replacement.Text = $aReplace[$i] .Format = False .MatchWholeWord = True .MatchAllWordForms = False .Replace($wdReplaceAll) .MatchWildcards = False .Execute() EndWith Next EndFunc ;==>MultiReplace 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
neypro Posted June 8, 2020 Posted June 8, 2020 On 9/16/2017 at 5:21 PM, water said: Something for you to start with: #include <Word.au3> Global $oWord ; Set Word application object here and assign object Global $oDoc ; Open Word document here and assign object Func MultiReplace() Local $aFind = StringSplit("∂,∆,π,lee", ",") Local $aReplace = StringSplit("de,trang,P,loo", ",") Local $oRange = $oWord.Selection.Range For $i = 1 To $aFind[0] $oWord.Selection.HomeKey($wdStory) With $oWord.Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = $aFind[$i] .Replacement.Text = $aReplace[$i] .Format = False .MatchWholeWord = True .MatchAllWordForms = False .Replace($wdReplaceAll) .MatchWildcards = False .Execute() EndWith Next EndFunc ;==>MultiReplace can't you help me?, It does not work
Developers Jos Posted June 8, 2020 Developers Posted June 8, 2020 1 minute ago, neypro said: It does not work Means what exactly? "It doesn't work" does not tell us anything! I hope the posted script isn't the whole script you are trying because that indeed doesn't do anything as that snippet never calls MultiReplace()! Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
neypro Posted June 8, 2020 Posted June 8, 2020 (edited) 13 minutes ago, Jos said: Means what exactly? "It doesn't work" does not tell us anything! I hope the posted script isn't the whole script you are trying because that indeed doesn't do anything as that snippet never calls MultiReplace()! Jos This is my code: expandcollapse popup#include <Word.au3> Dim $aFind[100] $FILE = FileOpen(@ScriptDir&"\Data\1.txt", 0+32) for $i=1 to 99 step 1 $aFind[$i]=FileReadLine($FILE, $i) Next FileClose($FILE) Dim $aReplace[100] $FILE = FileOpen(@ScriptDir&"\Data\2.txt", 0+32) for $i=1 to 99 step 1 $aReplace[$i]=FileReadLine($FILE, $i) Next FileClose($FILE) $oWord = _Word_Create(True,False) Local $sDocument = @ScriptDir&"\1.doc" $oDoc= _Word_DocOpen($oWord, $sDocument) Local $oRange = $oWord.Selection.Range For $i = 1 To 99 $oWord.Selection.HomeKey($wdStory) With $oWord.Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = $aFind[$i] .Replacement.Text = $aReplace[$i] .Format = False .MatchWholeWord = True .MatchAllWordForms = False .Replace($wdReplaceAll) .MatchWildcards = False .Execute() EndWith Next Hope you help me Edited June 8, 2020 by Jos
Developers Jos Posted June 8, 2020 Developers Posted June 8, 2020 (edited) You still haven't described what you want to do and what is going wrong ......or do you want me to figure that all out myself? Also provide all required bits and pieces so we can test. Edited June 8, 2020 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
neypro Posted June 8, 2020 Posted June 8, 2020 7 minutes ago, Jos said: You still haven't described what you want to do and what is going wrong ......or do you want me to figure that all out myself? My code does not perform the replacement operation according to the predefined pattern.I don't know VBA but I am trying to test the speed when using VBA
Developers Jos Posted June 8, 2020 Developers Posted June 8, 2020 4 minutes ago, Jos said: Also provide all required bits and pieces so we can test. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
neypro Posted June 8, 2020 Posted June 8, 2020 8 minutes ago, Jos said: I don't use fixed keywords, the .txt file only contains random keywords that I added
Developers Jos Posted June 8, 2020 Developers Posted June 8, 2020 Don't really care, just need an example document and the 2 examples files to do testing when you want any help. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
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