Agent to detach document from database and attach to email

I hope someone can offer some assistance. Still struggling with this agent. I need it to open the user mail client and attach the detached doc but I do not know how to do this.

I am getting to getting to “draft message has been created” running debugger and getting no errors, but it is not bringing up the email and attaching the doc.

Here is the code so far. Can someone please show me how to get the email agent to run and attach the document?

I appreciate your time, help and expertise.

Sub draft_msg

Dim ws As New notesuiworkspace

Dim session As New notessession

Dim uidoc As notesuidocument

Dim doc As notesdocument

Dim db2 As notesdatabase

Dim view2 As notesview

Dim doc2 As NotesDocument

Dim subj As Variant

Dim note As NotesRichTextItem

Dim item As NotesItem

Dim username As New NotesName(session.UserName)

strUser = username.canonical

server$ = session.GetEnvironmentString (“MailServer”, True) 'Get user’s home/mail server

mailfile$ = session.GetEnvironmentString (“MailFile”, True) 'Get user’s mail file name

Set db2 = session.getdatabase (server$,mailfile$)

’ Set view2 = db2.getview (“Drafts”)

Set doc2 = db2.createdocument

Call doc2.replaceItemValue(“Form”,“Memo”)

Call doc2.replaceItemValue(“Subject”,“test”)

Call doc2.save(True,False)

Call s_currentsession.UpdateProcessedDoc( doc_RecordDoc )

Messagebox “A draft message has been created with your selections.”

End Sub

Subject: agent to detach document from database and attach to email

Is some code missing? I don’t see anywhere you are attempting to detach / attach anything.

Subject: RE: agent to detach document from database and attach to email

Hi Alex;thank for your reply. Below is the entire agent “emaildocs”.

A user should be able to select a document, click the menu agent “emaildocs” and the document will be detached (copied) and attached to user email and sent for review. I have it detaching but cannot seem to fetch the user mail account for the attachment

function. I am getting the error “Type Mismatch”.

I appreciate any help.

Thank you very much in advance.

Declarations:

Dim s_currentsession As NotesSession

Dim db_thisDatabase As Notesdatabase

Dim doc_RecordDoc As Notesdocument

Dim str_runtime As String

Dim int_doccount As Long

Dim int_currentdoc As Long

Dim dc As NotesDocumentCollection

Initilze:

Sub Initialize

Call setup

’ Start with the first row and start looping

Call mainloop

End Sub

Mainloop:

Sub mainloop

Set doc_RecordDoc = dc.GetFirstDocument()

While Not(doc_RecordDoc Is Nothing)

Messagebox doc_RecordDoc.Titleopp(0), MB_OK, “Selecting”

Call detach

Call draft_msg

Call s_currentsession.UpdateProcessedDoc( doc_RecordDoc )

Set doc_RecordDoc = dc.GetNextDocument(doc_RecordDoc)

Wend

End Sub

Setup:

Sub setup

’ Create new objects where necessary and point to the correct view.

Set s_currentsession = New NotesSession

’ Connect to the database, create a query, and execute it.

Set db_thisDatabase = s_currentsession.CurrentDatabase

Set dc = db_thisDatabase.UnprocessedDocuments

End Sub

Detach:

Sub detach

Dim rtitem As Variant

Dim object As NotesEmbeddedObject

Dim handle As Variant

'…set value of doc…

Set rtitem = doc_RecordDoc.GetFirstItem( “deliverable” )

If ( rtitem.Type = RICHTEXT ) Then

Forall o In rtitem.EmbeddedObjects

If ( o.Type = EMBED_ATTACHMENT ) Then

Messagebox o.Source, MB_OK, “Detaching”

Call o.ExtractFile( "c:\temp" & o.Source )

End If

End Forall

End If

End Sub

Draft_msg:

Sub draft_msg

Dim ws As New notesuiworkspace

Dim session As New notessession

Dim uidoc As notesuidocument

Dim doc As notesdocument

Dim db2 As notesdatabase

Dim view2 As notesview

Dim doc2 As NotesDocument

Dim subj As Variant

Dim note As NotesRichTextItem

Dim item As NotesItem

Dim username As New NotesName(session.UserName)

strUser = username.canonical



server$ = session.GetEnvironmentString ("MailServer", True) 'Get user's home/mail server

mailfile$ = session.GetEnvironmentString ("MailFile", True) 'Get user's mail file name





Set db2 = session.getdatabase (server$,mailfile$)

’ Set view2 = db2.getview (“Drafts”)

Set doc2 = db2.createdocument

Call doc2.replaceItemValue("Form","Memo")

Call doc2.replaceItemValue("Subject","test")



Call doc2.save(True,False)

Call s_currentsession.UpdateProcessedDoc( doc_RecordDoc )



Messagebox "A draft message has been created with your selections." 

End Sub

Subject: RE: agent to detach document from database and attach to email

Hi Alex;I overhauled the old agent with this agent with just “initialize” code.

It sends a test message to the client’s email draft folder. I need it to physically open the email client and attach the document that has been selected in the database.

Can you take a peek–I think I am getting closer.

Thank you for any help.

Sub Initialize

On Error Goto errHandle

Dim session As New notessession

'Dim db2 As notesdatabase ← Renamed it as myMailDb for clarity

Dim myMailDb As NotesDatabase 

’ Dim doc2 As NotesDocument ← Renamed it as draftMailDoc for clarity

Dim draftMailDoc As NotesDocument 



Dim doc As notesdocument

'Set db2 = session.getdatabase (server$,mailfile$)

Set myMailDb = New NotesDatabase( "", "")

’ Lets open the mail file

Call myMailDb.OpenMail

’ Check we got the mail db open

If myMailDb.IsOpen = False Then 

	Msgbox "I was unable to open your mail file."

	Exit Sub

Else

	Msgbox "Have opened " & myMailDb.Title & " which is located " & myMaildb.Server & " " & myMailDb.FilePath 

End If

’ Create a draft mail doc

'Set doc2 = db2.createdocument

Set draftMailDoc = myMailDb.CreateDocument

'Call doc2.replaceItemValue(“Form”,“Memo”)

'Call doc2.replaceItemValue(“Subject”,“test”)

Call draftMailDoc.replaceItemValue("Form","Memo")

Call draftMailDoc.replaceItemValue("Subject","test")

’ Call doc2.save(True,False)

Call draftMailDoc.Save(True, False )



Messagebox "A draft message has been created with your selections."



Exit Sub

errHandle:

Msgbox "An error occurred on line" & Cstr( Erl) & Chr$(13) & "It said: " & Error$

Exit Sub

End Sub

Subject: RE: agent to detach document from database and attach to email

So it looks like all you need for now is the code to attach the extracted file attachments to the draft?

Alex

Subject: RE: agent to detach document from database and attach to email

Hi Alex;upon further testing, I am back to the original agent, the new one failed.

After selecting a documnet and clicking the menu agent, I have the agent sending the document to C:\temp and a test draft message to my draft folder. I need a new email message composition to open and the doc to be attached to it. Getting closer but stuck at this point.

Please show me how to do this if it can be done.

Thank you for your help.

Subject: RE: agent to detach document from database and attach to email

Here’s some example code for grabbing a file from the filesystem and attaching it to a document. Should help you:

’ Create the rich text item to store the attachment in

Dim BodyItem As NotesRichTextItem

Set BodyItem = New NotesRichTextItem(Doc2, “Body”)

’ Attach the extracted file to the document

Dim AttachmentObject As NotesEmbeddedObject

Set AttachmentObject = BodyItem.EmbedObject(EMBED_ATTACHMENT, “”, ExtractedFileName)

You’ll probably need to keep track of the attachments that have been extracted so you could create a global list variable for this.

For example let’s say you created the following global variables in Declarations:

Dim AttachmentsList List As String

Dim AttachmentNumber As Integer

Then at the following point of code:

Call o.ExtractFile( "c:\temp" & o.Source )

Add the following line

AttachmentNumber = AttachmentNumber + 1

AttachmentsList(Trim(Cstr(AttachmentNumber))) = "c:\temp" & o.Source

So now in the above code which performs the attaching you could do something like:

’ Create the rich text item to store the attachment in

Dim BodyItem As NotesRichTextItem

Set BodyItem = New NotesRichTextItem(Doc2, “Body”)

’ Attach the extracted files to the document

Dim AttachmentObject As NotesEmbeddedObject

Forall v In AttachmentsList

Set AttachmentObject = BodyItem.EmbedObject(EMBED_ATTACHMENT, "", v)

End Forall

Hope this helps.

Alex