Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 15th, 2008, 12:32 PM
Registered User
 
Join Date: Sep 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Selection Find Style not Working

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:

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 = ""
        .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!
Reply With Quote
  #2 (permalink)  
Old September 16th, 2008, 02:07 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Are you sure that you need - Selection.Find.ParagraphFormat.Borders.Shadow = False

Here is the slightly modifed code that works for me


Code:
'######################################
'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
    If wdApp.Selection.Find.Found = True Then
        .Selection.Copy
    End If
End With
'######################################
'FIND TEXT WITH HEADING STYLE NOT WORKING
'######################################
Cheers
Shasur

http://www.dotnetdud.blogspot.com

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
Reply With Quote
  #3 (permalink)  
Old September 16th, 2008, 11:27 AM
Registered User
 
Join Date: Sep 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

@Shasur- Removing that line worked once. However, how I seem to be getting a:

Error 429, "ActiveX Can't Create Object"

Nothing else has changed except for removing that one line. It fails on this line:

Code:
Set wdApp = GetObject(, "Word.Application")
Does anyone have any thoughts? Thanks.


Reply With Quote
  #4 (permalink)  
Old September 16th, 2008, 02:18 PM
Registered User
 
Join Date: Sep 2008
Location: , , USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have solved the Error 429 by removing:

Code:
'Set wdApp = GetObject(, "Word.Application")
Thank you for the help. I very much appreciate it!

Reply With Quote
  #5 (permalink)  
Old November 30th, 2009, 11:03 PM
Registered User
 
Join Date: Dec 2008
Location: Melbourne, Australia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same issue, but fix doesn't work

I have exactly the same issue, but I'm already doing the late binding and its still falling over on the line

With .Selection.Find
.Text = "Fred"

I've also tried
Application.Selection.Find.Text = "Fred"
to no avail.

I'm using this in a vb6 & MS Word (professional) 2003 environ, ref to Microsoft word object 11.0 library sitting on an XP box with all relevant service packs applied.

I've also tried the "winword.exe /r" fix that I found on msdn - also didn't work.
Reply With Quote
  #6 (permalink)  
Old November 30th, 2009, 11:40 PM
Registered User
 
Join Date: Dec 2008
Location: Melbourne, Australia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Found my own answer

Found my own answer: (I'm doing this in the footer of every document, which is why I'm doing the 'activewindow' bit)
Code:
                With .ActiveDocument.ActiveWindow.Selection.Find
                    strFind = "FindMe"
                    strReplace = ""
                    .Execute FindText:=strFind, Replace:=wdReplaceAll, ReplaceWith:=strReplace
                    strFind = "Replacement"
                    .Execute FindText:=strFind, Replace:=wdReplaceAll, ReplaceWith:=strReplace
                End With
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Open Word Doc from Access - find, find next save donaldmaloney Access VBA 1 May 25th, 2005 12:09 PM
Local COM working , but not working at Web Serv nagen111 .NET Web Services 3 February 19th, 2005 04:22 AM
Create a find and a find and replace in VB.NET snowy0 VB How-To 0 January 26th, 2004 07:03 PM
difference between style.visibility and style.disp Mimi Javascript How-To 6 September 18th, 2003 12:50 AM



All times are GMT -4. The time now is 10:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.