PDA

View Full Version : [SOLVED:] Extract Highlighted Words



VB-AN-IZ
02-04-2017, 01:18 AM
Related to this solved ticket, which highlights spelling and/or grammatical errors in a document: http://www.vbaexpress.com/forum/showthread.php?57468-Highlight-All-Spelling-and-or-Grammatical-Errors (http://www.vbaexpress.com/forum/showthread.php?57468-Highlight-All-Spelling-and-or-Grammatical-Errors)

Is there a way to:

1. Extract all of the highlighted words from a document, pasting them into a separate document?

2. Consider consecutive highlighted words as a single block of text?

As in, from this text:



The United States of America is known as...


...could it consider "The United States" one block of text:

The United States


America


...as opposed to separating each highlighted word?:

The
United
States
America

gmaxey
02-04-2017, 09:42 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oCol As New Collection
Dim oDoc As Document, lngIndex As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Highlight = True
While .Execute
oCol.Add oRng.Duplicate
oRng.Collapse wdCollapseEnd
Wend
End With
Set oDoc = Documents.Add
For lngIndex = oCol.Count To 1 Step -1
oDoc.Paragraphs.Last.Range.FormattedText = oCol.Item(lngIndex)
oDoc.Paragraphs.Add
Next
lbl_Exit:
Exit Sub
End Sub

VB-AN-IZ
03-30-2017, 02:30 AM
Brilliant. Thanks very much, and apologies for the belated "solved"...

VB-AN-IZ
05-09-2017, 08:28 PM
Apologies for another follow-up.

Is it possible to also treat the below characters the same as a space (as in, not the end of a phrase; potentially the start or continuation of a phrase) when highlighting?


&
[space]&[space]
[space]and[space]
,[space]
:[space]
-
[space]-[space]
)
)[space]
[space]^0145
[space]^0146
[space]^039
[space]^0147
[space]^0148
[space]^034
(
[space](

For example:


Currently, for "World Anti-Doping Code", only "World Anti" and "Code" are highlighted.


Currently, from "Law & Order" or "Law and Order", the words "Law" and "Order" will be highlighted separately; from "Law&Order", only "Law" is highlighted.

Hope that makes sense, and thanks again for any help!

gmaxey
05-10-2017, 04:28 AM
Unfortunately it makes no sense to me. This thread is about extracting highlighted text from a document. Where is the code you want to modify?

VB-AN-IZ
05-11-2017, 12:00 PM
I guess this is technically more about which words/phrases are highlighted before the text is extracted. It's a follow-up to the second point from the original post – "Is there a way to consider consecutive highlighted words as a single block of text?"

Maybe that aspect would be followed up better in the thread I mentioned in the original post?
http://www.vbaexpress.com/forum/showthread.php?57468-Highlight-All-Spelling-and-or-Grammatical-Errors

I don't see a lot of similarities between the codes in each of the threads. I assumed it would be a 2-step process of: 1) highlighting; 2) extracting the highlighted words. Evidently it's far more complicated than that!