Detach doc and attach to email with agent

Hello;Hope someone can point me in the right direction. I appreciate any help. I am stuck trying to get a feature working on a database. All the folowing code below is for an 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 “Object Variable not set”.

My humble theory is something to do with

notesUIworkspace and noteswsworkspace going from a view to get user mail file.

Can someone please offer some feedback in simple terms as I am learning this as I go.

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 uidoc = ws.currentdocument

Set doc=uidoc.document

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

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

Set doc2 = db2.createdocument

doc2.Form = "Memo"



doc2.Subject ="test"



Call doc2.save(True,False)



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

End Sub

Subject: detach doc and attach to email with agent

Thanks for your help and patience.

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 you please show me how to get the email agent to run with the attached doc?

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: detach doc and attach to email with agent

Hi Paul,

Here is some code. Drop it in an agent , set the agent to run on all docs in the database and from action selection. Save the agent and run it from the actions menu.

The agent can be in any database you wish it to be in.

There will be a message box pop up to let you know which server and the name of the mail file the draft docs are being saved in. you should see thme in the draft folder / view or otherwise the all documents folder.

Let me know how it goes.

shane

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: detach doc and attach to email with agent

Shane;the test message is going to my Drafts folder ok. I need the email client to actually open up and attach the selected document (in this case a pdf) The documnet has to be attached (embeded)to the email for the user to send for review. Your script is working better than mine and is getting close. Can you please help me get the script to open the mail and attach. I am still scouring the help files but cannot find a simple solution…

Thank you for your help

Subject: RE: detach doc and attach to email with agent

Hi Paul,

The class you want is the NotesRichTextitem class. Ata llows you to embedd files into the Body field of a memo (remembering the body field is an RT item).

The code below asks you to browse for the file to append but it could be easily adapted to a sub that has the file path passed in as a parameter.

It opens the mail db, creates a memo, gets a file path by asking the user to browse for the file then appends file to the memo and opens the memo for editing.

The filePath variable is declared as a variant as the workspace.Editdocument returns an array of strings. by changing one of the first paramaeters in that call you can allow users to choose multiple files to append. This means you could do a for next loop to append multiple files if you want to expand this method. It is the same code as previously but have removed the old comments for clarity.

HTH,

Shane

On Error Goto errHandle

Dim w As New NotesUIWorkspace 	

Dim session As New notessession

Dim myMailDb As NotesDatabase

Dim draftMailDoc As NotesDocument

Dim doc As notesdocument

Dim rtf As NotesRichTextItem 

Dim filePath As Variant



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



filePath = w.OpenFileDialog( False, "Select The Document To Append")

If Isempty( filePath) Then 

	Msgbox "You have hnot selected a file to append ... aborting email creation.", 48, "No File Choosen"

	Exit Sub

End If



'They have choosen a file to append

Set draftMailDoc = myMailDb.CreateDocument

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

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



' We have the doc .set up.. now set up the richTextitem and then embed or attach the attachment

' constants used are EMBED_ATTACHMENT (1454) or 	EMBED_OBJECT (1453)  or EMBED_OBJECTLINK (1452)

' See the EmbedObject method in the develoepr help file	



Set rtf = New NotesRichTextItem( draftMailDoc, "Body")

Call rtf.AppendText("The file " & filePath(0) & " is attached here --> ")

Call rtf.EmbedObject(EMBED_ATTACHMENT,"", filePath(0) , "")

rtf.addNewLine(2)





'Save the draft document 	

Call draftMailDoc.Save(True, False )



'Edit the document

Call w.EditDocument( True, draftMailDoc, False,"", False, True)	





Exit Sub

errHandle:

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

Exit Sub

Subject: detach doc and attach to email with agent

what line is throwing the error?

Subject: RE: detach doc and attach to email with agent

it blows up at:set db2=session.getdatabase{server$,mailfile$}

what could be hanging it up?

Subject: RE: detach doc and attach to email with agent

If my brain was in gear …

Dim db As New NotesDatabase( “”, “” )

Call db.OpenMail

Messagebox( db.Title & " on server " & db.Server )

Sorry, brain fade, end of the day.

HTH,

Shane

Subject: RE: detach doc and attach to email with agent

Thank you for your responses.

Shane, I was following you in adding the line to:

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

if ucase(server$) = “LOCAL” then server$ = “”

but I am lost with where to put the error handling and :

Dim db As New NotesDatabase( “”, “” )

Call db.OpenMail

Messagebox( db.Title & " on server " & db.Server )

Can you please cut and paste your code and place it where it is supposed to be as I, to, am having a brian freeze…

Thank you for your help…

Subject: RE: detach doc and attach to email with agent

Hi Paul,

THe two posts were not meant to be in the same code … necessarily. THe error handling would give you an indication as to which line wass failing for debugging purposes. The second post was a quick cut and paste from the help file showing a method you can use to get the mail fiel without having to resort to environment variables.

With that in mind here are two versions of your code …one still using environment variables to get servver nad mial file location … the second using the NotesDatabase method to get the mail file. both have debugging code in place so if there is an error, it jumps to the error handle and tells you which line the eror is on.

'Code 1: - Using Environ Variables.

’ My lines preceeded by ‘**** SEH INSERT *****’ for clarity

’ **** SEH INSERT ****

On Error Goto errHandle

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 uidoc = ws.currentdocument

Set doc=uidoc.document

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

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

Set doc2 = db2.createdocument

doc2.Form = “Memo”

doc2.Subject =“test”

Call doc2.save(True,False)

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

’ **** SEH INSERT *****

’ Change next line to Exit function if it is a function and not a sub

exit sub

errhandle:

Print "Error on line ";Cstr(Erl); " Error = " ; Error$

’ the print line above could just as easily be a msgbox… as shown below - however remove it from your production code … doesn’t look good to end user.

MsgBox "Error on line "& cstr(Erl) & chr$(13) & " Error = " & Error$

’ Change next line to Exit function if it is a function and not a sub

exit sub

Exit Sub

’ *****************************************

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

'Code 2: - Using NotesDatabase method, and no environ vars

’ My lines preceeded by ‘**** SEH INSERT *****’ for clarity

’ deleted lines just remmed out for comparison '*** SEH DEL

’ **** SEH INSERT ****

On Error Goto errHandle

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

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

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

Set uidoc = ws.currentdocument

Set doc=uidoc.document

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

’ *** SEH INSERT

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

Call db2.OpenMail

Messagebox( db2.Title & " on server " & db2.Server  & " DB is open=" & Cstr( db2.IsOpen) )

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

Set doc2 = db2.createdocument

doc2.Form = “Memo”

doc2.Subject =“test”

Call doc2.save(True,False)

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

’ **** SEH INSERT *****

’ Change next line to Exit function if it is a function and not a sub

exit sub

errhandle:

Print "Error on line ";Cstr(Erl); " Error = " ; Error$

’ the print line above could just as easily be a msgbox… as shown below - however remove it from your production code … doesn’t look good to end user.

MsgBox "Error on line "& cstr(Erl) & chr$(13) & " Error = " & Error$

’ Change next line to Exit function if it is a function and not a sub

exit sub

Exit Sub

Subject: RE: detach doc and attach to email with agent

I goofed in the code 1 excert above … missed th eline checking the servername so the following is how it should look

Code 1: - Using Environ Variables.

’ My lines preceeded by ‘**** SEH INSERT *****’ for clarity

’ **** SEH INSERT ****

On Error Goto errHandle

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

’ **** THIS IS THE LINE I MISSED PUTTING IN *****

if ucase$( serverName ) = “LOCAL” then serverName$ = “”

Set uidoc = ws.currentdocument

Set doc=uidoc.document

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

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

Set doc2 = db2.createdocument

doc2.Form = “Memo”

doc2.Subject =“test”

Call doc2.save(True,False)

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

’ **** SEH INSERT *****

’ Change next line to Exit function if it is a function and not a sub

exit sub

errhandle:

Print "Error on line ";Cstr(Erl); " Error = " ; Error$

’ the print line above could just as easily be a msgbox… as shown below - however remove it from your production code … doesn’t look good to end user.

MsgBox "Error on line "& cstr(Erl) & chr$(13) & " Error = " & Error$

’ Change next line to Exit function if it is a function and not a sub

exit sub

Exit Sub

’ *****************************************

Subject: RE: detach doc and attach to email with agent

Hi Shane;thanks for your help. Getting closer I think.

I tried the Using NotesDatabase (without environmental variables–not sure if I need them) method exactly as shown below for the draft_msg section:

Getting : error line 22 Object variable not set.

Sub draft_msg

On Error Goto errHandle

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





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

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

Set uidoc = ws.currentdocument

Set doc=uidoc.document

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

’ *** SEH INSERT

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

Call db2.OpenMail

Messagebox( db2.Title & " on server " & db2.Server & " DB is open=" & Cstr( db2.IsOpen) )

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

Set doc2 = db2.createdocument

doc2.Form = "Memo"



doc2.Subject ="test"



Call doc2.save(True,False)



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

’ **** SEH INSERT *****

’ Change next line to Exit function if it is a function and not a sub

Exit Sub 

errhandle:

Print "Error on line ";Cstr(Erl); " Error = " ; Error$

’ the print line above could just as easily be a msgbox… as shown below - however remove it from your production code … doesn’t look good to end user.

Msgbox "Error on line "& Cstr(Erl) & Chr$(13) & " Error = " & Error$

’ Change next line to Exit function if it is a function and not a sub

Exit Sub 



Exit Sub 

End Sub

Subject: RE: detach doc and attach to email with agent

not sure, I copy/pasted your code (below) into a button and it worked fine for me, no errors

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 uidoc = ws.currentdocument

Set doc=uidoc.document

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

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

Set doc2 = db2.createdocument

doc2.Form = “Memo”

doc2.Subject =“test”

Call doc2.save(True,False)

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

Subject: RE: detach doc and attach to email with agent

It could be to do where your mail file is located or if you have a replica mail icon on your desktop.

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

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

server$ might be ‘Local’ or else some weird canoniacal result.

try adding the line as below …

server$ = session.GetEnvironmentString (“MailServer”, True)

if ucase(server$) = “LOCAL” then server$ = “”

Also add the following to your code… it will let you know what line is failing

On Error Goto errHandle

<place all code here>

exit sub

errhandle:

Print "Error on line ";Cstr(Erl); " Error = " ; Error$ 

Exit Sub

Subject: detach doc and attach to email with agent

I have tried a few things but getting “Type Mismatch” at the Detach stage…here is the new code with your suggestions

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



' Added these threelines 

Dim db As New NotesDatabase( "", "" )

Call db.OpenMail

Messagebox( db.Title & " on server " & db.Server )









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



' Added this line  

If Ucase(server$) = "LOCAL" Then server$ = ""



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



Set uidoc = ws.currentdocument

Set doc=uidoc.document

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

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

Set doc2 = db2.createdocument

doc2.Form = "Memo"



doc2.Subject ="test"



Call doc2.save(True,False)



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

End Sub