montreal
04-23-2014, 05:58 PM
In reference to this thread: http://www.vbaexpress.com/forum/showthread.php?33093
Really useful post. Thanks so much. I am posting a little update to the code that permitted it to work for me.
Two things I would love.
1. That an option for a blank cell be added to the list built each time the loop switches to a new variable.
2. That a version of the code be posted that allows a list of regular expressions set up in the WordList.xls file to be used to search.
Here is the adapted code:
Sub SentencesToExcel()
Dim appExcel As Object
Dim objSheet As Object
Dim wbkXLNew As Object
Dim strSearch() As String
Dim var
Dim r As Word.Range
Dim j As Long
Set appExcel = CreateObject("Excel.Application")
Set wbkXLSource = appExcel.Workbooks.Open(FileName:="c:\temp\wordlist.xls")
Set wbkXLNew = appExcel.Workbooks.Add
For var = 0 To 300
' build the array of search words from the source Excel file
Redim Preserve strSearch(var)
strSearch(var) = wbkXLSource.Worksheets("Sheet1").Cells(var + 1, 1).Value
Next
' close the source Excel file as do not need it
wbkXLSource.Close
' destroy its object
Set wbkXLSource = Nothing
j = 1
' for each search words
For var = 0 To UBound(strSearch())
' make a range ovbject of the whole document
Set r = ActiveDocument.Range
With r.Find
' with each Found
Do While .Execute(Findtext:=strSearch(var), Forward:=True) _
= True
' expand to the sentence
r.Expand Unit:=wdSentence
' and put in the next cell in the new Excel file
wbkXLNew.Worksheets("Sheet1").Cells(j, 1).Value = r.Text
j = j + 1
r.Collapse 0
wbkXLNew.Worksheets("Sheet1").Cells(j, 1).Value = "break"
Loop
End With
Next
appExcel.Visible = True
'wbkXLNew.SaveAs FileName:="valid_path"
'Set wbkXLNew = Nothing
'appXL.Quit
'Set appXL = Nothing
End Sub
Really useful post. Thanks so much. I am posting a little update to the code that permitted it to work for me.
Two things I would love.
1. That an option for a blank cell be added to the list built each time the loop switches to a new variable.
2. That a version of the code be posted that allows a list of regular expressions set up in the WordList.xls file to be used to search.
Here is the adapted code:
Sub SentencesToExcel()
Dim appExcel As Object
Dim objSheet As Object
Dim wbkXLNew As Object
Dim strSearch() As String
Dim var
Dim r As Word.Range
Dim j As Long
Set appExcel = CreateObject("Excel.Application")
Set wbkXLSource = appExcel.Workbooks.Open(FileName:="c:\temp\wordlist.xls")
Set wbkXLNew = appExcel.Workbooks.Add
For var = 0 To 300
' build the array of search words from the source Excel file
Redim Preserve strSearch(var)
strSearch(var) = wbkXLSource.Worksheets("Sheet1").Cells(var + 1, 1).Value
Next
' close the source Excel file as do not need it
wbkXLSource.Close
' destroy its object
Set wbkXLSource = Nothing
j = 1
' for each search words
For var = 0 To UBound(strSearch())
' make a range ovbject of the whole document
Set r = ActiveDocument.Range
With r.Find
' with each Found
Do While .Execute(Findtext:=strSearch(var), Forward:=True) _
= True
' expand to the sentence
r.Expand Unit:=wdSentence
' and put in the next cell in the new Excel file
wbkXLNew.Worksheets("Sheet1").Cells(j, 1).Value = r.Text
j = j + 1
r.Collapse 0
wbkXLNew.Worksheets("Sheet1").Cells(j, 1).Value = "break"
Loop
End With
Next
appExcel.Visible = True
'wbkXLNew.SaveAs FileName:="valid_path"
'Set wbkXLNew = Nothing
'appXL.Quit
'Set appXL = Nothing
End Sub