Log in

View Full Version : how to extract sentences containing every word in the Excel word list file



fann80
07-16-2010, 05:16 AM
I have an MS Word file and an Excel file containing a word list. I'd like to extract sentences containing every word from the list. The extracted sentences will be saved into a new Excel file. I found a similar solution(Title: Extract sentences containing a specific word to excel file, submitted by Lucas), but this is different in that I want to retrieve sentences containing more than one word (that is, every word in the Excel list file) at the same time.


Lucas' solution is to extract sentences containing a word("shall"), but what I want is to extract sentences containing every word in the Excel file (at the same time).

Any help will be appreciated.
Thanks.

fumei
07-16-2010, 11:32 AM
Let's be clear. You want to extract single sentences that contain ALL of the following words - if a sentence has even one word missing it is not to be extracted.

pot,only,even,thing,feel,seem,next,still,stone,though,real,broom,sudden,bit ,
toward,voice,while,wood,ever,few,lot,mind,remember,large,wall,happen,
mean,moment,hall,lose,whisper,enough,ground,magic,suppose,
almost,notice,fire,course,already,past,reach,forget,shake,wonder,
without,yet,own,place,care

Ummmm, I find the idea of a single sentence containing all those words bordering on the absurd.

Could you please quote me such a sentence?

fumei
07-16-2010, 11:38 AM
Technically, could you find/extract text that meets multiple word criteria?

Yes. However, it becomes process intensive with every word added.

And are you sure you mean sentence, rather than paragraph?

fann80
07-16-2010, 02:48 PM
What I mean is to extract sentences containing the first word in the list(say "pot"), and other sentences containing the second word(say "only"), and so on. And save all the extracted sentences in one excel file.

Sprawlers will need a pot deep enough to drape over.
The man had a pot of cooked vegetables which served on a long bun, along with a cup of tea.
.....

Then the lady sighed, “If only people knew how much more attractive they look with the hoods down.”
....

If I change the word list, I would get new sentences with all the words on the list (not all the words in one sentence!)

Thanks.

fumei
07-19-2010, 10:10 AM
OK, that is completely different from what you stated.

"I'd like to extract sentences containing every word from the list."

In many ways this is even more difficult. However, can it be done?

Yes, absolutely.

What it takes is an solid, completely precise, EXACT, description of the logic.

You have not done that (not even close). Once you figure out the rock-solid logic, it can be done. Howevr, again, it will take a huge amount of logic and processing. Let me see if I can help you along here. Let's take your example.

Sprawlers will need a pot deep enough to drape over.
The man had a pot of cooked vegetables which served on a long bun, along with a cup of tea.

.....

Then the lady sighed, “If only people knew how much more attractive they look with the hoods down.”
....


Ok, you state: "What I mean is to extract sentences containing the first word in the list(say "pot"), and other sentences containing the second word(say "only"),"

So, even with your small example, the logic is weak.

You have TWO sentecnes with "pot".

Do you perfrom two separate extractions? Do you extract the first "pot" sentence, and the "only"sentence, and then ignore the second "pot" sentence? If you also process the second "pot" sentence, do you still include the same "only" sentence - ending up with:

Sprawlers will need a pot deep enough to drape over.
Then the lady sighed, “If only people knew how much more attractive they look with the hoods down.”



and then a separate extraction - is this for a different separate file? - of:

The man had a pot of cooked vegetables which served on a long bun, along with a cup of tea.
Then the lady sighed, “If only people knew how much more attractive they look with the hoods down.”

If you have a Found "pot" and a NOT-FOUND "only"...do you stop the process right there? Do you continue the process looking for the third word "even"? ...Which also has the same questions posed above.

Again, can this kind of thing be done? Yes. But you must must must spell out, write out every single possible action, precisely, exactly, with zero fuzziness. It must be precisely what you want.

VBA only does what you tell it to do. If you do not tell it - precisely - it will not do anything.

fann80
07-20-2010, 01:38 AM
Thank you so much for your help.

I'll explain what I want to do:

I have a Word document file(C:\temp\document.doc) and an Excel word list file(C:\temp\word_list.xls).

There are 90 words in "sheet1" of "word_list.xls". The word "pot" is in the Row 1 and Column 1 (R1C1); "only" in R2C1; "professor" in R3C1, and so on.

Firstly, the VBA finds every sentence containing "pot"(that is, R1C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C1 to R10C1 (if the VBA finds 10 sentences) in "sheet1" of a new Excel file(C:\temp\extract.xls)

Secondly, the VBA finds every sentence containing "only"(that is, R2C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C2 to R21C2 (if the VBA finds 21 sentences) in the same sheet(that is, "sheet1") of "extract.xls".

Thirdly, the VBA finds every sentence containing "professor"(that is, R3C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C3 to R5C3 (if the VBA finds 5 sentences) in sheet1 of extract.xls.

....

The VBA repeats the action until it finds every sentence containing the last word (that is, R90C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C90 to R20C90 (if the VBA finds 20 sentences) in sheet1 of extract.xls.

If the VBA can't find any sentence containing a certain word(say, R12C1), then the 12th column of sheet1 of extract.xls will remain empty.


This is what I want to do.

As I had told in my first posting, I found a very similar solution here (Title: Extract sentences containing a specific word to excel file, submitted by Lucas), but it should be modified to serve my need. Please refer to Lucas' code.

Thanks a loooooooot!

fumei
07-21-2010, 09:57 AM
"Please refer to Lucas' code."

You did not even post a link to Steve's code. So, no. I want to see your code. "but it should be modified to serve my need. "

I want to see what you have done.

But let me see if I understand. From what you posted above there is NO - repeat NO - connection between the words. There is NO connection whatsoever between sentences that have "pot" and sentences that have "only".

Correct?

I would suggest you try with a data file of only three words. Make that work.

You have not even stated what application you are working from. Are you doing this from Word (opening the Excel data file?); or are you doing this from Excel (opening the Word document file?).

In any case: "Lucas' solution is to extract sentences containing a word("shall"), but what I want is to extract sentences containing every word in the Excel file (at the same time). "

I have not gone hunting for whatever Lucas (Steve) posted, but it seems to be that if you can do something once - "extract sentences containing a word("shall)" - then this is easy.

Do it more than once.

Just going back to logic...what do you do if a sentence has two of the words? Say it has "pot" AND "only", does it gets listed twice? What about Case? Is "Only" to be treated the same as "only"?

fumei
07-21-2010, 10:19 AM
The reason I am asking you to really think about your logic is because you are NOT doing that. Read - carefully - what you posted.

"pot"(that is, R1C1 in sheet1 of word_list.xls)
"only"(that is, R2C1 in sheet1 of word_list.xls)

That is what YOU wrote. Correct? What does that look like? It looks like:

R1C1 - "pot"
R2C1 - "only"

However...you also wrote:

"Firstly, the VBA finds every sentence containing "pot"(that is, R1C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C1 to R10C1 (if the VBA finds 10 sentences) in "sheet1" of a new Excel file(C:\temp\extract.xls)

Secondly, the VBA finds every sentence containing "only"(that is, R2C1 in sheet1 of word_list.xls) from "

So......doing your Firstly -please note the text I bolded -:

R1C1 - "pot"
R2C1 - "only"


becomes....
R1C1 - "This is the first sentence with pot"
R2C1 - "This is the second sentence with pot."
R3C1 - "And this sentence has the word pot in it as well."
R4C1 - "another pot sentence"
R5C1 - "This pot thing is boring"

etc etc.

In other words, your "Firstly" will wipe out your words you are using to search for.

Again, figure out EXACTLY what you want to do. maybe that is what you want to do, I don't know, but if it IS, then you must build an array of your search words ("pot", "only", "professor") etc, BEFORE you do anything, because doing what you say will delete all your search words.

fann80
07-25-2010, 05:19 AM
My "Firstly" won't wipe out my words I am using to search for, because the copied sentences will be saved in a new Excel file (that is, in C:\temp\extract.xls, not in C:\temp\word_list.xls)

Note the parts I underlined:

"Firstly, the VBA finds every sentence containing "pot"(that is, R1C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C1 to R10C1 (if the VBA finds 10 sentences) in "sheet1" of a new Excel file(C:\temp\extract.xls)



My answers:

You did not even post a link to Steve's code.
--> If I could have, I would have posted, but I am not allowed because my post count is less than 5. Sorry for that. Let me know how. I think you can find the code by entering "Lucas extract sentences" in the search box with the second option selected(You know I can't post the website. I feel like to teach a fish how to swim. Sorry.).



But let me see if I understand. From what you posted above there is NO - repeat NO - connection between the words. There is NO connection whatsoever between sentences that have "pot" and sentences that have "only". Correct?
--Yes. There is NO connection whatsoever between sentences that have "pot" and sentences that have "only".



I would suggest you try with a data file of only three words. Make that work.
--If I use the Lucas' code and extract sentences manually (that is, by typing new key words over and again ), it works perfectly. But if the words I want to look for are more than 100, that would be very tiring.


You have not even stated what application you are working from. Are you doing this from Word (opening the Excel data file?); or are you doing this from Excel (opening the Word document file?).
--I will use a doc file to extract sentences from, an xls file to refer to word list, and finally another xls file to save the extracted sentences from the doc file. So I will open a doc file first and extract sentences from it using the xls word_list file. I am sorry, but this is the best I can describe what I would do.


I have not gone hunting for whatever Lucas (Steve) posted, but it seems to be that if you can do something once - "extract sentences containing a word("shall)" - then this is easy.
Do it more than once.

--I know, but I don't like to do it over and again 100 times or more!



Just going back to logic...what do you do if a sentence has two of the words? Say it has "pot" AND "only", does it gets listed twice? What about Case? Is "Only" to be treated the same as "only"?
--I need all sentences. The sentence(s) with "pot" and "only" will be saved in both "pot" column and "only" colum (in sheet1 of extract.xls). I don't care. Any case(upper or lower) will be treated as differenct.


English is a foreign language to me, and I have no prgramming background (=A double barrier!!). So it's very hard to explain what I want to do clearly and from a programmer's view.

This is the best I can explain. I am not sure I can answer your questions next time.

Thanks a lot, mate.

fumei
07-26-2010, 09:12 AM
The attached ZIp file demonstrates exactly what you have asked for. There are still unanswered questions, and you MUST deal with them.

You do not state what you want done with the new Excel file. So I do nothing with it.

I assume you want to save it, but you do not say this. It is important that you figure this out so you can properly save the file, end the instance of Excel created, and cleanly destroy the application object.

In the code this is commented out
'wbkXLNew.SaveAs FileName:="valid_path"
'Set wbkXLNew = Nothing
'appXL.Quit
'Set appXL = Nothing
these need to be fixed to what you are doing, and since I do not know what that is, I commented these lines out. Here is the code. Note that I have hard-coded the paths to the files, AND I used my names. You must adjust these to fit your circumstance. I will try to walk you through.
Option Explicit

Sub SentencesToExcel()
Dim appXL As Excel.Application
Dim wbkXLSource As Excel.Workbook
Dim wbkXLNew As Excel.Workbook

Dim strSearch() As String
Dim var
Dim r As Word.Range
Dim j As Long

Set appXL = CreateObject("Excel.Application")
Set wbkXLSource = appXL.Workbooks.Open(FileName:="c:\ZZZ\Test\test2\WordList.xls")
Set wbkXLNew = appXL.Workbooks.Add

For var = 0 To 2
' 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
Loop
End With
Next
appXL.Visible = True

'wbkXLNew.SaveAs FileName:="valid_path"
'Set wbkXLNew = Nothing
'appXL.Quit
'Set appXL = Nothing
End Sub
My source Excel file only has three items. It makes NO difference if this is 3 or 100. So...

1. declare variiables and objects

2. Set the objects: the application, the source workbook with the words, and the new Excel workbook that is going to get the sentences.
Set appXL = CreateObject("Excel.Application")
Set wbkXLSource = appXL.Workbooks.Open(FileName:="c:\ZZZ\Test\test2\WordList.xls")
Set wbkXLNew = appXL.Workbooks.Add


3. build an array of the words to be searched for. In this case, I only have three words, so it is hard-coded. You could make this your 100, or go through the source Excel for non-blank values in Col1.
For var = 0 To 2
' 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
NextNow we have an array of the search words.

4. so we can close the course file, and properly destroy its object.
' close the source Excel file as do not need it
wbkXLSource.Close
' destroy its object
Set wbkXLSource = Nothing

5. iterate through each word in the search array using it to search for the word, and if found, expand to the containing sentence, use THAT text to put into the next cell in the new Excel file, collapse the range (so you do not include it again...otherwise you get an infinite loop), and go on to the next search word found.

If one is not found, resize the document range to be the whole document again, and use the NEXT search word, iterating through all Found of that words, dumping each sentence text into the new Excel file.
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
Loop ' to next found instance of current word
End With
Next ' search word
IMPORTANT! the paths are hard coded - "c:\ZZZ\Test\test2\WordList.xls")

Either change the code to fit your files, or recreate the folders I used.

Tinbendr
07-26-2010, 08:18 PM
Here's my contribution.

This is written in Excel were the list of words are held. Seems like the appropriate place.

Option Explicit

Sub GrabTheSentence()
'Add Word object reference library.
'Tools->References - Check the Microsoft Word Object Library box
Dim WB As Workbook 'Source
Dim WS As Worksheet 'Source
Dim WB2 As Workbook 'Destination
Dim oWord As Word.Application
Dim wRng As Word.Range
Dim WordWasNotRunning As Boolean
Dim oDoc As Word.Document 'Word Doc Object
Dim aRow As Long 'Row index
Dim xRow As Long 'Row count
Dim Counter As Long 'Column index for destination sheet.
Dim Fname As Variant 'Filename of word file.

Set WB = ActiveWorkbook
Set WS = WB.Worksheets(1)
Set WB2 = Workbooks.Add

'Get existing instance of Word if it's open; otherwise create a new one
On Error Resume Next

Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
WordWasNotRunning = True
End If

On Error GoTo Err_Handler

'Prompt to select file
Fname = Application.GetOpenFilename("Word Files (*.doc; *.docx), *.doc; *.docx")

'Open the Word file.
Set oDoc = oWord.Documents.Open(Fname, Visible:=False)

'Get the last row of source.
xRow = WS.Cells(WS.Rows.Count, 1).End(xlUp).Row
'lRow = Cells(Rows.Count, 1).End(xlUp).Row

'Counter for Destination Rows.
Counter = 0

'Loop through the Source Rows.
For aRow = 1 To xRow

'Use Word's Find to search for words.
Set wRng = oDoc.Range
Do
With wRng.Find
.ClearFormatting
.Text = WS.Range("B" & aRow).Value
.Forward = True
.Format = True
.MatchWholeWord = True
.Execute
End With
If wRng.Find.Found Then
With wRng
'The found range only includes the found word.
'Move the Range to include the sentence.
.Expand wdSentence
'Counter is the column on the Destination sheet.
Counter = Counter + 1
'Insert the sentence into the cell.
WB2.Worksheets(1).Cells(aRow, Counter).Value = wRng
'Collapse the range.
.Collapse 0
End With
End If
Loop Until Not wRng.Find.Found
Counter = 0
Next
oDoc.Close savechanges:=wdDoNotSaveChanges

If WordWasNotRunning Then
oWord.Quit
End If

'Make sure you release object references.
Set oDoc = Nothing
Set oWord = Nothing

'quit
Exit Sub

Err_Handler:
MsgBox "Program Error" & Err.Description, vbCritical, "Error: " _
& Err.Number
If WordWasNotRunning Then
oWord.Quit
End If

End Sub

fumei
07-27-2010, 10:27 AM
For my testing I originally wrote it for Excel as well. However, the OP clearly clarifuied that it was to be executed from Word, so I changed it to Word.

fann80, conceptually it does not really matter whether it is working from Word or Excel. Technically it does, as you need to change some variables and some actions. Once you decide which way it is going you adjust accordingly.

fann80
07-28-2010, 07:02 PM
Hi, Gerry
Your code works beautifully..
Except that the sentences from 2nd/3rd/4th... word(of word_list.xls) should have been saved in the 2nd/3rd/4th column(of extract.xls).

Please refer to the below again.

Firstly, the VBA finds every sentence containing "pot"(that is, R1C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C1 to R10C1 (if the VBA finds 10 sentences) in "sheet1" of a new Excel file(C:\temp\extract.xls)

Secondly, the VBA finds every sentence containing "only"(that is, R2C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C2 to R21C2 (if the VBA finds 21 sentences) in the same sheet(that is, "sheet1") of "extract.xls".

Thirdly, the VBA finds every sentence containing "professor"(that is, R3C1 in sheet1 of word_list.xls) from document.doc, and copy & paste the sentences into R1C3 to R5C3 (if the VBA finds 5 sentences) in sheet1 of extract.xls.

....

Thank you very much.


p.s.
I'd like to change the list word(that is, the word on the word_list.xls) in the extracted senteces to have bold face. I tried to insert the code like below, but it doesn't work!(Sorry, you know I'm a raw beginner.)

Do While .Execute(Findtext:=strSearch(var), Forward:=True) _
= True

'Because the word could be "comes, await, or touched," the list word need to be expanded to hold the variation.
r.Expand unit:=wdWord
Selection.Font.Bold = 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



Hi, Tinbendr
Sorry, but it doesn't seem to work. I start from the word_list.xls, and (after Checking the Microsoft Word Object Library box). run the code. But after I select the word file that the code asks me to choose, nothing happens except that I have a new Excel file(Book1.xls) which is blank . I need more detailed step-by-step explanation.
Thank you anyway.

fann80
07-28-2010, 10:23 PM
I think I can solve the column problem by adding a variable "k" like below.


Sub SentencesToExcel()
Dim appXL As Excel.Application
Dim wbkXLSource As Excel.Workbook
Dim wbkXLNew As Excel.Workbook

Dim strSearch() As String
Dim var
Dim r As Word.Range
Dim j As Long
Dim k As Long


Set appXL = CreateObject("Excel.Application")
Set wbkXLSource = appXL.Workbooks.Open(FileName:="i:\HPwork\WordList.xls")
Set wbkXLNew = appXL.Workbooks.Add

For var = 0 To 99
' 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
k = 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, k).Value = r.Text
j = j + 1
r.Collapse 0
Loop ' to next found instance of current word
k = k + 1
j = 1

End With
Next
appXL.Visible = True

wbkXLNew.SaveAs FileName:="i:\HPwork\SentenceList3.xls"
Set wbkXLNew = Nothing
appXL.Quit
Set appXL = Nothing
End Sub

fumei
07-29-2010, 09:25 AM
As...yikes!....I missed that you wanted it to go into different columns. Me bad. You have fixed this?

And the other thing re:bold? This is working for you?

fann80
07-29-2010, 10:38 PM
I solved the columns, but I can't solve the bold face font.

I'd like to have the extracted sentence with its key word being bold.

Thank you, mite.

Tinbendr
07-30-2010, 04:15 AM
Insert the bold command after the Execute

With r.Find
' with each Found
Do While .Execute(Findtext:=strSearch(var), Forward:=True) _
= True
'Bold the found
r.Font.Bold = true

Tinbendr
07-30-2010, 04:32 AM
Hi, Tinbendr
Sorry, but it doesn't seem to work.

Did you d/l the file and run the macro, or did you drop the code into your Excel file?

I tried in on several different Doc files and it worked fine.

fumei
07-30-2010, 08:19 AM
That will not make the word in the string going to Excel bold.

Tinbendr
07-30-2010, 03:24 PM
That will not make the word in the string going to Excel bold.I've really gotta stop shooting from the hip, because Gerry (and others) always fill me full of holes.

Add Dim

Dim ChrStart as Long


' and put in the next cell in the new Excel file
wbkXLNew.Worksheets("Sheet1").Cells(j, 1).Value = r.Text
With wbkXLNew.Worksheets("Sheet1").Cells(j, 1)
ChrStart = InStr(wbkXLNew.Worksheets("Sheet1").Cells(j, 1), strSearch(var))
If ChrStart > 1 Then
With .Characters(Start:=ChrStart, Length:=Len(strSearch(var))).Font
.FontStyle = "Bold"
End With
End If
End With

fumei
07-30-2010, 04:03 PM
Oh come now, it is not all that bad. Besides, I have been caught in - cough, cough, cough - errors from MY shooting from the hip at times.

Join the club pal.

fann80
08-13-2010, 12:54 PM
I've really gotta stop shooting from the hip, because Gerry (and others) always fill me full of holes.

Sorry for the late response.
Could you show me exactly where this code should be inserted in the above Gerry's code?

Tinbendr
08-13-2010, 07:41 PM
Sub SentencesToExcel()
Dim appXL As Excel.Application
Dim wbkXLSource As Excel.Workbook
Dim wbkXLNew As Excel.Workbook
Dim ChrStart As Long
Dim strSearch() As String
Dim var
Dim r As Word.Range
Dim j As Long
Dim k As Long


Set appXL = CreateObject("Excel.Application")
Set wbkXLSource = appXL.Workbooks.Open(Filename:="i:\HPwork\WordList.xls")
Set wbkXLNew = appXL.Workbooks.Add

For var = 0 To 99
' 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
k = 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, k).Value = r.Text
With wbkXLNew.Worksheets("Sheet1").Cells(j, 1)
ChrStart = InStr(wbkXLNew.Worksheets("Sheet1").Cells(j, 1), strSearch(var))
If ChrStart > 1 Then
With .Characters(Start:=ChrStart, Length:=Len(strSearch(var))).Font
.FontStyle = "Bold"
End With
End If
End With
j = j + 1
r.Collapse 0
Loop ' to next found instance of current word
k = k + 1
j = 1

End With
Next
appXL.Visible = True

wbkXLNew.SaveAs Filename:="i:\HPwork\SentenceList3.xls"
Set wbkXLNew = Nothing
appXL.Quit
Set appXL = Nothing
End Sub

fann80
08-17-2010, 04:31 PM
Thanks for the full code, Tinbendr.

But this code produces the sentences with all the words on the wordlist being bold.

And the bold effect doesn't applied to the sentences on the second column and thereafter.

It looks like this.

The first column (the sentences containing "only"(that is, the first word on the wordlist))

1. It only took Harry one trip upstairs to move everything he owned from the cupboard to this room.
2. And now there were only three people left to be sorted.
3. Only the photographs on the mantelpiece really showed how much time had passed.

<All the words on the wordlist have been changed into bold face. What I need is that on A column only "only" should be bold, and other words should be normal font face. Regarding 3., even if it(=only) appears at the first of the sentence, it should be bold.>


The second column (the sentences containing "even"(that is, the second word on the wordlist))

1. He'd never even seen the boy.
2. We've had precious little to celebrate for eleven years.
3. Famous for something he won't even remember!

<No bold effects applied to the sentences. What I need is that on B column only "even" should be bold.>


And I'd like to change the search rule. The code should extract sentences containing the exact word but not the part of the word. And it is not case sensitive.

fumei
08-18-2010, 12:43 AM
1. part of this is the issue of performing action in Excel, rather than in Word. Excel is a crappy application when dealing with text. And correctly so, as it is NOT a word-processor. Word on the other hand IS a word-processor. And therefore handles...ahem, processing words, much much better.

2. I am going to giove up on this thread because of:

"And I'd like to change the search rule. The code should extract sentences containing the exact word but not the part of the word. And it is not case sensitive."

Until you come up with what Ihave repeatedly stated - figure out your EXACT logic - this just seems to be a moving target.

I fail to see much point to trying to assist until you decide - as I have stated precisely, exactly, what you want to happen.

But I will reiterate - if you want precise exact control of the actions of word...use a word-processor.

fann80
08-18-2010, 02:41 PM
2. I am going to giove up on this thread because of:

"And I'd like to change the search rule. The code should extract sentences containing the exact word but not the part of the word. And it is not case sensitive."


Hi, Gerry.
I thought changing the search rule would be a tiny step further.
I found the solution from your original code.


Do While .Execute(Findtext:=strSearch(var), MatchWholeWord:=True, Forward:=True) _
= True


Thanks.

Tinbendr
08-27-2010, 06:46 PM
I finally got back to this.

I couldn't duplicate the 'every search word bold' problem, but I did discover when a word wasn't found, it would leave a blank column.

I uploaded both files so you could examine it.

expling
04-12-2011, 03:09 PM
I finally got back to this.

I couldn't duplicate the 'every search word bold' problem, but I did discover when a word wasn't found, it would leave a blank column.

I uploaded both files so you could examine it.
Thanks for your code.

expling
04-12-2011, 03:36 PM
I finally got back to this.

I couldn't duplicate the 'every search word bold' problem, but I did discover when a word wasn't found, it would leave a blank column.

I uploaded both files so you could examine it.

Can you make the output file with three columns like:
115 pot This is a sentence with pot.
111 only This is a sentence with only.

Thanks,
Expling

expling
04-12-2011, 06:03 PM
Hi ,
Can you make the output file with three columns as follow in which the first two columns is just copy from the wordlist file:

115 pot This is a sentence with pot.
111 only This is a sentence with only.

BIG THANKSSSSS

Pat