Can't find styles in VBA that were created during import of .htm file to Word

I

Ian Jaffray

Hello,

I'm creating documents by opening and converting .htm files into Word using
Word VBA. Styles, and style names, are created automatically during the open
from the CSS properties of the text in the .htm file and the associated CSS
file.

When I use Find in the Word app to find paragraphs with one of the style
names created during the import (for example "transcript-title"), this works
as it normally would. However, when I try the same operation in VBA, it
fails to recognize the style name, and finds nothing.

Of course, I could be making a mistake with the VBA code, but this is pretty
basic, and the same standard code works when trying to Find other styles not
created during the .htm import, so I think that's not the problem. It seems
as if the styles so created during the .htm import are "different" in some
way and are being seen differently from the VBA environment.

In the code below, I have recorded the simple Find a style operation
executed in the Word app, and then tried running the same macro recorded
during that successful Find operation on the same original document. The
Find operation in the app runs successfully and finds all text styled with
"transcript-title", as you'd expect, during the recording of the macro, but
the recorded operation fails when run as a macro.

Has anyone had a similar problem trying to Find styles created during .htm
files importing? Anyone have the Find op work in the app but not from VBA?
Any ideas of what to try?

Stepping through the following macro in VBA, which is just a recording of
the basic Find a style operation, when it hits "Selection.Find.Execute",
nothing is being found; it acts just as if there were no paragraphs styled
with a style of that name.

Thanks very much,

Ian J.

Sub Macro116()
'
' Macro116 Macro
' Macro recorded 06/05/01
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("transcript-title")
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
End Sub
 
C

Cindy M -WordMVP-

Hi Ian,

I can't say that I've ever noticed a difference/problem, as long as you're
careful that the style names match exactly. Style-stuff is case-sensitive, and
the property usually wants the style name (a string); you're passing it a style
object. A quick test, here, does work.

The only other thing I notice is that the setting is separated from the actual
execution of Find. The code I tested looks like this:

With Selection.Find
.Text = ""
.Style = "Heading 3"
.format = True
.Wrap = wdFindStop
.Execute
End With
I'm creating documents by opening and converting .htm files into Word using
Word VBA. Styles, and style names, are created automatically during the open
from the CSS properties of the text in the .htm file and the associated CSS
file.

When I use Find in the Word app to find paragraphs with one of the style
names created during the import (for example "transcript-title"), this works
as it normally would. However, when I try the same operation in VBA, it
fails to recognize the style name, and finds nothing.

Of course, I could be making a mistake with the VBA code, but this is pretty
basic, and the same standard code works when trying to Find other styles not
created during the .htm import, so I think that's not the problem. It seems
as if the styles so created during the .htm import are "different" in some
way and are being seen differently from the VBA environment.

In the code below, I have recorded the simple Find a style operation
executed in the Word app, and then tried running the same macro recorded
during that successful Find operation on the same original document. The
Find operation in the app runs successfully and finds all text styled with
"transcript-title", as you'd expect, during the recording of the macro, but
the recorded operation fails when run as a macro.

Has anyone had a similar problem trying to Find styles created during .htm
files importing? Anyone have the Find op work in the app but not from VBA?
Any ideas of what to try?

Stepping through the following macro in VBA, which is just a recording of
the basic Find a style operation, when it hits "Selection.Find.Execute",
nothing is being found; it acts just as if there were no paragraphs styled
with a style of that name.

Thanks very much,

Ian J.

Sub Macro116()
'
' Macro116 Macro
' Macro recorded 06/05/01
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("transcript-title")
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
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top