Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Word Search from Excel results in Duplicates

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_PD SN_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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Word Search from Excel results in Duplicates

Hi

I think the problem is probably that the second search only searches the
end of the document (from the last match to the first search). You have
included the following line of code:
.Wrap = wdFindContinue

but if you do not have a reference to Word, the constant value for
wdFindContinue (1) will not have been set. There are a couple of possible
solutions for this:
- Change the code to use the value rather than the constant:
..Wrap = 1
- Set the value of wdFindContinue in your code:
wdFindContinue=1

The same problem could occur with the first search if the cursor is not at
the start of the document when the search occurs.

I hope that this helps!

Regards
Jane Pratt
Developer Tools Support
Microsoft UK

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Word Search from Excel results in Duplicates

Hi Jane:

Thanks for pointing me in the right direction. I resolved my problem
by adding the following code prior to initiating the second search

wrdApp.Selection.HomeKey Unit:=wdStory

This sets the cursor back to the start of the document and avoids the
duplicate selection when the search wraps around. Since I am doing
multiple searches, this significantly optimizes my performance (as
opposed to closing and re-opening the document each time)

regards
-Sarvesh

("Jane Pratt [MSFT]") wrote in message ...
Hi

I think the problem is probably that the second search only searches the
end of the document (from the last match to the first search). You have
included the following line of code:
.Wrap = wdFindContinue

but if you do not have a reference to Word, the constant value for
wdFindContinue (1) will not have been set. There are a couple of possible
solutions for this:
- Change the code to use the value rather than the constant:
.Wrap = 1
- Set the value of wdFindContinue in your code:
wdFindContinue=1

The same problem could occur with the first search if the cursor is not at
the start of the document when the search occurs.

I hope that this helps!

Regards
Jane Pratt
Developer Tools Support
Microsoft UK

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a macro to search an excel spread for duplicates jamalin Excel Worksheet Functions 4 February 16th 07 10:58 AM
Export Search results to Excel QBob Excel Discussion (Misc queries) 2 October 17th 06 09:50 PM
dump file search results into excel Nadia Excel Discussion (Misc queries) 2 July 29th 05 02:43 AM
Excel search results are hard to identify. Monique Excel Worksheet Functions 2 March 18th 05 12:57 AM
trying to search excel from Outlook... NO Results Phillips Excel Programming 2 December 10th 03 12:49 AM


All times are GMT +1. The time now is 07:38 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"