0
votes

enter image description hereI have a Word Document that was created by saving a PDF document as a Word Document. The PDF document has some text that was highlighted in Yellow. I need to detect the highlighted text. I guess the conversion is not perfect. Some of the text in the Word Document is showing that it is highlighted, and some just has a Yellow background but is not showing as being highlighted. I can select some of the text and click on the highlight button on the menu and it shows a highlight color, and for some of the words, it shows No Color.

Also in the VBA immediate window if I type

?selection.formattedtext.highlightcolorindex, I get 7 for some selections, and 0 for others.

For those selections that do not show up as highlighted, how can I detect them?

Update: I selected a "Highlighted" word and executed call Selection.ClearFormatting and the font changed, but the yellow highlight remained. So that tells me that the yellow highlight is not a format. I also selected No Color with the text selected and the yellow highlight remained. I have verified that it is neither Highlight or Shading. Hmmm... What could be causing the highlight?

Update: I discovered that if I delete the "highlighted text" that is not really highlighted, the text that is to the right of the deleted text moves over to the left and the highlight remains. So then I discovered that I can Select the highlight if there is no text under a portion of the highlight. Then I can drag the highlight to another portion of the page. So, this highlight is an object. If I right click on it, I have options such as Format Object.

So now I know that these "Highlights" are actually like objects. If there is no text under the object, I can grab it and drag it around. How can I, using VBA, find these types of objects?

What I really want is the entire paragraph under the highlight objects. Is that possible?

1
Hard to say without a screenshot or maybe you can copy-paste some text and shre the doc file?Tim Williams
Hi Tim. I am not permitted to share the entire file. I wish I could. But, I did share a small screen shot that shows the highlight selected and the sizing handles.David.Warwick

1 Answers

0
votes

Well, I figured out how to do it. I used another StackOverflow question to lead me to the answer How do you select an image that was just pasted to MS Word via VBA

I am running this code from Microsoft Access on a Microsoft Word object.

Public Sub ReadWord2(strPath As String)
'Const wdColorRed = 255
Dim objWord As Object
Dim objDoc As Word.Document
Dim strParagraph As String
Dim intShapeCount As Integer
Dim Shape As Word.Shape

Set objWord = CreateObject("Word.Application")
objWord.Visible = True

Set objDoc = objWord.Documents.Open(strPath)
Set objSelection = objWord.Selection

intShapeCount = objDoc.Shapes.Count
'objDoc.Shapes.SelectAll

For Each Shape In objDoc.Shapes

'If Shape.AutoShapeType = 1 Then
Shape.Select
'Debug.Print Shape.Fill.BackColor
'Debug.Print Shape.AutoShapeType, Shape.Name
'Debug.Print Shape.TextFrame.TextRange.Text
objSelection.Expand wdParagraph
Debug.Print objSelection.Text, Shape.Fill.BackColor
'End If
Next
End Sub

There is actually more code in the sub, but I deleted the parts that are not relevant. Hopefully this will help someone else who runs into the same problem. The objSelection.Text will contain the paragraph text that the shape is part of. Now I just need to write code to handle the text and make sure that it is text that I want.