Created
February 15, 2014 18:00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Explicit | |
'References: | |
'http://stackoverflow.com/questions/19071173/ms-word-macro-how-to-adapt-so-it-get-data-from-my-excel-file | |
'http://www.mrexcel.com/forum/excel-questions/32187-convert-variant-array-string-array.html | |
Dim TargetList() As String | |
Public Sub GetWordsFromExcelAndHighlight() | |
Dim lngIndex As Long | |
Dim arrWords As Variant | |
arrWords = GetListArray("C:\wordlist.xlsx") | |
ReDim TargetList(UBound(arrWords, 1)) As String | |
For lngIndex = 2 To UBound(arrWords, 1) | |
TargetList(lngIndex) = arrWords(lngIndex, 1) | |
Next | |
Call Highlight | |
End Sub | |
Function GetListArray(ByRef strFileName As String) As Variant | |
Dim xlapp As Object | |
Dim xlbook As Object | |
Dim xlsheet As Object | |
Dim bAppStart As Boolean | |
On Error Resume Next | |
Set xlapp = GetObject(, "Excel.Application") | |
If Err Then | |
bAppStart = True | |
Set xlapp = CreateObject("Excel.Application") | |
End If | |
On Error GoTo 0 | |
Set xlbook = xlapp.Workbooks.Open(FileName:=strFileName) | |
Set xlsheet = xlbook.Worksheets(1) | |
GetListArray = xlsheet.range("A1").CurrentRegion.Value | |
xlbook.Close | |
If bAppStart = True Then xlapp.Quit | |
Set xlapp = Nothing | |
Set xlbook = Nothing | |
Set xlsheet = Nothing | |
End Function | |
Sub Highlight() | |
Dim range As range | |
Dim i As Long | |
For i = 1 To UBound(TargetList) | |
Set range = ActiveDocument.range | |
With range.Find | |
.Text = TargetList(i) | |
.Format = True | |
.MatchCase = True | |
.MatchWholeWord = False | |
.MatchWildcards = False | |
.MatchSoundsLike = False | |
.MatchAllWordForms = False | |
Do While .Execute(Forward:=True) = True | |
range.HighlightColorIndex = wdYellow | |
Loop | |
End With | |
Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment