CPForecast
Programmer
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:
The thing that's killing me is that i can do a
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!
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:
Code:
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 = "This is a test."
.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
Code:
.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!