Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found an interesting thing:

In word 2010, select some text, and run the following VBA code:

public Sub Test()
    With Selection.Range.Find
        MsgBox .Execute(Selection.Range.text) 
        MsgBox .Found
    End With
End Sub


Both the two message box say "False", but both should be "True". Why?

Thanks a lot for your suggestion.
Posted
Updated 18-Oct-10 20:13pm
v4

1 solution

Try to add ClearFormatting, like this:
public Sub Test()
    Selection.Range.Find.ClearFormatting()
    With Selection.Range.Find
        MsgBox .Execute(Selection.Range.text) 
        MsgBox .Found
    End With
End Sub

http://msdn.microsoft.com/en-us/library/f1f367bx%28VS.80%29.aspx[^]

Good luck!
 
Share this answer
 
Comments
dxjhtian 19-Oct-10 20:49pm    
Thank you. In my really code, the ClearFormatting() methods are included, I do find only the text without any formatting. But the Find cannot work correctly.
E.F. Nijboer 20-Oct-10 5:04am    
I do not completely understand your response. Do you mean it is working when adding ClearFormatting and without is doesn't?
dxjhtian 23-Oct-10 0:54am    
Sorry for my poor English. Calling the ClearFormatting() method or not will not change the result. The code I list at top is simplified, in my real code, the ClearFormatting() is included. The problem is, if the range for finding in is exactly same as the finding text, the result is error. I solved the problem partly by searching in the range from Selection.Range.Start to Activedocument.Range.End, and by comparing the range of the found text between the original selection range I can stop searching.
E.F. Nijboer 23-Oct-10 5:08am    
I did some additional searching for you because I simply couldn't believe that the text cannot be found even when it is an exact match! The reason I think is causing your problem is in a very little detail that can indeed be easily overlooked. You can either use Selection.Find or Range.Find to do a search. In case of Selection.Find the selection is changed to the match found. In case of Range.Find the selection in the document itself will be unchanged. In your case you are using the range of the selection and this is where I think it goes wrong because the selection itself should not change but the range of the selection will now point to the matching text.
If you would so Selection.Find instead of Selection.Range.Find. I think you could also first store the Selection.Range in a temporary variable and use that instead of the Selection.Range.

The link I found that gave me this idea: http://msdn.microsoft.com/en-us/library/bb211938%28v=office.12%29.aspx

Well, I'm really curious if this would solve your problem and hopefully you let me know :-)
dxjhtian 23-Oct-10 6:39am    
Thank you very much, and I'm very happy for someone love programming with VBA like me. I think the problem is not relative to the Selection. As I said above, instead of searching in the Selection.Range, I search in :

Set realFindingRange = ActiveDocument.Range(Selection.Range.Start, ActiveDocument.Range.End)

By extending the searching range to end of the active document, in most case the finding text will not match exactly with the finding range. After found each matched text, I detect that whether the range of the matched text is beyond the "realFindingRange" range, if true, the search will be stop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900