Solved

vb read word detect text format

Posted on 2012-12-26
3
267 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
3 Comments
 
LVL 49

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

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

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

17 Experts available now in Live!

Get 1:1 Help Now