Solved

vb read word detect text format

Posted on 2012-12-26
3
266 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 48

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
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 video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

744 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

12 Experts available now in Live!

Get 1:1 Help Now