Solved

vb read word detect text format

Posted on 2012-12-26
3
270 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 51

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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
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.

740 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