Jump to content

Need to help replace multipe word in MS Word


Recommended Posts

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

 

Link to post
Share on other sites
  • Developers
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.
  :)

Link to post
Share on other sites

Did you have a look at the Word UDF that comes with AutoIt?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites
  • Developers
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.
  :)

Link to post
Share on other sites

With multiple words to replace I would call _Word_DocFindReplace in a loop.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites
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

Link to post
Share on other sites

Did you try to translate the VBA code to AutoIt?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites

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 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2021-06-14 - Version 1.6.5.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
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2021-07-20 - Version 1.0.0.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Link to post
Share on other sites
  • 2 years later...
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

Link to post
Share on other sites
  • Developers
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.
  :)

Link to post
Share on other sites
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:

#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 by Jos
Link to post
Share on other sites
  • Developers

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 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.
  :)

Link to post
Share on other sites
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

Link to post
Share on other sites
  • Developers

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.
  :)

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...