|
-
Feb 7th, 2012, 10:52 AM
#1
Thread Starter
Junior Member
[RESOLVED] [Outlook 2010 & VS2008] Create email with AutoSig
Hi,
I'm fairly new to the world of forums (& coding) so please bear with me if I fail on ettiquette.
I've searched the forums and the oracle & found a few posts that are similar to the issue I'm having but none of the advised fixes have proved successfull.(re-installing PIA's, re-registering just about every .dll & .ocx ever created)
I'm sure that I've missed something very simple on this but I've been at it for 3 days now and for the life of me I cannot figure it out.
When running the code below, I get the following error:
Unable to cast COM object of type
'Microsoft.Office.Interop.Outlook.InspectorClass' to interface type
'Microsoft.Office.Interop.Outlook.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID
'{00063001-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRRESULT: 0x80004002 (E_NOINTERFACE)).
Background:
- Recently upgraded from VS2005 & MsO2007 to VS2008 & MsO2010
- .NET conversion worked without (reported) error
Code snippet:
Code:
Imports System
Imports System.IO
Imports System.Management
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Outlook
Dim oMailApp As Interop.Outlook.Application
Dim oNS As Interop.Outlook.NameSpace = Nothing
Dim oFolder As Interop.Outlook.MAPIFolder = Nothing
Dim oFolderItems As Interop.Outlook.Items = Nothing
Dim oMail As Interop.Outlook.MailItem = Nothing
Dim oMailMoved As Interop.Outlook.MailItem = Nothing
Try
oMailApp = New Interop.Outlook.Application
oNS = oMailApp.GetNamespace("MAPI")
oFolder = oNS.GetDefaultFolder(Interop.Outlook.OlDefaultFolders.olFolderDrafts)
oFolderItems = oFolder.Items
oMail = oFolderItems.Add(Interop.Outlook.OlItemType.olMailItem)
oMailApp = oMail.GetInspector
If LCase(sPC) = "not known" Then
oMail.To = "anemailaddress"
Else
oMail.To = "anemailaddress"
oMail.CC = "anemailaddress"
End If
oMail.Subject = "A Subject"
oMail.BodyFormat = Interop.Outlook.OlBodyFormat.olFormatHTML
oMail.HTMLBody = "Some body text"
oMail.Importance = Interop.Outlook.OlImportance.olImportanceHigh
oMail.UnRead = True
' Move the item to Inbox and then save it there
oMailMoved = oMail.Move(oFolder)
oMailMoved.Save()
oMailMoved.Display(True)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
If Not IsNothing(oMailMoved) Then Marshal.ReleaseComObject(oMailMoved)
If Not IsNothing(oMail) Then Marshal.ReleaseComObject(oMail)
If Not IsNothing(oFolderItems) Then Marshal.ReleaseComObject(oFolderItems)
If Not IsNothing(oFolder) Then Marshal.ReleaseComObject(oFolder)
If Not IsNothing(oNS) Then Marshal.ReleaseComObject(oNS)
End Try
A couple of other points:
- Could this be anything to do with the fact the project location is not trusted?
- Under VS2005 the Microsoft.Office.Tools.Outlook namespace was imported, I can't seem to be able to find a reference to this to add to the project but seeing as it's not used (directly) I'm not sure if this matters either.
Any help would be hugely appreciated.
Many thanks
Lex.
Last edited by LexGixxer; Feb 8th, 2012 at 02:07 PM.
Reason: Removed pointless screenshot
-
Feb 7th, 2012, 12:15 PM
#2
Thread Starter
Junior Member
Re: [Outlook 2010 & VS2008] Creating an email and saving it to drafts
Probably should have mentioned, the error is thrown at the line
oMailApp = oMail.GetInspector
Apparently this is the way to make the email appear with a sig.
So I suppose the actual question is, how to I compose an Outlook 2010 email and have the sig included? It's set in the Outlook options to appear on all emails (new; forwards & replies)
-
Feb 8th, 2012, 02:03 PM
#3
Thread Starter
Junior Member
Re: [Outlook 2010 & VS2008] Creating an email and saving it to drafts
Ok after yet another day of banging my head against my monitors I've finally cracked it. Forget about all that inspector rubbish, that was a proper red herring. Just in case anyone else is interested here's the working code. I'd appreciate any comments where I've done things the long way or in not the most efficient way. I'm very much self taught but really only have the time to make things work rather than make things work properly...
Working code: 
Code:
Dim oItem As Object = CreateObject("Outlook.Application")
Dim oMailApp As Interop.Outlook.Application
Dim oNS As Interop.Outlook.NameSpace = Nothing
Dim oFolder As Interop.Outlook.MAPIFolder = Nothing
Dim oFolderItems As Interop.Outlook.Items = Nothing
Dim oMail As Interop.Outlook.MailItem = Nothing
Dim oMailMoved As Interop.Outlook.MailItem = Nothing
Dim oCBP As Office.Core.CommandBarPopup = Nothing
Dim oCBB As Office.Core.CommandBarButton = Nothing
Dim oCBC As Office.Core.CommandBarControls = Nothing
TryoMailApp = New Interop.Outlook.Application
oNS = oMailApp.GetNamespace("MAPI")
oFolder = oNS.GetDefaultFolder(Interop.Outlook.OlDefaultFolders.olFolderDrafts)
oFolderItems = oFolder.Items
oMail = oFolderItems.Add(Interop.Outlook.OlItemType.olMailItem)
oMail.Display() 'KEY STEP - Must display email first or sig not added
With oMail .To = "AnEmailAddress"
.Subject = "Subject Text"
.HTMLBody = "<p>Hello,</p><p>Here's my daily stats report: </p>" & _
"<p>Number of emails I ignored: <br />Meetings missed: <br />Files deleted by mistake: <br />" & _
"Presentations not upated: </p>" & .HTMLBody
.Importance = Interop.Outlook.OlImportance.olImportanceHigh End With
oMailMoved = oMail.Move(oFolder)
oMailMoved.Save()
oMail.UnRead = True Catch ex As System.ExceptionSystem.Windows.Forms.MessageBox.Show(ex.Message) FinallyIf Not IsNothing(oMailMoved) Then Marshal.ReleaseComObject(oMailMoved)
If Not IsNothing(oMail) Then Marshal.ReleaseComObject(oMail)
If Not IsNothing(oFolderItems) Then Marshal.ReleaseComObject(oFolderItems)
If Not IsNothing(oFolder) Then Marshal.ReleaseComObject(oFolder)
If Not IsNothing(oNS) Then Marshal.ReleaseComObject(oNS) End Try
Almost too simple really. Slightly ashamed it took me 3 days to figure it out
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|