Object Variable Not Set Error -- I am probably blind today

Good Day All,

I am stumped. I have code that is called from a button click in document A, it finds and extracts the attachments from document B, then it is supposed to come back to document A and embedded all the attachments from the specific folder.

So far I am getting the attachments to detach from B. I can see them in the correct folder. I can see that B closes and I am back at A. However when it gets near my line of code I keep getting an Object Variable Not Set error at the If (rtitem2.Type = RICHTEXT) Then line. I am not seeing what it is that I haven’t set/declared so would appreciate an extra set of eyes. Along with the error, the attachments are not getting embedded in the Attach1 field.

Sub Initialize

Dim workspace As New notesuiworkspace

Dim session As New NotesSession

Dim CC_db As NotesDatabase



Dim CurDoc As NotesDocument

Dim uidoc As NotesUIDocument, uidocNew As NotesUIDocument

Dim doc As NotesDocument

Dim w As NotesUIWorkspace

Dim view As NotesView

Dim key As Variant



Set uidoc = workspace.CurrentDocument  

Set doc = uidoc.Document

Call doc.Save(True, True)

key = "7C480947F9DA63F48825753D005A55E9" 



Set CC_db = session.CurrentDatabase     

Set view = CC_db.GetView( "(Links)" )  

Set CurDoc = view.GetDocumentByKey(key , True )



If CurDoc Is Nothing Then

	Messagebox "No such document database.",,"document not Found"

	Exit Sub  

Else

	Call workspace.EditDocument(False, CurDoc)

	

	Set sess = New NotesSession

	Set db = sess.CurrentDatabase

	Set uidocNew = workspace.CurrentDocument 

	Dim rtitem As Variant

	Dim object As NotesEmbeddedObject

	

	Set rtitem = Curdoc.GetFirstItem( "Link" )

	Dim eOArray As Variant 

	eOArray = rtitem.EmbeddedObjects 

	If Isempty(eOArray)Then 

		

	Else 

		If ( rtitem.Type = RICHTEXT ) Then 

			

			Forall o In rtitem.EmbeddedObjects 

				If ( o.Type = EMBED_ATTACHMENT ) Then  

					fn = o.source

					nam = fn  '**

					Do While fileName$ <> ""   

						fileName$ = Dir$()

						count = count +1

					Loop 

					splitName = Split(nam,".",2)    

					nam = splitName(0) &"_" & count & "." & splitName(1) 

					Call o.ExtractFile( "C:\temp\"& nam )

				Else

				End If

			End Forall

			

		End If

		

		Dim uidoc1 As NotesUIDocument

		Set uidoc1 = workspace.CurrentDocument

		Call uidoc1.Close 

		

		Dim rtitem2 As Variant

		Dim object2 As NotesEmbeddedObject

		Dim uidoc2 As NotesUIDocument

		Dim doc2 As NotesDocument

		

		Set uidoc2 = workspace.CurrentDocument  

		Set doc2 = uidoc2.Document

		'*******************************************************************

		Set rtitem2 = doc2.GetFirstItem( "Attach1" )

		

		If ( rtitem2.Type = RICHTEXT ) Then     ' OBJECT VARIABLE NOT SET

			Set object2 = rtitem2.EmbedObject( EMBED_ATTACHMENT, "", "C:\temp\" & nam )   

		End If

		

		'*******************************************************************

	End If

End If

End Sub

Thanks in advance for any help.

Teri

Subject: Object Variable Not Set Error – I am probably blind today.

If you’re getting the error at the ‘if (rtitem2.Type = RICHTEXT) Then’ statement, then your rtitem2 is probably not set. Turn on debug, then once you encounter the error, check the your variables. Are you sure there is an “Attach1” item on doc2?

Patrick

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Patrick,

Thanks for helping.

I double checked my spelling on the field Attach1 on doc2 and it is correct. I verified that it is also a rich text field.

The debugger is setting/showeing the RTITEM2 as a [NOTESITEM]. when it goes on the line: Set rtitem2 = doc2.GetFirstItem(“Attach1”)

Then it gives me the error on the next line of code: If (rtitem2.Type = RICHTEXT) then

Does that mean it is not finding the Attach1 field? I noticed on the RTITEM variable it would refer to the field name.

Teri

Subject: RE: Object Variable Not Set Error – I am probably blind today.

It does sound like it is setting the rtitem2 correctly.

Maybe it’s failing on the line after that? Do you have a default view set in the database? It’s a requirement per the help file when using the EmbedObject method.

Patrick

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Yes, I have a default view set. What else can I check??? :0)

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Patrick has misunderstood you; I believe he thought you were saying that you could see the value of the item name of rtitem2 in the debugger. In fact, you referred to rtitem in that context.

Does that mean it is not finding the Attach1 field?

Yes, that is exactly what it means. You must check whether the variable “is nothing” before trying to use it.

With this error message, the approach for solving it is to debug to find what line it occurs on (or use an On Error trap to display the line number, if debugging is not practical in the situation) and look for the variable that appears before the “.”. Since your line of code is:

If (rtitem2.Type = RICHTEXT) then

and there’s only one “.”, you can confidently conclude that the variable rtitem2 contains the value Nothing. So you look at the previous line where it was assigned to figure out what went wrong.

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Issue solved, but now I have another problem.

I found that I was declaring doc2, but needed to use my original declaration for the original form or doc. So, now it is finding the Attach1 field. I will post the code at the bottom.

However, I noticed that the formula was only grabbing one of the attachments from the folder when there are two. Eventually there will be 4-5 files to grab and embed. So I put in a loop for that.

Through the debugger: I see through the loops that it has created then found the attachments within the specified file: C:\temp*.*. It writes them to Object2. The RTITEM2 shows [Attach1,1,“-emplyLeave_Colvin.jpg”,2,…]. However, when I save and open the document. The attachments are not in the field. They are at the bottom of the document. Sometimes the name is on the attachment, other time an arbitrary name such as ATTWZ84V, is at the bottom. When it is the arbitrary name then, you can not open it since it does not have the .jpg designation.

So,

One - How can I get the file to attach each time without losing the name value.

Two - How can I get the multiple attachments in the specified field (yes it is rich text, and yes it is finding the field now).

Thanks for the help.

Teri

Updated Code:

Sub Initialize

Dim workspace As New notesuiworkspace

Dim session As New NotesSession

Dim CC_db As NotesDatabase



Dim CurDoc As NotesDocument

Dim uidoc As NotesUIDocument, uidocNew As NotesUIDocument

Dim doc As NotesDocument

Dim w As NotesUIWorkspace

Dim view As NotesView

Dim key As Variant



Set uidoc = workspace.CurrentDocument  

Set doc = uidoc.Document

Call doc.Save(True, True)

key = "7C480947F9DA63F48825753D005A55E9" 

who = doc.EmNameLast(0)

Set CC_db = session.CurrentDatabase     

Set view = CC_db.GetView( "(Links)" )  

Set CurDoc = view.GetDocumentByKey(key , True )



If CurDoc Is Nothing Then

	Messagebox "No such document database.",,"document not Found"

	Exit Sub  

Else

	Call workspace.EditDocument(False, CurDoc)

	

	Set sess = New NotesSession

	Set db = sess.CurrentDatabase

	Set uidocNew = workspace.CurrentDocument 

	Dim rtitem As Variant

	Dim object As NotesEmbeddedObject

	

	Set rtitem = Curdoc.GetFirstItem( "Link" )

	Dim eOArray As Variant 

	eOArray = rtitem.EmbeddedObjects 

	If Isempty(eOArray)Then 

		

	Else 

		If ( rtitem.Type = RICHTEXT ) Then 

			

			Forall o In rtitem.EmbeddedObjects 

				If ( o.Type = EMBED_ATTACHMENT ) Then  

					fn = o.source

					nam = fn  '**

					splitName = Split(nam,".",2)    

					nam = splitName(0) &"_" & who & "." & splitName(1) 

					Call o.ExtractFile( "C:\temp\"& nam )

				Else

				End If

			End Forall

			

		End If

		

		Dim uidoc1 As NotesUIDocument

		Set uidoc1 = workspace.CurrentDocument

		Call uidoc1.Close 

		

		Dim rtitem2 As Variant

		Dim object2 As NotesEmbeddedObject

		

		'*******************************************************************

		Set rtitem2 = New NotesRichTextItem( doc, "Attach1" )                                 'doc.GetFirstItem( "Attach1" )

		

		If ( rtitem2.Type = RICHTEXT ) Then   

			Dim pathName As String, fileName As String

			pathName$ = "C:\temp\*.*"

			fileName$ = Dir$(pathName$, 0)

			Do While fileName$ <> ""

				fileName$ = Dir$()

				Set object2 = rtitem2.EmbedObject( EMBED_ATTACHMENT, "", "C:\temp\" & fileName$)   

				

			Loop	

			

		End If

		

		'*******************************************************************

	End If

End If

End Sub

Subject: RE: Object Variable Not Set Error – I am probably blind today.

I suggest you try CopyItemToDocument method, which will let you copy the entire rich text field, including attachments, at one go.

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Thanks Andre, This does paste the attachments in the document but not in the specified field. It is pasting at the bottom of the form not in the field Attach1.

Here is my new code. Obviously I don’t have something set correctly. There is no error. When I run debugger on it, (I put in a message box at the end of my code so I could try to see what was happening on the last line) the RTITEM2 which is the Attach1 field, shows that the attachments have been added. But, as stated when the document is saved and opened, the attachments are at the bottom.

Thanks again for your help,

Teri

Sub Initialize

Dim workspace As New notesuiworkspace

Dim session As New NotesSession

Dim CC_db As NotesDatabase



Dim CurDoc As NotesDocument

Dim uidoc As NotesUIDocument, uidocNew As NotesUIDocument

Dim doc As NotesDocument

Dim w As NotesUIWorkspace

Dim view As NotesView

Dim key As Variant



Set uidoc = workspace.CurrentDocument  

Set doc = uidoc.Document

Call doc.Save(True, True)

key = "7C480947F9DA63F48825753D005A55E9" 

who = doc.EmNameLast(0)

Set CC_db = session.CurrentDatabase     

Set view = CC_db.GetView( "(Links)" )  

Set CurDoc = view.GetDocumentByKey(key , True )



If CurDoc Is Nothing Then

	Messagebox "No such document database.",,"document not Found"

	Exit Sub  

Else

	Call workspace.EditDocument(False, CurDoc)

	

	Set sess = New NotesSession

	Set db = sess.CurrentDatabase

	Set uidocNew = workspace.CurrentDocument 

	Dim rtitem As Variant

	Dim object As NotesEmbeddedObject

	Set rtitem = Curdoc.GetFirstItem( "Link" )

	Dim uidoc1 As NotesUIDocument

	Set uidoc1 = workspace.CurrentDocument

	Call uidoc1.Close 

	

		'*******************************************************************

	Dim rtitem2 As Variant

	Set rtitem2 = New NotesRichTextItem( doc, "Attach1" )                            

	Call rtitem.CopyItemToDocument(doc, "Attach1")

	

End If

End Sub

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Delete this line:Set rtitem2 = New NotesRichTextItem( doc, “Attach1” )

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Ok. I deleted that line. I looked in the Variables. Found the items within the Doc. I found the Attach field which I see

  • [5] [“Attach1”, 1, “”, 104, False, False, False, …]

    • EMBEDDEDOBJECTS [NotesembeddedObject()]

      • [0] [$File", 1084, 82, True, True, True, False,…]

And so on.

I Save and close the form, open again, and the attachments 2 of them are not in the field Attach1. Instead they are on the bottom of the page not in a field.

What next?

Teri

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Sorry, I didn’t look carefully enough at what you were doing. When you do a back-end update to a rich text field, it’s not like other fields – you have to close and reopen the document before it displays. See the Update rich text tip and Front-end file attachments in LotusScript.

Moreover, because the document will already have an Attach1 field on it, you have to delete the old one. Don’t create new items with the same names as existing items.

Also, I don’t understand why you’re opening the document you look up on screen. You don’t need to do this to read data from it.

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Thanks for sticking with me Andre. It is now working perfectly, with your guidance.

One more question though. If I want to copy attachments to a mail file will this same concept work?

Thanks again,

Teri

Subject: RE: Object Variable Not Set Error – I am probably blind today.

I don’t see why not, though if you’re adding to an existing Body field in a document, you may find you need to use AppendRTItem to preserve the existing contents.

Subject: RE: Object Variable Not Set Error – I am probably blind today.

Andre and Willie,

I have to take off for now, but will get back at this tomorrow.

I will start backtracking to see if I can get the rtitem2 to “is nothing” the Attach1 field. As Andre suggested. I will update when I either have it or hit my head on the wall a time or two more.

Cheers

Teri