Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range.Find.Execute fails in word 2013

Consider the following table in Word 2013

A BBB
A CCC
D E F

A, B and C are merged cells.

A and B are empty. C has text A inside.

Now the following code

Set rng = ActiveDocument.Range(0, 0)  
With rng.Find
    .Forward = True
    .Wrap = wdFindStop
    .Execute "A"
End With

Crashes word on Execute "A".

If I change text to Execute "B" it doesn't find anything but doesn't crash word. Issue is present only in word 2013.

We tried searching manually and Selection.Find cell by cell, but both of those are rather slow.

Is there a quick way to circumvent this error?

EDIT: this is minimum fail example that I constructed. In our application we use a lot of Range.Find, sometimes with wrap and almost never starting from Document.Start

EDIT2: further investigation shows that Error isn't present if you open Document in compatibility mode (Word 97-2003 format).

like image 937
kilotaras Avatar asked Nov 10 '22 10:11

kilotaras


1 Answers

And you can't just activate a .Find object off the Tables(index).Selection object rather than the range object?

If you are just activating the .Selection object off of the ActiveDocument instead of the Table object then, yeah that could take forever. But coming directly off the table index will speed it up significantly.

ActiveDocument.Tables(1).Select
With Selection.Find
    .Forward = True
    .Wrap = wdFindStop
    .Execute FindText:="A"
End With
like image 88
Rich Avatar answered Nov 14 '22 23:11

Rich