Word VBA >> Help with Oject Deleted Error

by Greg » Mon, 26 Jul 2004 23:03:23 GMT

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


Word VBA >> Help with Oject Deleted Error

by Dave Lett » Mon, 26 Jul 2004 23:31:41 GMT


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








Word VBA >> Help with Oject Deleted Error

by Greg » Tue, 27 Jul 2004 00:10:14 GMT

ave,

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




Selection.Font.Name
("OldFont").Value
message
back
understand
a
tried
ActiveDocument.Variables


Help with Oject Deleted Error

by Greg » Tue, 27 Jul 2004 00:30:47 GMT

ave,

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


waiting
ActiveDocument.Variables
out
like "The


Help with Oject Deleted Error

by Dave Lett » Tue, 27 Jul 2004 02:00:28 GMT

i 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

"Greg" < XXXX@XXXXX.COM > wrote in message
news:458401c4732d$e7f481e0$ XXXX@XXXXX.COM ...




Help with Oject Deleted Error

by Dave Lett » Tue, 27 Jul 2004 02:06:28 GMT

i 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

"Greg" < XXXX@XXXXX.COM > wrote in message
news:458401c4732d$e7f481e0$ XXXX@XXXXX.COM ...




Help with Oject Deleted Error

by Greg » Tue, 27 Jul 2004 02:28:56 GMT

ave,

Thanks for the tips and pointers.

avoids the err_Handler
Selection.Font.Name
("BaseFont").Value
applied. The is no base
font.", "Apply Font",
Name:="BaseFont", Value:=UserInput
message
the
ActiveDocument.Variables
ActiveDocument.Variables
applied.
to
toggle
the
5825.
error
must be
and


Similar Threads

1. Can't create oject error

2. i need help with error on deleting rows in vba

3. "move oject with text"

4. Cast Oject to String Array

Is it possible to Cast an Object to a String Array the way I'm doing it in 
the code below?  I'm trying to reuse code I have without creating new code if 
I can.

        Dim asNames() As String
        Dim asValues() As String
        Dim sKeyPairValues As String = String.Empty

        Dim oNames Object
        Dim oValues Object

        Dim asDelims() As Char = {","}

        asNames = oNames.ToString.Split(asDelims)
        asValues = oValues.ToString.Split(asDelims)

        For i = 0 To asNames.Length - 1
            Try
                sKeyPairValues += asNames(i).ToUpper & "=" & asValues(i) & 
"; "
            Catch ex As Exception
                TraceUtil.WriteError(ex)
            End Try
        Next

Thanks

5. New Oject() {} vs. New Object(-1) {} - VB.Net

6. Cannot access to disposed oject.

sorry I post this problem again. I have to stop my work 
to fix the problem.

I'm doing a multi form application(Not a MDI one).
My startup form say Form1 has a datagrid say datagrid1, 
when I click a grid item in datagrid1, I wanna show 
form2. In my code, I achieved that with:

Public class Form1
  Inherits System.Windows.Forms.Form
  DIm frm2 as Form2  
  ...  
  Private sub Datagrid1_currentCellChanged(ByVal sender 
As Object, ByVal e As EventArgs)
  Dim bm As BindingManagerBase =      
DataGridBTerm.BindingContext(DataGridBTerm.DataSource, 
DataGridBTerm.DataMember)
  Dim datarw As DataRow = CType(bm.Current, 
DataRowView).Row
  //click an item
  If sender.currentcell.columnnumber = 0 Then
    Me.close()
    frm2 = new form2
End sub
....
End Class

   My problem is when I click the item in form1,form2 
showed up but with an error massage window says: 
 Cannot access disposed object named "DataGridTextBox"
 Object name: "DataGridTextBox"
 Here is the debug information:
************** Exception Text **************
System.ObjectDisposedException: Cannot access a disposed 
object named "DataGridTextBox".
Object name: "DataGridTextBox".
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.TextBoxBase.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.TextBox.GetLength()
   at System.Windows.Forms.TextBoxBase.set_SelectionStart
(Int32 value)
   at System.Windows.Forms.TextBox.SelectInternal(Int32 
start, Int32 length)
   at System.Windows.Forms.TextBoxBase.SelectAll()
   at System.Windows.Forms.DataGridTextBoxColumn.Edit
(CurrencyManager source, Int32 rowNum, Rectangle bounds, 
Boolean readOnly, String instantText, Boolean 
cellIsVisible)
   at System.Windows.Forms.DataGrid.Edit(String 
instantText)
   at System.Windows.Forms.DataGrid.Edit()
   at System.Windows.Forms.DataGrid.set_CurrentCell
(DataGridCell value)
   at System.Windows.Forms.DataGrid.OnMouseDown
(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseDown(Message& 
m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage
(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc
(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr 
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Any help on that will be appreciated.
Thanks!

7. how to clear/reset oject instance

8. Create a .NET oject from vbs

Dear all,
I am a newby in vbs, so please  sorry if the question is stupid.
I have a .NET assembly myasm.dll, In this assembly I have a class myclass.
Now I need from a vbs file create an instance of the myclass and call 
myclass.myfunc();
Is it possible? May be somebody could give a code sniplet.
Thanks,
Boni