Log in

View Full Version : Run time error 5825: Object has been deleted



dpcardiff
11-15-2011, 06:53 PM
Hi

I created a user form to populate a set of bookmarks in a document. It worked fine but after a bit of research I noticed that the advice from the experts is to user Doc Variable fields rather than bookmarks.

I decided to give Doc Variables a try so copied the original file (just to be on the safe side). I replaced the bookmarks with the variables and amended the code to suit.

However, when I open the template I get an error message saying Run-time error 5825: Object has been deleted. The Debug highlights the following:

Private Sub Document_New ()

frmReportDetails.Show
End Sub


I checked the Document_New code in the template containing bookmarks and it is identical to above.

Does anyone know why I'm getting this error?

gmaxey
11-15-2011, 08:21 PM
The actual problem is most likely in your userform intialize event.

Step through the Document_New procedure using the F8 key to determine which line of code is actually causing the error.

mgm05267
11-15-2011, 08:24 PM
Hi...

As you are using bookmarks, it is safe to use as following:

ActiveDocument.Bookmarks(Selection.BookMarkID).Name


so that, it won't show the error..

Thanks & Regards,

MGM

dpcardiff
11-15-2011, 09:22 PM
Thank you both for your prompt responses!

mgm05267 - I'm trying to get the document containing doc variables to work. The one with bookmarks works well.

gmaxey - I did as you suggested and stepped through the initialize code. It stopped at the first line and said the object had been deleted. I checked all the doc variable fields in the document against the doc variable fields listed in the initialize code and all of the fields are present in the document.

Am I missing something?

DP

gmaxey
11-16-2011, 05:51 AM
DocVariable fields in a document are not the same as the "Variables" themselves. Have you actually created the variables themselvles?

ActiveDocument.Variables("Test").Value = "Test"

dpcardiff
11-16-2011, 09:42 PM
Hi Greg

I've got myself into a right knot with this code!

Now I have the situation where the form appears when the document is opened, but when text is entered into the form it's not appearing in the document. Well, that is apart from the following 3 fields:

ReporTitle
DocNo
RevDate Current

These fields are somehow populating themselves with text that has not been entered into the user form.

I have attached the code for This Document and User Form modules.

Do you think you could take a look at it when you get a moment? I'm sure I have doubled up on some code and am missing other bits. I'm new to this stuff.

Regards

DP

gmaxey
11-17-2011, 04:57 AM
Maybe I don't know where to look, but I don't see any attachements. Have you looked at: http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm

dpcardiff
11-17-2011, 02:47 PM

gmaxey
11-17-2011, 04:35 PM
It would be a whole lot easier if you attached the file (a doc version).

UserForm
Private Sub cmdCancel_Click()
Me.Hide
End Sub
Private Sub cmdOK_Click()
With ActiveDocument
.Variables("Var1").Value = Me.TextBox1.Text
.Variables("Var2").Value = Me.TextBox2.Text
'And so on ....
.Fields.Update
End With
Me.Hide
End Sub
Private Sub UserForm_Initialize()
PopulateControlFromVariable Me.TextBox1, "Var1"
PopulateControlFromVariable Me.TextBox2, "Var2"
'And so on ....
End Sub
Sub PopulateControlFromVariable(ByRef oCtr As Control, strName As String)
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
On Error GoTo Err_Handler
oCtr.Text = oVars(strName).Value
lbl_Exit:
Exit Sub
Err_Handler:
oCtr.Text = ""
Resume Next
End Sub

dpcardiff
11-17-2011, 05:41 PM
Hi Greg

Thanks for the code. I will give it a go.

I did attache the file. It is appearing in my post just before your last post.

I'll let you know how I get on.

DP

gmaxey
11-18-2011, 04:54 AM
Yes. But what you attached was a Word file that contained all of your code as plain text. Anyone wanting to help you would have to recreate the form and place the fields. Save your template as a document and attach the file containing the code modules, forms, and fields.

dpcardiff
11-20-2011, 03:58 PM
Hi Greg

I have saved the template as a document and attached for you to look at.

I really appreciate your help in this matter.

DP

gmaxey
11-20-2011, 07:13 PM
The code has been stripped out. Google me "Greg Maxey" and go to my website and send me a feedback message. I will then reply via e-mail