none
If Selection.Find.Text <> "this check box" does not work RRS feed

  • Question

  • The Selection.Find.Text <> "this check box" condition is not working in the macro for the following code snippet. I am trying to find phrases ending with <g class="gr_ gr_235 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del gr-progress" data-gr-id="235" id="235"><g class="gr_ gr_236 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="236" id="236">check</g> box</g> that is not formatted where the phrase is not "this check box". 

        Selection.Find.Font.Bold = False
        With Selection.Find.Font
            .Bold = False
        End With
        With Selection.Find
            .Text = "^? check box"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = True
            .MatchWholeWord = True
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Do While Selection.Find.Execute
        'If Selection.Find.Text = "this check box" Then
       Selection.Comments.Add Range:=Selection.Range
       Selection.TypeText Text:= _
    "If this is the proper name of a check box, make sure it is in bold."
       ActiveWindow.ActivePane.Close
       'End If
       Loop

    Wednesday, May 23, 2018 8:42 AM

Answers

  • There are multiple 'issues' with your code. Try:

    Sub Demo()
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<*> check box"
        .Replacement.Text = ""
        .Forward = False
        .Format = True
        .Font.Bold = False
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found = True
        If .Duplicate.Text <> "this check box" Then
          .Comments.Add Range:=.Duplicate, Text:= _
            "If this is the proper name of a check box, make sure it is in bold."
        End If
        .Collapse wdCollapseStart
        .Find.Execute
       Loop
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]




    • Edited by macropodMVP Wednesday, May 23, 2018 9:56 PM Code Revision
    • Proposed as answer by Terry Xu - MSFT Thursday, May 24, 2018 2:38 AM
    • Marked as answer by pragram Friday, May 25, 2018 6:15 AM
    Wednesday, May 23, 2018 10:22 AM

All replies

  • There are multiple 'issues' with your code. Try:

    Sub Demo()
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "<*> check box"
        .Replacement.Text = ""
        .Forward = False
        .Format = True
        .Font.Bold = False
        .Wrap = wdFindStop
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found = True
        If .Duplicate.Text <> "this check box" Then
          .Comments.Add Range:=.Duplicate, Text:= _
            "If this is the proper name of a check box, make sure it is in bold."
        End If
        .Collapse wdCollapseStart
        .Find.Execute
       Loop
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]




    • Edited by macropodMVP Wednesday, May 23, 2018 9:56 PM Code Revision
    • Proposed as answer by Terry Xu - MSFT Thursday, May 24, 2018 2:38 AM
    • Marked as answer by pragram Friday, May 25, 2018 6:15 AM
    Wednesday, May 23, 2018 10:22 AM
  • Hi Paul,

    Thanks for the reply. I tried using your code snippet, but the <g class="gr_ gr_86 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" data-gr-id="86" id="86">If .</g>Text <> "this check box" Then is not working as expected. It is commenting for all occurrences of the "this check box" phrase.

    Wednesday, May 23, 2018 4:20 PM
  • I've revised the code to force it to search backwards through the document. It should work correctly now.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, May 23, 2018 9:58 PM
  • <g class="gr_ gr_52 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="52" id="52">Thanks</g> you very much Paul. It works fine now.
    Thursday, May 24, 2018 4:53 PM
  • Hello pragram,

    Good news to hear that your issue has been resolved. I would suggest you mark the helpful reply to close the thread. If you have any other issue, please feel free to post thread to let us know.

    Best Regards,

    Terry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 25, 2018 1:25 AM
  • Hi Paul, 

    I had a small issue with the following statement. I tried to use the same sub-routine for "drop-down", but the following "If "construct is failing to recognize the phrase and yet it is commenting.

     If .Duplicate.Text <> "the drop-down" Then

    Thanks again. I have marked the first comment as the answer.

    Friday, May 25, 2018 6:19 AM
  • If the constructs are the same, you should be using:
    If .Duplicate.Text <> "this drop-down" Then
    not:
    If .Duplicate.Text <> "the drop-down" Then
    a
    ssuming, of course, the text contains 'drop-down' rather than, say, 'dropdown'.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, May 27, 2018 5:11 AM