Help with Oject Deleted Error

G

Greg

Hi All,

Yesterday I was helping another user crate a macro to
toggle from any font to "symbol" font and then toggle back
to the original font. The following works, but if the
initial font is "symbol" an error "Run time error 5825.
The object has been deleted." is generated. I understand
why the error is being generated but I can't figure out a
way to prevent the error or mask the error. I have tried
all kinds of On error Goto, On error Resume,
isObjectValid, .Exists, but nothing seems to work.

I am looking for a means to interupt (mask) the error
message with a more pleasing message something like "The
first use of this macro in the current document must be
initiated with a font other than symbol." Any help and
critique of the code below is welcome.



Sub ToggleFromAnyFontToSymbolandBack()

'Macro created July 25, 2004 by Gregory K. Maxey

If Selection.Font.Name <> "Symbol" Then
'Save current font name to a docvariable
ActiveDocument.Variables("OldFont").Value =
Selection.Font.Name
End If
With Selection.Font
If .Name <> "Symbol" Then
.Name = "Symbol"
Else: Selection.Font.Name = ActiveDocument.Variables
("OldFont").Value
End If
End With
End Sub
 
D

Dave Lett

Hi Greg,

Perhaps you can use something like the following:

Sub ToggleFromAnyFontToSymbolandBack()

'Macro created July 25, 2004 by Gregory K. Maxey
Dim err_Handler
On Error GoTo err_Handler
If Selection.Font.Name <> "Symbol" Then
'Save current font name to a docvariable
ActiveDocument.Variables("OldFont").Value = Selection.Font.Name
End If
With Selection.Font
If .Name <> "Symbol" Then
.Name = "Symbol"
Else: Selection.Font.Name = ActiveDocument.Variables("OldFont").Value
End If
End With

err_Handler:
MsgBox "Enter your message here.", vbInformation
End Sub

HTH,
Dave
 
G

Greg

Dave,

I will give that a try (for experience), but while waiting
for a reply I pounded out the following. It seems to
work, but I am always looking for comments from more
experienced users:

Sub ToggleFromAnyFontToSymbolandBack()

'Macro created July 25, 2004 by Gregory K. Maxey
Dim UserInput As String
If Selection.Font.Name <> "Symbol" Then
'Set the base font and save to a docvariable
ActiveDocument.Variables("BaseFont").Value =
Selection.Font.Name
End If
'Determines if a a base font is named. This prevents
execution errors if the user
'starts in symbol font.
For Each aVar In ActiveDocument.Variables
If aVar.Name = "BaseFont" Then num = aVar.Index
Next aVar
If num = 0 Then GoTo Error
Retry:
With Selection.Font
If .Name <> "Symbol" Then
.Name = "Symbol"
Else: Selection.Font.Name = ActiveDocument.Variables
("BaseFont").Value
End If
End With
End
Error: MsgBox "You started with the symbol font applied.
The is no base font defined."
UserInput = InputBox("Please define a base
font.", "Apply Font", "Times New Roman")
If UserInput = "" Then Exit Sub
ActiveDocument.Variables.Add Name:="BaseFont",
Value:=UserInput
GoTo Retry
End Sub
 
G

Greg

Dave,

Works great and eliminates the code needed to look for the
variable. Here is my result:

Sub ToggleFromAnyFontToSymbolandBack()

'Macro created July 25, 2004 by Gregory K. Maxey.
Coaching by Dave Lett.
Dim err_Handler
On Error GoTo err_Handler
Dim UserInput As String
If Selection.Font.Name <> "Symbol" Then
'Set the base font and save to a docvariable
ActiveDocument.Variables("BaseFont").Value =
Selection.Font.Name
End If
Retry:
With Selection.Font
If .Name <> "Symbol" Then
.Name = "Symbol"
Else: Selection.Font.Name = ActiveDocument.Variables
("BaseFont").Value
End If
End With
End
err_Handler: MsgBox "You started with the symbol font
applied. The is no base font defined."
UserInput = InputBox("Please define a base
font.", "Apply Font", "Times New Roman")
If UserInput = "" Then Exit Sub
ActiveDocument.Variables.Add Name:="BaseFont",
Value:=UserInput
GoTo Retry
End Sub
 
D

Dave Lett

Hi Greg,

You ought to beware of too many pointers in your code (i.e., GoTo Retry).
This is what is called spaghetti code because you have to chase the strands
through the code. A better, and more concise alternative to your routine
would be the following:

Dim err_Handler
On Error GoTo err_Handler
Dim UserInput As String
With Selection.Font
If .Name <> "Symbol" Then
'Set the base font and save to a docvariable
ActiveDocument.Variables("BaseFont").Value = Selection.Font.Name
.Name = "Symbol"
Else
.Name = ActiveDocument.Variables("BaseFont").Value
End If
End With
Exit Sub
err_Handler:
MsgBox "You started with the symbol font applied. The is no base font
defined."
UserInput = InputBox("Please define a base font.", "Apply Font",
"Times New Roman")
If UserInput = "" Then Exit Sub
ActiveDocument.Variables.Add Name:="BaseFont", Value:=UserInput
Resume

We only test the name of the Font family once and we use Resume (instead of
your GoTo) method to run the line of code that fired the err_Handler.

HTH,
Dave
 
D

Dave Lett

Hi again Greg,

And here's an example that anticipates the error and avoids the err_Handler
altogether:

Dim err_Handler
Dim UserInput As String
Dim bExists As Boolean
With Selection.Font
If .Name <> "Symbol" Then
'Set the base font and save to a docvariable
ActiveDocument.Variables("BaseFont").Value = Selection.Font.Name
.Name = "Symbol"
Else
For Each oVar In ActiveDocument.Variables
If oVar.Name = "BaseFont" Then
bExists = True
End If
Next oVar
If bExists Then
.Name = ActiveDocument.Variables("BaseFont").Value
Else
MsgBox "You started with the symbol font applied. The is no base
font defined."
UserInput = InputBox("Please define a base font.", "Apply Font",
"Times New Roman")
If UserInput = "" Then Exit Sub
ActiveDocument.Variables.Add Name:="BaseFont", Value:=UserInput
.Name = UserInput
End If
End If
End With

HTH,
Dave
 
G

Greg

Dave,

Thanks for the tips and pointers.
-----Original Message-----
Hi again Greg,

And here's an example that anticipates the error and avoids the err_Handler
altogether:

Dim err_Handler
Dim UserInput As String
Dim bExists As Boolean
With Selection.Font
If .Name <> "Symbol" Then
'Set the base font and save to a docvariable
ActiveDocument.Variables("BaseFont").Value = Selection.Font.Name
.Name = "Symbol"
Else
For Each oVar In ActiveDocument.Variables
If oVar.Name = "BaseFont" Then
bExists = True
End If
Next oVar
If bExists Then
.Name = ActiveDocument.Variables ("BaseFont").Value
Else
MsgBox "You started with the symbol font applied. The is no base
font defined."
UserInput = InputBox("Please define a base font.", "Apply Font",
"Times New Roman")
If UserInput = "" Then Exit Sub
ActiveDocument.Variables.Add
Name:="BaseFont", Value:=UserInput
 

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