S
Sarvesh
Hi:
I am attempting to do the following from within excel
1. Open a word document
2. Search on a specific Style.
3. Copy the text found associated with that style into excel.
I am able to do this for the first Style search but for subsequent
searches, it finds the same style twice. I am able to work around this
issue by closing the word document after each search and re-opening
it. However, this is quite time and resource expensive. Any
suggestions?
Here is a snippet of my code that causes duplicates (without closing
word document)
Sub CopyFromWord()
Dim wrdApp As Object
' Note: Using late binding (as opposed to Dim wrdApp AS
Word.Application) due to a bug
' Details at http://support.microsoft.com?kbid=292744
Dim wrdDoc As Object
Dim IsWordRunning As Boolean
Dim row As Integer
IsWordRunning = ApplicationIsRunning("Word.Application")
If IsWordRunning = True Then
Set wrdApp = GetObject(, "Word.Application")
Else
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("D:\temp\SBS_Enhancements_PDSN_List.doc")
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Style = "Requirement"
With wrdApp.Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
row = 0
Do While wrdApp.Selection.Find.Execute = True
row = row + 1
Worksheets("Sheet1").Cells(row, 1).Value =
wrdApp.Selection.Range.text
Loop
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Style = "Conditional Requirement"
With wrdApp.Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Do While wrdApp.Selection.Find.Execute = True
row = row + 1
Worksheets("Sheet1").Cells(row, 1).Value =
wrdApp.Selection.Range.text
Loop
wrdApp.Quit ' close the Word application
I am attempting to do the following from within excel
1. Open a word document
2. Search on a specific Style.
3. Copy the text found associated with that style into excel.
I am able to do this for the first Style search but for subsequent
searches, it finds the same style twice. I am able to work around this
issue by closing the word document after each search and re-opening
it. However, this is quite time and resource expensive. Any
suggestions?
Here is a snippet of my code that causes duplicates (without closing
word document)
Sub CopyFromWord()
Dim wrdApp As Object
' Note: Using late binding (as opposed to Dim wrdApp AS
Word.Application) due to a bug
' Details at http://support.microsoft.com?kbid=292744
Dim wrdDoc As Object
Dim IsWordRunning As Boolean
Dim row As Integer
IsWordRunning = ApplicationIsRunning("Word.Application")
If IsWordRunning = True Then
Set wrdApp = GetObject(, "Word.Application")
Else
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("D:\temp\SBS_Enhancements_PDSN_List.doc")
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Style = "Requirement"
With wrdApp.Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
row = 0
Do While wrdApp.Selection.Find.Execute = True
row = row + 1
Worksheets("Sheet1").Cells(row, 1).Value =
wrdApp.Selection.Range.text
Loop
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Style = "Conditional Requirement"
With wrdApp.Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Do While wrdApp.Selection.Find.Execute = True
row = row + 1
Worksheets("Sheet1").Cells(row, 1).Value =
wrdApp.Selection.Range.text
Loop
wrdApp.Quit ' close the Word application