none
VSTO Word - How to get highlighted sections in word? RRS feed

  • Question

  • Hi,

    User is highlighting diffrecnt sections in word document using built in higlight button.

    I have a requirement to add some words before and after get highlighted sections.

    Please help me to find the start range and end range of each highlighted sections.

    Thanks in Advance,

    Raj

    Friday, November 25, 2016 11:14 AM

Answers

  • To clarify the requirement, word doument will be heghlightd in more than one places.

    Whether  each highlight, can be differentiated in the above code? ( I am using VSTO with C#).

    I have to give different text for starting of each highlighted text.

    If you had tested the code, you would have seen that each hihlighted range is processed separately.

    It would also have been helpful if you had said up front that you need to apply different texts for each highlight. In that case, you will need to use the second approach and add the required logic that determines which text to output. For example (in VBA):

    Sub Demo2()
    Application.ScreenUpdating = False
    Dim StrPrefix As String, StrSuffix As String
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Highlight = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case "Superman": StrPrefix = "Lois Lane & ": StrSuffix = " & Jimmy Olson"
          Case "Batman": StrPrefix = "Batgirl & ": StrSuffix = " & Robin"
          Case Else: StrPrefix = "": StrSuffix = ""
        End Select
        With .Duplicate
          .Collapse wdCollapseStart
          .Text = StrPrefix
          .Font.Italic = True
        End With
        With .Duplicate
          .Collapse wdCollapseEnd
          .Text = StrSuffix
          .Font.Bold = True
          .HighlightColorIndex = wdNoHighlight
        End With
        'The next line is only needed if the Find is based on formatting without regard to text
        If .End = ActiveDocument.Range.End Then Exit Sub
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    I'll leave you to do the C# conversion, as I don't use C#.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Raj9448 Tuesday, November 29, 2016 2:09 PM
    • Unmarked as answer by Raj9448 Wednesday, November 30, 2016 9:50 AM
    • Proposed as answer by Chenchen LiModerator Thursday, December 1, 2016 9:32 AM
    • Marked as answer by Raj9448 Friday, December 2, 2016 6:47 AM
    Monday, November 28, 2016 10:14 PM

All replies

  • You could use Find/Replace. In VBA, you could use code like:

    Sub Demo1()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = "Prefix ^& Suffix"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Highlight = True
        '.Replacement.Highlight = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    Uncomennting '.Replacement.Highlight = False' will remove the original highlighting.

    Alternatively, if you want the original content to remain highlighted but don't want the additional content higlighted or you want to apply a different format to the prefix or suffix text, you could use something like:

    Sub Demo2()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Highlight = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        With .Duplicate
          .Collapse wdCollapseStart
          .Text = "Prefix "
          .Font.Italic = True
        End With
        With .Duplicate
          .Collapse wdCollapseEnd
          .Text = " Suffix"
          .Font.Bold = True
          .HighlightColorIndex = wdNoHighlight
        End With
        'The next line is only needed if the Find is based on formatting without regard to text
        If .End = ActiveDocument.Range.End Then Exit Sub
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Saturday, November 26, 2016 3:01 AM
  • Hi Paul,

    Thanks for the replay..

    To clarify the requirement, word doument will be heghlightd in more than one places.

    Whether  each highlight, can be differentiated in the above code? ( I am using VSTO with C#).

    I have to give different text for starting of each highlighted text..


    Thanks in Advance,

    Raj.

    • Marked as answer by Raj9448 Friday, December 2, 2016 6:46 AM
    • Unmarked as answer by Raj9448 Friday, December 2, 2016 6:47 AM
    Monday, November 28, 2016 7:41 AM
  • To clarify the requirement, word doument will be heghlightd in more than one places.

    Whether  each highlight, can be differentiated in the above code? ( I am using VSTO with C#).

    I have to give different text for starting of each highlighted text.

    If you had tested the code, you would have seen that each hihlighted range is processed separately.

    It would also have been helpful if you had said up front that you need to apply different texts for each highlight. In that case, you will need to use the second approach and add the required logic that determines which text to output. For example (in VBA):

    Sub Demo2()
    Application.ScreenUpdating = False
    Dim StrPrefix As String, StrSuffix As String
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
        .Highlight = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        Select Case .Text
          Case "Superman": StrPrefix = "Lois Lane & ": StrSuffix = " & Jimmy Olson"
          Case "Batman": StrPrefix = "Batgirl & ": StrSuffix = " & Robin"
          Case Else: StrPrefix = "": StrSuffix = ""
        End Select
        With .Duplicate
          .Collapse wdCollapseStart
          .Text = StrPrefix
          .Font.Italic = True
        End With
        With .Duplicate
          .Collapse wdCollapseEnd
          .Text = StrSuffix
          .Font.Bold = True
          .HighlightColorIndex = wdNoHighlight
        End With
        'The next line is only needed if the Find is based on formatting without regard to text
        If .End = ActiveDocument.Range.End Then Exit Sub
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Application.ScreenUpdating = True
    End Sub

    I'll leave you to do the C# conversion, as I don't use C#.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Raj9448 Tuesday, November 29, 2016 2:09 PM
    • Unmarked as answer by Raj9448 Wednesday, November 30, 2016 9:50 AM
    • Proposed as answer by Chenchen LiModerator Thursday, December 1, 2016 9:32 AM
    • Marked as answer by Raj9448 Friday, December 2, 2016 6:47 AM
    Monday, November 28, 2016 10:14 PM
  • Thanks Paul..
    Tuesday, November 29, 2016 2:09 PM
  • Hi,

    Does the demo from Paul work for you? 

    If you have any new issues, I suggest you post new threads.

    Thanks for your understanding.

    Regards,

    Celeste


    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.

    Thursday, December 1, 2016 9:32 AM
    Moderator