dj44
07-30-2016, 04:06 PM
Hi Folks,
hope all are doing good:)
I am trying to make new documents by extracting paragraphs that have a specific keyword.
In a spreadsheet Column A - is a List of words found in my document
Example
Column A - Find Word
Jaguar
Ferrari
Ford
So the idea is it will go through the document and find all the paragraphs that start with for example Jaguar and then put them in a new document.
I lost the link to the original unfortunately, and then I tried to fix it even more, and I'm not sure what I did.
I have put it together the best i can - it ran but no document was made.
Sub MakeNewFiles()
Dim xlApp As Object
Dim xlWbk As Object
Dim oWorksheet As Object
Dim blnStart As Boolean
Dim i As Long
Dim iLastRows As Long
Dim StrFnd As Range
Dim RngSrc As Range
Dim RngTgt As Range
Dim DocSrc As Document
Dim DocTgt As Document
On Error Resume Next
Set xlApp = GetObject(Class:="Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject(Class:="Excel.Application")
If xlApp Is Nothing Then
MsgBox "Failed to start Excel", vbExclamation
Exit Sub
End If
blnStart = True
End If
Set DocSrc = ActiveDocument
Set xlWbk = xlApp.Workbooks.Open(FileName:="C:\Users\DJ\Desktop\WordList.xlsx")
Set oWorksheet = xlWbk.Worksheets("Words")
iLastRows = xlWsh.Range("A" & xlWsh.Rows.Count).End(-4162).Row ' -4162 = xlUp
For i = 2 To iLastRows
Set DocTgt = Documents.Add
With DocSrc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Set StrFnd = xlWsh.Range("A" & i).Value
.Text = StrFnd
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set RngSrc = .Paragraphs(1).Range
Set RngTgt = DocTgt.Range.Characters.Last
RngTgt.Collapse wdCollapseStart
RngTgt.FormattedText = RngSrc.FormattedText
.Start = RngSrc.End
.Find.Execute
Loop
End With
DocTgt.SaveAs2 FileName:=DocSrc.Path & "\" & StrFnd & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
DocTgt.Close False
Next i
ExitHandler:
On Error Resume Next
xlWbk.Close SaveChanges:=False
If blnStart Then
xlApp.Quit
End If
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
I don't know if its an excel problem or a word problem
Can a pro have a look at it for me - thank you
Dj
hope all are doing good:)
I am trying to make new documents by extracting paragraphs that have a specific keyword.
In a spreadsheet Column A - is a List of words found in my document
Example
Column A - Find Word
Jaguar
Ferrari
Ford
So the idea is it will go through the document and find all the paragraphs that start with for example Jaguar and then put them in a new document.
I lost the link to the original unfortunately, and then I tried to fix it even more, and I'm not sure what I did.
I have put it together the best i can - it ran but no document was made.
Sub MakeNewFiles()
Dim xlApp As Object
Dim xlWbk As Object
Dim oWorksheet As Object
Dim blnStart As Boolean
Dim i As Long
Dim iLastRows As Long
Dim StrFnd As Range
Dim RngSrc As Range
Dim RngTgt As Range
Dim DocSrc As Document
Dim DocTgt As Document
On Error Resume Next
Set xlApp = GetObject(Class:="Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject(Class:="Excel.Application")
If xlApp Is Nothing Then
MsgBox "Failed to start Excel", vbExclamation
Exit Sub
End If
blnStart = True
End If
Set DocSrc = ActiveDocument
Set xlWbk = xlApp.Workbooks.Open(FileName:="C:\Users\DJ\Desktop\WordList.xlsx")
Set oWorksheet = xlWbk.Worksheets("Words")
iLastRows = xlWsh.Range("A" & xlWsh.Rows.Count).End(-4162).Row ' -4162 = xlUp
For i = 2 To iLastRows
Set DocTgt = Documents.Add
With DocSrc.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Set StrFnd = xlWsh.Range("A" & i).Value
.Text = StrFnd
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found
Set RngSrc = .Paragraphs(1).Range
Set RngTgt = DocTgt.Range.Characters.Last
RngTgt.Collapse wdCollapseStart
RngTgt.FormattedText = RngSrc.FormattedText
.Start = RngSrc.End
.Find.Execute
Loop
End With
DocTgt.SaveAs2 FileName:=DocSrc.Path & "\" & StrFnd & ".docx", _
FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False
DocTgt.Close False
Next i
ExitHandler:
On Error Resume Next
xlWbk.Close SaveChanges:=False
If blnStart Then
xlApp.Quit
End If
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation
Resume ExitHandler
End Sub
I don't know if its an excel problem or a word problem
Can a pro have a look at it for me - thank you
Dj