Solved

vb read word detect text format

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
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.

688 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