PDA

View Full Version : Solved: Selection Find Style not Working



CPForecast
09-15-2008, 08:53 AM
Hello,

I have a macro that is technically being written for Excel XP, but the part of the program that isn't working involves searching a word document for a particular style.

The program loops through several thousand file paths and names in an excel document. Among other things, the program then takes the path and file name and opens it. From there, the program is supposed to find and copy any "Heading 1" style text. That's the part that seems to be failing. Code below:



Static Function GetWordTitle(path As String, FileName As String) As String

Dim wdApp As Word.Application, wdDoc As Word.Document

On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0

Set wdDoc = wdApp.Documents.Open(path + "\" + FileName)
wdApp.Visible = True

wdDoc.Activate

'######################################
'FIND TEXT WITH HEADING STYLE NOT WORKING
'######################################
With wdApp
.Selection.Find.ClearFormatting
.Selection.Find.Style = ActiveDocument.Styles("Heading 1")
.Selection.Find.ParagraphFormat.Borders.Shadow = False
With .Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
.Selection.Find.Execute
.Selection.Copy
End With
'######################################
'FIND TEXT WITH HEADING STYLE NOT WORKING
'######################################

'Copy title from clipboard
Dim MyData As DataObject
Dim strClip As String

Set MyData = New DataObject
MyData.GetFromClipboard
strClip = MyData.GetText


'Close the document
wdApp.ActiveDocument.Close (wdDoNotSaveChanges)
'Close out of Word
wdApp.Quit (wdDoNotSaveChanges)


GetWordTitle = strClip

End Function

The thing that's killing me is that i can do a


.Selection.TypeText "This is a test."
right at the beginning of the "With" and it'll add it to the beginning of the document just fine.

Any ideas on why this find might not be working would be much appreciated. Thanks!

fumei
09-15-2008, 10:20 AM
Posted an answer to the same question on Tek-Tips.

Please, when posting a question to multiple sites, it is good form to inform us that you have cross-posted.

http://www.excelguru.ca/node/7

CPForecast
09-15-2008, 12:32 PM
Sorry about that. I wasn't sure if any or all of the forums I posted to were popular enough to get a quick response. I posted here, on Tek-Tips, vbforums.com, and p2p wrox forums. I'll be sure to follow the rules next time.

fumei
09-16-2008, 08:53 AM
Not a problem really. It is not a rule, so to speak. You can certainly post in multiple places. I just wanted you to know that informing us that you have done so is a good idea.

CPForecast
09-16-2008, 12:14 PM
Problem has been solved at p2p wrox forums! I can't post full links yet, but the url is below. Thank you!

p2p [dot] wrox [dot] com/topic [dot] asp?TOPIC_ID=74060

fumei
09-16-2008, 01:07 PM
Not really, but whatever.

CPForecast
09-17-2008, 06:40 AM
@fumei- I would be happy to answer any questions you still have regarding this program.

fumei
09-17-2008, 10:39 AM
Well I mentioned a number of things in response to your Tek-Tips thread.

I mentioned some specific tips in order to use the code more efficiently. For example, using a Range instead of Selection; using the string directly rather than going through Copy/Paste.

I asked some specific questions. For example, why do you create/destroy the instance each time? Why do you not use the Document object you declare and set?


No answers to anything. Or comments to anything.

Shrug. It is not important. If you are happy with how it is working for you, then I certainly will not argue the point.

Please mark this thread as SOLVED.