Solved

VBA Word. Get the color number of the highlight, of the highlighted text

Posted on 2011-02-14
5
1,228 Views
Last Modified: 2012-05-11
The code below works like a charm. It lists the highlighted text in a String.
What I would like to ask though, is to modify this code a bit so I get the number of the highlight color next to each string catch.  

Option Explicit

Sub CollectHighlights()
    Dim rng As Range
    Dim strText As String
    
    Set rng = ActiveDocument.Range
    Do
        With rng.Find
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            If .Execute Then
                strText = strText & rng.Text & vbCr
                rng.Collapse wdCollapseEnd
                rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop While True
    MsgBox strText
End Sub

Open in new window


Thank you with love again as always.

I give full 250 points with an A classification for this.
0
Comment
Question by:New_Alex
  • 2
  • 2
5 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34894697
See below.

Chris
Sub CollectHighlights()
    Dim rng As Range
    Dim strText As String
    
    Set rng = ActiveDocument.Range
    Do
        With rng.Find
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            If .Execute Then
                strText = strText & rng.Text & ", (" & strwdColorIndex(rng.HighlightColorIndex) & ")" & vbCr
                rng.Collapse wdCollapseEnd
                rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop While True
    MsgBox strText
End Sub

Function strwdColorIndex(index As Integer) As String
    Select Case index
        Case -1
            strwdColorIndex = "wdByAuthor"
        Case 0
            strwdColorIndex = "wdAuto - No Highlight"
        Case 1
            strwdColorIndex = "Black"
        Case 2
            strwdColorIndex = "Blue"
        Case 3
            strwdColorIndex = "Turquoise"
        Case 4
            strwdColorIndex = "Bright Green"
        Case 5
            strwdColorIndex = "Pink"
        Case 6
            strwdColorIndex = "Red"
        Case 7
            strwdColorIndex = "Yellow"
        Case 8
            strwdColorIndex = "White"
        Case 9
            strwdColorIndex = "Dark Blue"
        Case 10
            strwdColorIndex = "Teal"
        Case 11
            strwdColorIndex = "Green"
        Case 12
            strwdColorIndex = "Violet"
        Case 13
            strwdColorIndex = "Dark Red"
        Case 14
            strwdColorIndex = "Dark Yellow"
        Case 15
            strwdColorIndex = "Grey 50%"
        Case 16
            strwdColorIndex = "Grey 25%"
        Case Else
            strwdColorIndex = "Error!"
    End Select
        
End Function

Open in new window

0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 250 total points
ID: 34894705
Apologies ... color number is of course simply:

Chris
Sub CollectHighlights()
    Dim rng As Range
    Dim strText As String
    
    Set rng = ActiveDocument.Range
    Do
        With rng.Find
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            If .Execute Then
                strText = strText & rng.Text & ", (" & rng.HighlightColorIndex & ")" & vbCr
                rng.Collapse wdCollapseEnd
                rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop While True
    MsgBox strText
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:New_Alex
ID: 34894757
Chris thanks very much,

I love you brother, like always....

Promised points given straight away.....
0
 
LVL 1

Author Closing Comment

by:New_Alex
ID: 34894778
I have modified the code a bit. I inactivated the 2 lines :
 '   rng.Collapse wdCollapseEnd
              '  rng.End = ActiveDocument.Range.End
As they cause some crashes and other problems.
It works better now. (Code below)

[code]Option Explicit

Sub CollectHighlights()
    Dim rng As Range
    Dim strText As String
   
    Set rng = ActiveDocument.Range
    Do
        With rng.Find
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            If .Execute Then
                  strText = strText & "Color Index " & rng.HighlightColorIndex _
                                    & "  : " & rng.Text & vbCr
                '   rng.Collapse wdCollapseEnd
              '  rng.End = ActiveDocument.Range.End
            Else
                Exit Do
            End If
        End With
    Loop While True
    MsgBox strText
End Sub

[/code]

Thanks!!!!
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 34894813
The two lines shouldn't give a problem. They are designed to explicitly search from the end of the current highlight to the end of the document.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now