locked
Problem using Find object to search for strings containing any kind of characters. RRS feed

  • Question

  • Hi,

    I'm developing a method that is supposed to use Find object of a Range to find and replace sentences in a Word file. The main point about the method is that it receives strings that may contain any kind of special characters and also characters like paragraph mark (that is being search by "^p" using find/replace dialog) and tab space (which also is being searched by "^t"). The method is supposed to find the complete input string and replace it by the other input. As I know, characters like paragraph mark should be replaced by "^p" and special characters should be replaced by "^0[character code]" before being used as input to Find.Execute method. But I thought that there should be an easier way to do the job instead of replacing all of these special characters in the input sentence. Could anyone help me with the issue, please?

    Regards,

    Hamidreza Ghader

    Thursday, October 28, 2010 12:48 PM

Answers

  • In fact if your search string contains a chr(13) character, the search will find Word paragraph marks (as you might hope). And if it contains a chr(11) character, it will find a "hard line return" character. And if it contains a Unicode character (e.g. the CJK character 0x92CC), then a Unicode string that contains that character will match the character in the text - i.e. you do not need to use ^p, ^l and so on to match those characters if you already have the characters appropriately coded. Using them in /replace/ text is not necessarily such a good idea. 

    However, I have been wondering why you need to search for text when (if I have understood your requirement correctly) you already know where that text is, or at least, you /could/ know if you changed the way your application worked.

    AFAICS you are starting with a text, copying the entire text to a new document, then taking each sentence in turn, translating it and replacing the original sentence in the new document. If you are doing that sequentially, it isn't completely clear why you do not already know the location of the text that you need to replace. OK, if you are doing the translations in parallel somehow, then the text might have moved by the time that the translation returns, but in that case, why not mark each original sentence uniquely (e.g. with a sequence-numbered bookmark) before you send it for translation. You must be able to mark each translation request with that unique sentence identifier, and then all you would need to do is locate the bookmark (or whatever) and replace its contents by the translation.

    Just my 2c-worth!

     


    Peter Jamieson
    • Marked as answer by Hamidreza G Thursday, November 4, 2010 11:57 AM
    Sunday, October 31, 2010 9:30 AM

All replies

  • Without information as to what strings you are finding it is not possible to be specific, but the wildcard search in Word is explained at http://www.gmayor.com/replace_using_wildcards.htm


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Thursday, October 28, 2010 1:29 PM
  • Hi, Thank you for the reply, but I don't think that the wildcards could help me with the issue. At least I can't figure out how it may help. Let me explain a little more about the problem. I am developing a class that manages "Word" files for a Translator software. It reads sentences of a readonly "Word" file and sends them one by one to a translator engine. The engine gives back the original sentence and its translation. Then the class tries to find the right place of the original sentence in a writable copy of the original file. Then it selects the Range and replaces the text with the translation. This is being done without use of Find object of the Word object model, now. But it does not work well. I hope using "Find" object it could be enhanced. But I need help with it. Thanks for spending time to read this long explanation. Regards,
    Friday, October 29, 2010 6:45 AM
  • I included the link because it contains a reference to all the special characters used in Word's find and replace function.
    I don't know if it will help, but in Word vba you might use something like

    Dim strText As String, strNewText As String
    Dim oRng As Range
    strText = "Your Text to find"
    strNewText = "This is the replacement text"
    Set oRng = ActiveDocument.Range
    With oRng.Find
        Do While .Execute(FindText:=strText, Forward:=True)
            oRng.Text = strNewText
            'oRng.Collapse wdCollapseEnd
        Loop
    End With

    so by applying your original text string to strText and the translation to strNewText, the above will find the first instance of the text and replace it.
    If you remove the apostrophe from 'oRng.Collapse wdCollapseEnd it will replace all instances.



    <Hamidreza G> wrote in message news:f5671727-623e-43d4-beaf-39a631ae1cee@communitybridge.codeplex.com...

    Hi, Thank you for the reply, but I don't think that the wildcards could help me with the issue. At least I can't figure out how it may help. Let me explain a little more about the problem. I am developing a class that manages "Word" files for a Translator software. It reads sentences of a readonly "Word" file and sends them one by one to a translator engine. The engine gives back the original sentence and its translation. Then the class tries to find the right place of the original sentence in a writable copy of the original file. Then it selects the Range and replaces the text with the translation. This is being done without use of Find object of the Word object model, now. But it does not work well. I hope using "Find" object it could be enhanced. But I need help with it. Thanks for spending time to read this long explanation. Regards,


    Graham Mayor - Word MVP
    www.gmayor.com
    Posted via the Communities Bridge
    http://communitybridge.codeplex.com/
    Friday, October 29, 2010 12:13 PM
  • Hi again,

    Thank you for the code included, but I knew how to call Find object for searching a string. The main problem with using Find object, as I mentioned in my first post, is that the sentence to be searched may contains characters like paragraph mark, which should be replaced by "^p" in order to be searched using Find object. I want to know whether there is any method to do the search using Find object without replacing these special characters.

    As you know if these special characters have not been replaced in the sentence to be searched, the search would not be successful.

    Regards,

    Hamidreza Ghader
    Sunday, October 31, 2010 8:36 AM
  • In fact if your search string contains a chr(13) character, the search will find Word paragraph marks (as you might hope). And if it contains a chr(11) character, it will find a "hard line return" character. And if it contains a Unicode character (e.g. the CJK character 0x92CC), then a Unicode string that contains that character will match the character in the text - i.e. you do not need to use ^p, ^l and so on to match those characters if you already have the characters appropriately coded. Using them in /replace/ text is not necessarily such a good idea. 

    However, I have been wondering why you need to search for text when (if I have understood your requirement correctly) you already know where that text is, or at least, you /could/ know if you changed the way your application worked.

    AFAICS you are starting with a text, copying the entire text to a new document, then taking each sentence in turn, translating it and replacing the original sentence in the new document. If you are doing that sequentially, it isn't completely clear why you do not already know the location of the text that you need to replace. OK, if you are doing the translations in parallel somehow, then the text might have moved by the time that the translation returns, but in that case, why not mark each original sentence uniquely (e.g. with a sequence-numbered bookmark) before you send it for translation. You must be able to mark each translation request with that unique sentence identifier, and then all you would need to do is locate the bookmark (or whatever) and replace its contents by the translation.

    Just my 2c-worth!

     


    Peter Jamieson
    • Marked as answer by Hamidreza G Thursday, November 4, 2010 11:57 AM
    Sunday, October 31, 2010 9:30 AM
  • Hi Hamidreza,

    I'm writing to check the status of the thread. Could you please let me know if you have solved the problem?

    Best Regards,
    Amy Li
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Wednesday, November 3, 2010 3:04 AM
  • Hi Amy,

    Thanking Peter for his suggestions, I did the job.

    Regards,

    Hamidreza Ghader

     

    Thursday, November 4, 2010 12:01 PM