More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 22-Nov-2017 16:46

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Outlook Form Design Printer Friendly Version
Title: Send a standard mail message from a custom form.
Description: To work around the issue of being able to display the contents of a message built from a custom form in the Outlook 2003 reading pane, this VBScript will create a new message based on the content of the custom form and send that instead.
Date: 23-Feb-2005  22:22
Code level: intermediate
Code area: Outlook Form Design
Posted by: Paul Scullard
This message is displayed as VB.NET
 Function Item_Send()

'Stop the custom form from sending
Item_Send = False

'Send a message with no code behind it
Send_Duplicate()

'Call the Item_Close Function to close the custom form
Item_Close()

End Function

Function Item_Close()

'Close the custom form without saving the message
Set objInsp = Item.GetInspector
objInsp.Close 1

End Function

Function Send_Duplicate()

'Create a new item using the standard mail form, and send that instead

'Variables for copying the recipients
Dim CountRecipients
Dim LoopRecipients
Dim NewRecipient

'Variables for copying the attachments
Dim fso
Dim objAtt
Dim fldTemp
Dim CountAttachments
Dim LoopAttachments

'Get the number of recipients and attachments for the loops
CountRecipients = Item.Recipients.Count
CountAttachments = Item.Attachments.Count

'Create a new item using the default mail form
Set sendMail = Application.CreateItem(0)

'Copy the contents of the custom form into the new message
With sendMail
	'Set body format to HTML
	.BodyFormat = 2
	
	'Copy the recipients
	For LoopRecipients = CountRecipients To 1 Step -1
		Set NewRecipient = .Recipients.Add (Item.Recipients(LoopRecipients).Address)
		NewRecipient.Type = Item.Recipients(LoopRecipients).Type
	Next

	.Recipients.ResolveAll	

	'Copy the from address
	.SentOnBehalfOfName = Item.SentOnBehalfOfName

	'Copy the attachments
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set fldTemp = fso.GetSpecialFolder(TemporaryFolder)

	For LoopAttachments = CountAttachments To 1 Step -1
		strFilePath = fso.BuildPath(fldTemp.Path, Item.Attachments(LoopAttachments).filename)

		If fso.FileExists(strFilePath) Then
			intLoc = InStrRev(Item.Attachments(LoopAttachments).filename, ".")
			strFileName = left(Item.Attachments(LoopAttachments).filename, intLoc - 1)
			strFileExt = Mid(Item.Attachments(LoopAttachments).filename, intLoc + 1) 
			I = 1
			Do
				strFilePath = fso.BuildPath(fldTemp.Path, strFileName & CStr(I) & "." & strFileExt)
				I = I + 1
			Loop While fso.FileExists(strFilePath)
		End If

		

		Item.Attachments(LoopAttachments).SaveAsFile strFilePath
		sendMail.Attachments.Add strFilePath, 1, , Item.Attachments(LoopAttachments).DisplayName
		fso.DeleteFile strFilePath
	Next

	Set objAtt = Nothing
	Set fldTemp = Nothing
	Set fso = Nothing

	'Copy the subject
	.Subject = Item.Subject

	'Copy the body
	.HTMLBody = Item.HTMLBody

	'Send the message
	.Send

End With

End Function
All 10comments
Page [ 1  
  24-Feb-2005  06:24   
Just wanted to say thanks to Sue for her help on this. I have lost count of the number of google searches that have turned up information she has posted. I thought it might be useful to post the code as it stands before it gets too bespoke.

PS
  24-Feb-2005  08:25   
That's very helpful, Paul. I can see a few issues that ought to be noted:

1) The technique above works only with HTML format messages. Plain text is problematic in versions before Outlook 2000, and RTF would require Redemption or CDO + a special DLL.

2) The technique assumes that any images in the message are linked not embedded. Sending an embedded image in an HTML message without showing an attached file requires a special technique; see http://www.outlookcode.com/d/code/htmlimg.htm

3) Of recent Outlook versions, only Outlook 2003 will perform the necessary operations without security prompts.
  14-Apr-2008  17:03   
This was extremely helpful for me, thank you very much!!!
  24-Jun-2008  08:14   
Is there any way to send mail with custom message class without opening outlook?
  27-Jun-2008  10:27   
Venkat, you can *send* a message using a technique like that at http://www.outlookcode.com/d/forms/skedrpt.htm but the message won't be *delivered* -- i.e. sent to the server for relay to the recipient -- unless Outlook is running.
  03-Jul-2008  01:49   
Thanks sue.
  04-Sep-2009  06:47   
Hi Sue,
I have used a very similar code in my system and the below code gives an error after clicking on the Send button .
Function Item_Close()
'Close the custom form without saving the message
Set objInsp = Item.GetInspector
objInsp.Close 1
End Function

The requirement is to cancel the actual form from being "Send" but to send it in a new mail in normal text format as the system which recieves the mail can only parse through the text format.The error that we get is :
Script Error
---------------------------
Unable to complete the command.

Line No:45

Is there any workaround for this ?
  04-Sep-2009  07:06   
karthik, you didn't say which version of Outlook you're using, but both Outlook 2003 and Outlook 2007 have had recent bugs that made the Close method unusable. Both now have been fixed with hotfixes and/or service packs. Details are on the "issues" page for each version on this site. Search at top left for Outlook 2003 issues, for example.
  06-Sep-2009  22:41   
Hi Sue,
I have used a very similar code in my system and the below code gives an error after clicking on the Send button .
Function Item_Close()
'Close the custom form without saving the message
Set objInsp = Item.GetInspector
objInsp.Close 1
End Function

The requirement is to cancel the actual form from being "Send" but to send it in a new mail in normal text format as the system which recieves the mail can only parse through the text format.The error that we get is :
Script Error
---------------------------
Unable to complete the command.

Line No:45

Is there any workaround for this ?
  11-Nov-2009  00:25   
This post saved my project and helped me get over the road block! THANK YOU!
Page [ 1