Need Help with Macro to Change Style

T

Thomas M.

Word 2003

I have the following macro in a Word 2003 template.

Sub DocDevFix()

If Selection.Start = Selection.End Then
Selection.Style = ActiveDocument.Styles("DocDevFix")
Else
Selection.Style = ActiveDocument.Styles("DocDevFix Char")
End If

End Sub

Basically, if the insertion point is a single point then the macro should
act on the paragraph, and if a range is selected then it should act on that
selection. DocDevFix is just a custom style that I created in which the
font is red and the line spacing is set to exactly 15.

If there is no selection, then the macro works. However, if there is a
selection then the macro errors out and shows a dialog box that contains
nothing but OK and Help buttons (no error message or text of any kind). I
seem to remember that this worked at one time, so I'm not sure why it's not
working now. Can someone tell me what the problem is?

Also, if the macro is run on text that is outlined, it removes the outline
numbering. If anyone has a clever idea for how to avoid that and leave the
outline numbering intact, I would be grateful for the advice on how to do
that.

Thanks for any help that you may provide.

--Tom
 
B

Bear

Tom:

I don't think anything in my post will help you. Your original code worked
on my machine, so something else is amiss. However, the following macro may
give you some ideas.

~~~~~~~~~~
Sub x()
'
' x Macro
' Macro recorded 5/7/2007 by David Chinell
'
Dim objListTemplate As ListTemplate

If Selection.Type = wdSelectionIP Then
Set objListTemplate = Selection.Range.ListFormat.ListTemplate
Selection.Style = ActiveDocument.Styles("DocDevFix")
Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=objListTemplate
Else
Selection.Style = ActiveDocument.Styles("DocDevFix Char")
End If

Set objListTemplate = Nothing

End Sub
~~~~~~~~~~

First, you can test the selection type itself to see what's selected. Check
out the possible values in the Object browser, or in Help or by typing the
statement and letting the VBA autocomplete display them.

Outline numbering and bullets are defined by listtemplate objects. I
dimensioned a working object (objListTemplate) to use to store the
listtemplate of the selection, then reapply it after you apply your paragraph
style. It might not work perfectly -- I didn't test it on anything other than
a bullet list. Maybe numbers will get jumbled, maybe the first item in a list
will lose its restart property. Could use some refinement I'm guessing.

As an automatic reflex, I destroy the object at the end of the macro to
avoid ghosts in the memory.

Bear
 

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