PDA

View Full Version : Find text in any of several styles



docLok
06-24-2013, 10:29 PM
Need help badly to preserve sanity. Have tried every way I can think of in VBA to search a document for text with subscript in one of three heading styles, then format the fourth character as subscript.
I'd like to do it fastest and most efficiently--but at this point arrays are fast efficient way to eliminate what's left inside my head.
Welcome any and all examples.
Thanks.

gmaxey
06-25-2013, 02:11 PM
You tried everything and did post one example of what you tried.

You state that you have text in "subscript" and you want to find is and format the fourth character as subscript. Isn't is already subscript?

Maybe not so you might try:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim lngIndex As Long
For lngIndex = 1 To 3
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "<*>"
.MatchWildcards = True
.Format = True
.Style = "Heading " & lngIndex
While .Execute
On Error Resume Next
oRng.Characters(4).Font.Subscript = True
On Error GoTo 0
oRng.Collapse wdCollapseEnd
Wend
End With
Next

End Sub

fumei
06-25-2013, 02:38 PM
Is there a post in some other forum from the OP? Am I missing something?

"and did post one example of what you tried." There is no example here.

Text = "<*>" How do we know anything about the text context?

docLok
06-25-2013, 06:10 PM
I've been at this long enough I may have posted previously, perhaps even in another forum. The text placeholder is okay: insert any chemical formula (e.g., H2O) and adjust the subscript function.

Greg: it's elegant. Beautiful. I almost don't care it runs or no. I have to alter the variable name to avoid conflict with previous declaration (another function within the macro), but not a problem.
Apologies for the ambiguity in original post about what was and wasn't subscript. The doc tool (Author-it) that's publishing to Word handles local formatting in all styles except named heading styles, where the publisher doesn't support character stuff, which has to be applied ex post. Some of these books are over 500 pgs, so I try to make the routines as efficient as possible.

I'll post after I've run it through the test materials.

--david

docLok
06-26-2013, 07:03 PM
My thanks to Greg. Even with my mods the routine runs just fine. Saved a bunch of work. Made much happy.

--david