Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vb read word detect text format

Posted on 2012-12-26
3
Medium Priority
?
279 Views
Last Modified: 2012-12-28
I'm writing a program that reads Word text.  The text is in tables.  Parts of the text in any given cell of the table may be highlighted.  Is there an easy way to pick out where the text highlight color starts and stops and also to detect what color is used for the highlighting?
0
Comment
Question by:RBECKMN
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 38722878
Hi,

You can adapt that

Sub FindHighlight()
 ' Make sure you can access the pane properly.
   Application.ActiveWindow.View = wdNormalView
   
   ' Holds the current pane.
   Dim CurrPane As Pane
   
   ' Access the test document pane.
   Set CurrPane = Application.ActiveWindow.ActivePane
   
   ' Go to the beginning of the document.
   CurrPane.Selection.GoTo wdGoToLine, wdGoToFirst
   

  ' Creates a search for the word.
   Dim DoSearch As Find
   Set DoSearch = CurrPane.Selection.Find
   
   ' Perform the search.
   With DoSearch
   
      ' Clear any existing formatting information.
      .ClearFormatting
      .Highlight = True
      .MatchCase = False
      .Wrap = wdFindContinue
         
      ' Continue until there is nothing else to search.
      While DoSearch.Execute()
      
         With CurrPane.Selection.FormattedText
            Bgn = .Start
            Last = .End
            MsgBox "This highlight begins at " & Bgn & " and ends at " & Last
            ColorIdx = .HighlightColorIndex
         End With
         
         ' Move to the next character.
         CurrPane.Selection.Move Count:=1
      Wend
   End With
   
   ' Go to the beginning of the document.
   CurrPane.Selection.GoTo wdGoToLine, wdGoToFirst
End Sub

Open in new window


Regards
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 38723185
This code shows how to analyse the highlighting colours for a particular table cell. It Note that it is written in VBA, and not in .NET, so might need a bit of tweaking.
.
The colour descriptions are taken from Word Help

Sub CellHighLighting(cl As Word.Cell)

Dim ch As Word.Range
Dim i As Integer
Dim j As Integer
Dim prev As Integer
Dim strText() As String
Dim Colour() As Word.WdColorIndex
Dim strDesc() As String
Dim strMessage As String

prev = 999

For Each ch In cl.Range.Characters
    If Asc(ch.Text) > 31 Then
        If ch.HighlightColorIndex <> prev Then
            prev = ch.HighlightColorIndex
            ReDim Preserve strText(i)
            ReDim Preserve strDesc(i)
            ReDim Preserve Colour(i)
            Colour(i) = prev
            strDesc(i) = GetColourDescription(Colour(i))
            i = i + 1
        End If
        strText(i - 1) = strText(i - 1) & ch.Text
    End If
Next ch
For j = 0 To i - 1
    strMessage = strMessage & "Text: " & strText(j) & vbTab & "Description: " & strDesc(j) & vbCrLf
Next j
MsgBox strMessage
End Sub
Function GetColourDescription(Colour As Word.WdColorIndex) As String
    Dim strColourDesc As String
    
    Select Case Colour
        Case wdAuto  '0
            strColourDesc = "Automatic color. Default; usually black."
        Case wdBlack     '1
            strColourDesc = "Black color."
        Case wdBlue  '2
            strColourDesc = "Blue color."
        Case wdBrightGreen   '4
            strColourDesc = "Bright green color."
        Case wdByAuthor  '-1
            strColourDesc = "Color defined by document author."
        Case wdDarkBlue  '9
            strColourDesc = "Dark blue color."
        Case wdDarkRed   '13
            strColourDesc = "Dark red color."
        Case wdDarkYellow    '14
            strColourDesc = "Dark yellow color."
        Case wdGray25    '16
            strColourDesc = "Shade 25 of gray color."
        Case wdGray50    '15
            strColourDesc = "Shade 50 of gray color."
        Case wdGreen     '11
            strColourDesc = "Green color."
        Case wdNoHighlight   '0
            strColourDesc = "Removes highlighting that has been applied."
        Case wdPink  '5
            strColourDesc = "Pink color."
        Case wdRed   '6
            strColourDesc = "Red color."
        Case wdTeal '10
            strColourDesc = "Teal color."
        Case wdTurquoise     '3
            strColourDesc = "Turquoise color."
        Case wdViolet    '12
            strColourDesc = "Violet color."
        Case wdWhite     '8
            strColourDesc = "White color."
        Case wdYellow    '7
            strColourDesc = "Yellow color."
    End Select
    GetColourDescription = strColourDesc
End Function

Open in new window


This
0
 

Author Closing Comment

by:RBECKMN
ID: 38728678
didn't take too much tweaking to adapt.  exactly what I was looking for - thanks very much.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
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 …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Suggested Courses

604 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