VBA Runtime Error 5281

S

susiecc

I'm getting a randome Runtime Error #5281 when I run a macro that creates a
paragraph style in a document. The error says something like the style I'm
trying to create is based on too many styles.

The funny thing about this is that if I quit out of Word and load the macro
again, it doesn't necessarily happen.

I've created 120 style macros. When a user activates a User Form through a
command bar, a selection of buttons are available to apply style to text. You
highlight the text and then click a button. It checks to see whether the
style is already created and if it isn't it creates the style, then applies
it to the selected text.

It was very odd. It was working fine yesterday. Then, when I created a few
more style macros, it suddenly started giving me the error. After closing and
opening the project template, it then allowed me to cycle through all the
macros with no error.

I don't know what is going on. I couldn't even find the error when I checked
with Microsoft.

Help
 
J

Jezebel

It might be possible to help you if you posted the code and highlighted the
line that throws the error.
 
S

susiecc

It always errors on the line i indicated, but not always on the same macro.
i.e. sometimes this macro will run through fine and other times it will error.
I also cannot find any reference to this particular runtime error
number...5281.

Here is the line that randomly errors:

ActiveDocument.Styles.Add Name:="ACK", Type:=wdStyleTypeParagraph

-----
here is the code snippit:


Sub ETMInsertACK1()

'adding this new
'styleName = "ACK"

'-------

Dim ETMmyRange As Range
Set ETMmyRange = Selection.Range
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

'-------------
If StyleExists("ACK") Then
'----------------
GoTo continue
Else: GoTo continue2
continue2:

ActiveDocument.Styles.Add Name:="ACK", Type:=wdStyleTypeParagraph

continue:

Selection.InsertBefore "{ACK}"
'-----------------------
'THIS APPLIES THE ACK STYLE TO THE SELECTED AREA

Selection.Style = ActiveDocument.Styles("ACK")

'-----------------------------

With ETMmyRange.Find
.Text = "{ACK}"
With .Replacement
.ClearFormatting
.Font.Bold = False
.Font.Italic = False
.Font.Color = wdColorRed
End With
End With
'Else
'msgbox "no the style does not exist"

'--------
End If
'--------

ETMmyRange.Find.Execute Replace:=wdReplaceAll

End Sub
 
J

Jezebel

At a guess, there's a bug in your StyleExists() function, so that it
sometimes wrongly returns FALSE. Simpler coding is just to try adding the
style and ignore the error if it already exists --

on Error resume next
ActiveDocument.Styles.Add Name:="ACK", Type:=wdStyleTypeParagraph
on error goto 0


If you do use StyleExists (and in ALL other cases!) you never need GOTOs
except for error-handling. All you need is

If not StyleExists("ACK") Then
...
End If
 

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