Jump to content
Sign in to follow this  
CE101

Word: Using "FIND.EXECUTE"

Recommended Posts

CE101

FIND.EXECUTE is a Word Method that does the same thing as /Edit/Find.

See http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.find.execute.aspx

I need some help in getting to to work in AutoIT.

My program is trying to modify a Word doc which contains a two column table that looks like this.

City    # of Members
    Atlanta 175
    Boston  123
    Chicago 137

It searches for a value in column A and if it's found it will modify the corresponding value in column B. For example, we search for the word "Boston" and replace the number 123 with the number 199.

In VBA it's done as follows:

Selection.PasteExcelTable False, False, False
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "Boston"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="199"

However I can’t seem to do it properly in AutoIT. Here's what I've tried.

With $oWordDoc.Range.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            $Return4 = .Execute("Boston")       ; ReturnCode will be True
    EndWith
    MsgBox(0, "Case3", "Just before MoveRight... It seems that we are outside the table. The FIND return code was " & $Return4 )
    $oWordApp.Selection.MoveRight(12)         ; Units = 12,  ie move to the next cell
    $oWordApp.Selection.TypeText("199")

The FIND return code shows that the word Boston is present in the file. However the new number (199) gets written after the table, not inside it (in column :). The Msgbox shows that the cursor is no longer positioned at the word Boston when the MoveRight operation begins. That's why the data ends up outside the table.

If you would like to experiment with this, here's a script containing test data. The table is created in Excel and then copied into Word. This script includes some of the other things I've tried as well..

AutoItSetOption("MustDeclareVars", 0)   ; no need to declare vars
    #include <Debug.au3>      ; included at top of pgm
    _DebugSetup ("" & ":  Debugger")
    ; _DebugReportVar("... $var2", $var2)
    #include <Excel.au3>
    #include <Word.au3>

    ;------------------------
    ; Step1:  Create test data in Excel and copy to Word
    ;------------------------
    Local $oExcel = _ExcelBookNew() ;Create new book, make it visible
    Local $aArrayA[2] = ["City", "# of Members"]
    Local $aArrayB[2] = ["Atlanta", "175"]
    Local $aArrayC[2] = ["Chicago", "137"]
    Local $aArrayD[2] = ["Boston", "123"]

    _ExcelWriteArray($oExcel, 1,   1, $aArrayA)
    _ExcelWriteArray($oExcel, 2,   1, $aArrayB)
    _ExcelWriteArray($oExcel, 3,   1, $aArrayC)
    _ExcelWriteArray($oExcel, 4,   1, $aArrayD)


    ; Sort randomly
    If Random() < 0.5 Then  ; Returns a value between 0 and 1.
        $oExcel.ActiveSheet.Range("A2:B4").Sort($oExcel.ActiveSheet.Range("A2"),1 )       
    Else
        $oExcel.ActiveSheet.Range("A2:B4").Sort($oExcel.ActiveSheet.Range("B2"),1 )     
    Endif

    ; Copy to pasteboard
    $oExcel.ActiveSheet.Cells.Select
    $oExcel.Application.Selection.Copy
    $oExcel.ActiveSheet.Range("A1").Select      ; So that the entire sheet is no longer selected


    ; Create empty Word doc
    $oWordApp = _WordCreate("")
    $oWordDoc = _WordDocAdd($oWordApp)


    ;------------------------
    ; Step2:  Manipulate the data in Word
    ;------------------------

    $FindText     = "Boston" 
    $ReplaceText  = "199" 
    Dim $wdUnit
    $wdCell       = 12
    $wdUnit  = $wdCell

    ; Paste in the text from Excel
    $oWordApp.Selection.PasteExcelTable(False, False, False)


    ; CASE1:  The following sequence crashes at Selection.Find, regardless whether it's appended to $oWordApp or to $oWordDoc
    If 0 <> 0    Then     ; bypass  
                $oWordApp.Selection.ClearFormatting
                $oWordApp.Selection.Find($FindText)      ; crashes
                ; $oWordDoc.Selection.Find($FindText)    ; crashes
                $oWordApp.Selection.Find.Execute
                $oWordApp.Selection.MoveRight()
                $oWordApp.Selection.TypeText($ReplaceText) 
    Endif   ;   If 0 <> 0    Then     ; bypass  


    ; CASE2:  The following sequence (using a With statement) does not crash. 
    ;         However the $ReplaceText (199) gets written after the table, not inside it. 
    $oWordApp.Selection.ClearFormatting
    With $oWordDoc.Range.Find
            .Text = "????" 
    EndWith
    $Return1 = $oWordApp.Selection.Find.Execute     ; ReturnCode will be False
    With $oWordDoc.Range.Find
            .Text = "Boston" 
    EndWith
    $Return2 = $oWordApp.Selection.Find.Execute     ; ReturnCode will be False
    $oWordApp.Selection.MoveRight()          ; doesn't crash, but cursor ends up outside the table
    ; $oWordApp.Selection.MoveRight(Default, Default, Default)  ; doesn't crash, but cursor ends up outside the table
    ; $oWordApp.Selection.MoveRight($wdUnit)   ; crashes - probably bec we are outside the table and therefore can't use Cell for Units
    ; $oWordApp.Selection.MoveRight(12, Default, Default)  ; crashes - same as above
    $oWordApp.Selection.TypeText($ReplaceText) 


    ; CASE3:  The following sequence (using a With statement) does not crash. 
    ;           The With statements show that Find.Execute will succeed in it's search for the word Boston 
    ;           (but will not succeed searching for the text "????"). 
    ;           Nevertheless, even if it finds the word Boston, the $ReplaceText (199) gets written after the table, not inside it. 
    With $oWordDoc.Range.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            $Return3 = .Execute("????")     ; ReturnCode will be False
    EndWith         
    With $oWordDoc.Range.Find
            .ClearFormatting()
            .Replacement.ClearFormatting()
            $Return4 = .Execute($FindText)      ; ReturnCode will be True
    EndWith
            MsgBox(0, "Case3", "Just before MoveRight... It seems that we are outside the table. The FIND return code was " & $Return4 )
    $oWordApp.Selection.MoveRight()
    $oWordApp.Selection.TypeText($ReplaceText) 

                        _DebugReportVar("$Return1 ... ", $Return1)
                        _DebugReportVar("$Return2 ... ", $Return2)
                        _DebugReportVar("$Return3 ... ", $Return3)
                        _DebugReportVar("$Return4 ... ", $Return4)

Any suggestions would be greatly appreciated. Sample code would be even better.

Share this post


Link to post
Share on other sites
MrMitchell

Something like this should work as long as your Word option "Typing Replace Text" is Enabled...

Global Const $wdMove = 0
$FindText   = "Boston"
$ReplaceText  = "199"
With $oWordApp.Selection.Find
    .Forward = True
    .ClearFormatting
    .Wrap = 1   ;.Wrap could be $wdFindContinue which is 1
    .Execute($FindText)
EndWith

$oWordApp.Selection.MoveRight($wdCell, 1, $wdMove)  ;$wdMove = 0, needs to be declared or just replace with Default or 0.
$oWordApp.Selection.TypeText($ReplaceText)

Here's my reference: http://msdn.microsoft.com/en-us/library/aa213125%28v=office.11%29.aspx

Share this post


Link to post
Share on other sites
CE101

Something like this should work as long as your Word option "Typing Replace Text" is Enabled...

Global Const $wdMove = 0
$FindText   = "Boston"
$ReplaceText  = "199"
With $oWordApp.Selection.Find
    .Forward = True
    .ClearFormatting
    .Wrap = 1   ;.Wrap could be $wdFindContinue which is 1
    .Execute($FindText)
EndWith

$oWordApp.Selection.MoveRight($wdCell, 1, $wdMove)  ;$wdMove = 0, needs to be declared or just replace with Default or 0.
$oWordApp.Selection.TypeText($ReplaceText)

Here's my reference: http://msdn.microsoft.com/en-us/library/aa213125%28v=office.11%29.aspx

Share this post


Link to post
Share on other sites
CE101

Thank you for the help.

Works fine now.

Share this post


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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.