none
Find and Replace problem in word RRS feed

  • Question

  • when hidden text shown

    When hidden text are hide

    The word document contain both normal text and hidden text, while i am trying to find the text by hiding the hidden text(in View Option). 

    I am unable to find my decide find text.
    i am attached screen shot for your reference.

    text i am used to find(when hidden text are hide)
    Clare.15 This connection is also evident in
    Result: Problem

    text i am used to find(when hidden text are shown)
    Clare.<xref rid="ftn15"><super>15</super></xref> This connection is also evident in
    Result: No Problem

    Thursday, October 10, 2013 9:04 AM

Answers

  • Well, in a test I found I could delete the hidden text using Find/Replace. Then, of course, searching the search text works. That made me wonder if something along these lines would work for you:

    1. Designate a section of the document (paragraph, for example) as the search range.

    2. Run a Find/Replace on that range to remove all hidden text.

    3. Run a Find to find the text you want to check against the database. Assign this to a Range object so that it can't "get lost"

    4. Now UNDO to bring the hidden text back.

    5. At this point, in my test the Range contains the found text AND the hidden text, but it possible to continue to ignore the hidden text using TextRetrievalMode.

    Here's some VBA code that illustrates the principle. You select some text in the document you want to process. Running the macro will copy that selection to a new document, remove the hidden text, FIND the string, mark that Range, reinstate the deleted hidden text, then mark the Range again.

    Sub FindWithoutHiddenText()
        Dim rngOriginal As word.Range, rngFind As word.Range
        Dim newDoc As word.Document
        Dim rngEdit As word.Range
        
        Set rngOriginal = Selection.Range
        Set newDoc = Documents.Add
        Set rngFind = newDoc.content
        Set rngEdit = newDoc.content
       rngFind.TextRetrievalMode.IncludeHiddenText = False
       Debug.Print rngFind.Text
       rngEdit.FormattedText = rngOriginal.FormattedText
       rngEdit.Find.ClearFormatting
       With rngEdit.Find
        .Format = True
        .Font.Hidden = True
        .Text = ""
        .Execute Replace:=wdReplaceAll
       End With
       
        rngFind.Find.ClearFormatting
        rngFind.Find.Replacement.ClearFormatting
        
        With rngFind.Find
            .Text = "Clare.15 This connection is also evident in"
            .Replacement.Text = ""
            .Forward = True
            '.wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Debug.Print (.Execute)
        End With
        rngFind.Select
        newDoc.Undo
        rngFind.Select
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 17, 2013 6:40 PM
    Moderator

All replies

  • You could tick the check box "Match wildcards" and search for Clare.*15

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    Thursday, October 10, 2013 10:32 AM
  • Are you using code for this, or working in the user interface?

    If code (and since this is programming forum, we would expect that), please show us the code.

    If you're working in the user interface, the better place to ask would be the "Communities", where you'll find a lot more people who work with Word the way you're doing:

    http://answers.microsoft.com/en-us/office/forum/word

    Unfortunately, we're not able to move discussions from the MSDN developer forums to the Communities, so you'd need to re-post.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 12:53 PM
    Moderator
  • ya you correct, in this occurrence it is possible. But  some time I am unable to tell where the hidden text will occur on that time what can I do.
    Thursday, October 10, 2013 2:53 PM
  • ya i am using VBA only to find the text in the document.
    Thursday, October 10, 2013 2:54 PM
  •    Selection.HomeKey Unit:=wdStory
        Selection.find.ClearFormatting
        With Selection.find
            .Text = "Clare.15 This connection is also evident in"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.find.Execute

    Thursday, October 10, 2013 3:07 PM
  • In that case, it might help us guide you if you could show us the basic syntax you're using to Find.

    Mainly, I'm concerned whether you're using the Range.Find or Selection.Find variation.

    If you use a Range object, then you can leverage the TextRetrievalMode.IncludeHiddenText property. Set it to False to ignore hidden text in the Range and then execute the Find.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 3:07 PM
    Moderator
  • Try something more like this (untested):

       Dim rngFind as Word.Range
       Set rngFind = ActiveDocument.Content
       rngFind.TextRetrievalMode.IncludeHiddenText = false
        rngFind.find.ClearFormatting
        With rngFind.find
            .Text = "Clare.15 This connection is also evident in"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        rngFind.find.Execute


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 10, 2013 3:47 PM
    Moderator
  • Hi

    The above code is not working.

    But we have tried selection.Find.Execute("Clare.15") is working.

    Text used to find:

    "Clare.<xref rid="ftn15"><super>15</super></xref> This connection is also evident in map"

    It ignores only upto open tag,  remaining it wont accept.

    what is the problem?

    Friday, October 11, 2013 8:30 AM
  • I don't understand. You don't explain accurately.

    1. HOW does the code I provided not work? Please be specific

    2. You state "we have tried selection.Find.Execute("Clare.15") is working", but then you say "what is the problem?" Please provide more detail about what is working and what is not.

    Note: You may need to bring someone into this discussion with better language skills...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 11, 2013 4:04 PM
    Moderator
  • Hi Cindy,

    Sorry for the lack of knowledge.

    This is the sample text content in the document, hidden text are pink colored:

    Text show with hidden text:

    "Clare.<xref rid="ftn15"><super>15</super></xref> This connection is also evident in map"

    Text without hidden text (hidden text are hide by Tools->Option->View):

    "Clare.15 This connection is also evident in map"


    1. I was tried selection.Find.Execute("Clare.15") or your code is working.


    In this above hidden text “<xref rid="ftn15"><super>” present in between “Clare.” And “15” but it’s working or I am able to find the text("Clare.15").


    1. I was tried selection.Find.Execute("15 This") or your code is working.


    In this above hidden text “</super></xref>” present in between “15” And “ This” but it’s working or I am able to find the text("15 This").

     

    Problem:

    If I am try to find Text (Clare.15 This) or (Clare.15 This connection is also evident in) is not working.

     

    That’s my problem.

     

    Do you need any more information?

     

    Note:

    As per my knowledge the word will ignore the hidden text present only ONE occurrence in between two normal text that is only one occurrence(hidden text) in the given string.

    If hidden text present in multiple location in the given string it won’t ignore or word can’t find the text given text.

    Better if you have time please try yourself,

    Thanks for you kind patience response.




    Tuesday, October 15, 2013 4:44 AM
  • OK, I've looked at this a bit and see that I can get the full text, without hidden text, using the TextRetrievalMode...

    What do you need to do with this, once you've found it?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 15, 2013 5:58 PM
    Moderator
  • Yes Cindy, you are correct, it’s working fine in the Word 2010, but it’s not working in word 2000.

    That is the problem, So I think there is problem in Word 2000.

    But we are using Word 2000, So we need to fix this problem in Word 2000.

    Is there is any possibility in Word 2000?

    Thanks for you valuable reply.


    Regards

    Thirusanguraja Venkatesan

    Wednesday, October 16, 2013 1:09 PM
  • Please answer my QUESTION.

    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, October 16, 2013 1:25 PM
    Moderator
  • After finding, I want to compare the found string with string in my Database, if any changes present I will make correction in the document.

    The checking will be done word by word.

    In the checking process I will remove or add some word or char into the document based on the changes

    Thursday, October 17, 2013 8:00 AM
  • Well, in a test I found I could delete the hidden text using Find/Replace. Then, of course, searching the search text works. That made me wonder if something along these lines would work for you:

    1. Designate a section of the document (paragraph, for example) as the search range.

    2. Run a Find/Replace on that range to remove all hidden text.

    3. Run a Find to find the text you want to check against the database. Assign this to a Range object so that it can't "get lost"

    4. Now UNDO to bring the hidden text back.

    5. At this point, in my test the Range contains the found text AND the hidden text, but it possible to continue to ignore the hidden text using TextRetrievalMode.

    Here's some VBA code that illustrates the principle. You select some text in the document you want to process. Running the macro will copy that selection to a new document, remove the hidden text, FIND the string, mark that Range, reinstate the deleted hidden text, then mark the Range again.

    Sub FindWithoutHiddenText()
        Dim rngOriginal As word.Range, rngFind As word.Range
        Dim newDoc As word.Document
        Dim rngEdit As word.Range
        
        Set rngOriginal = Selection.Range
        Set newDoc = Documents.Add
        Set rngFind = newDoc.content
        Set rngEdit = newDoc.content
       rngFind.TextRetrievalMode.IncludeHiddenText = False
       Debug.Print rngFind.Text
       rngEdit.FormattedText = rngOriginal.FormattedText
       rngEdit.Find.ClearFormatting
       With rngEdit.Find
        .Format = True
        .Font.Hidden = True
        .Text = ""
        .Execute Replace:=wdReplaceAll
       End With
       
        rngFind.Find.ClearFormatting
        rngFind.Find.Replacement.ClearFormatting
        
        With rngFind.Find
            .Text = "Clare.15 This connection is also evident in"
            .Replacement.Text = ""
            .Forward = True
            '.wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        Debug.Print (.Execute)
        End With
        rngFind.Select
        newDoc.Undo
        rngFind.Select
    End Sub
    


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 17, 2013 6:40 PM
    Moderator
  • Ya Thanks Cindy, done a good job

    Your concept is very nice to practice.

    Note:

    There is one more bit correction in your code that is we need to add 

    newDoc.Content = rngOriginal.text

    after new document creation

    Code is working fine.


    Thanks & Regards

    Thirusanguraja Venkatesan



    Friday, October 18, 2013 9:31 AM