none
How do you write a macro that deletes all text with X formatting, but only if the text with X formatting isn't followed by text with Y formatting? RRS feed

All replies

  • Hi jerrrywang,

    I am not clear of your requirement. Do you want to remove the text that the formatting is different?

    If so, I think you could check each paragraph’s font (e.g. name), if it is empty, then check each character of this paragraph.

    If ActiveDocument.Paragraphs(1).Range.Font.Name = "" Then
      'TODO 
    End If

    There are some links that can help you:

    # Font Object (Word)

    https://msdn.microsoft.com/en-us/library/office/ff837477.aspx

    # Styles Object (Word)

    https://msdn.microsoft.com/en-us/library/office/ff837469.aspx

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, June 23, 2015 7:05 AM
    Moderator
  • Assuming you have particular text strings in mind, you could use code like:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim StrA As String, StrB As String
    Dim RngA As Range, RngB As Range
    StrA = "Text A"
    StrB = "Text B"
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = StrA & StrB
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        Set RngA = .Duplicate.Range
        Set RngB = .Duplicate.Range
        RngA.End = RngA.Start + Len(StrA)
        RngB.Start = RngB.End - Len(StrB)
        If RngA.Style = "StyleA" Then
          If RngB.Style <> "StyleB" Then
            RngA.Text = vbNullString
          End If
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    Alternatively, if you're only interested in the Styles and you don't care what the text is, you could use code like:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Style = "StyleA"
        .Execute
      End With
      Do While .Find.Found
        If .Characters.Last.Next.Style <> "StyleB" Then
          .Text = vbNullString
        End If
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]



    • Edited by macropodMVP Tuesday, June 23, 2015 10:17 AM Additional code
    Tuesday, June 23, 2015 10:12 AM