Click here to Skip to main content
13,044,935 members (46,914 online)
Rate this:
 
Please Sign up or sign in to vote.
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 18-Oct-10 19:49pm
Updated 18-Oct-10 20:13pm
v4

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
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.
dxjhtian 23-Oct-10 6:41am
   
sorry, the text above has a error: "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." should be "After found each matched text, I detect that whether the range of the matched text is beyond the original selection range, if true, the search will be stop."
E.F. Nijboer 24-Oct-10 14:28pm
   
Before you solved it you used Selection.Range and I think the reason that it didn't work was related to the above. You solved it by implementing it in another way that worked but I was just thinking that it might be the reason why it first didn't work. :-)
dxjhtian 24-Oct-10 20:14pm
   
In fact, the code I listed at top will work correct(show "True") sometimes. I cannot find why the results are different. May be I can find the reason in future, and if I found, I will post at here.
E.F. Nijboer 26-Oct-10 10:54am
   
And if the selection is first stored into a temp range variable and that is used? Maybe that would just make a difference.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 19 Oct 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100