Bringing up Outlook's New Message Dialog

dilbert0610

Member
Joined
Aug 3, 2006
Messages
22
Programming Experience
Beginner
I built an application that will search through a couple of folders for specific files. The user puts in a file name to search and it will find it. It allows wildcards and such so they can find the specific one without really knowing the name exactly how it is set. Once they click on it I have a new form open up and it displays the contents of the file (they are .pdf files so i use the acrobat add in). I had a user request that I add a button so that when he clicks it will open up his New Message dialog in outlook XP.

In Excel there is a feature to send the currenly open document as an attachment. When you click that option it will bring up the New Message dialog from outlook. This will give him the option to click the "To" button and use his address book to find the recipient. Does anyone know of a way I can do this? I need to basically add the file as an attachment to a new mail message and bring up the New message dialog from outlook so he can use the "To" button to find the recipient.

Thanks in advance.
 
You can do it with the Mailto protocol, just start a new process with the command line. Ex:
VB.NET:
Process.Start("mailto:recipient@domain.com")
The protocol is very limited with arguments, but some email client supports custom parameters. You can try the web for "Outlook mailto attachment".

Some email clients support command line switches. Outlook may work with /a switch to add attachment. Perhaps:
VB.NET:
Process.Start("Outlook.exe", "/a ""C:\My Documents\labels.doc""")
(a note for this that many email clients add a shortcut in the SendTo folder to support sending files directly from Explorer context menu as email attachment. Have a look a this shortcuts properties to see what command it does)
 
I use Office automation for a similar kind of task. Add a reference to this DLL:

Microsoft Outlook 11.0 Object Library

And here's some example code for connecting to Outlook and creating a new email with an attachment:

VB.NET:
'create application
Dim oOutlook As New ApplicationClass()

'connect to current namespace
Dim oNamespace As [Namespace] = oOutlook.GetNamespace("MAPI")
oNamespace.Logon()

'create a new email object
Dim mi As MailItem = oOutlook.CreateItem(OlItemType.olMailItem)
mi.To = "bob@harry.com"
mi.Body = "body text here"
mi.Subject = "Test Email"

'add attachments
mi.Attachments.Add("<path to file here>")

'pop up email dialog
mi.Display()

One drawback to this method is that Outlook (annoyingly) pops up a little dialog warning the user of scripted access to their email. Virus protection crap or something - but still some users won't like it.

Don't suppose anyone knows how to get Outlook to 'trust' an automating application?
 
Back
Top