Murray Brown
asked on
VB.net Excel Add-In Sending Outlook Email when Outlook is closed
Hi
I am using the following code in my VB.net Excel add-in to send an email. It works just fine
if Outlook is open but crashes when Outlook is closed. How can I modify the code to send it even if Outlook is close?
Imports Microsoft.Office.Interop.O utlook
Module modEmail
Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)
Try
' Create an Outlook application.
Dim oOutApp As Microsoft.Office.Interop.O utlook.App lication
oOutApp = New Microsoft.Office.Interop.O utlook.App lication
' Create a new MailItem.
Dim oMsg As Microsoft.Office.Interop.O utlook.Mai lItem
oMsg = oOutApp.CreateItem(Microso ft.Office. Interop.Ou tlook.OlIt emType.olM ailItem)
oMsg.Subject = oSubject
oMsg.Body = oBody
' TODO: Replace with a valid e-mail address.
oMsg.To = oTo
oMsg.CC = oCc
' Add an attachment
Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"
'Dim sBodyLen As String = oMsg.Body.Length
Dim sBodyLen As Integer = oBody.Length
Dim oAttachs As Microsoft.Office.Interop.O utlook.Att achments
Dim oAttach As Microsoft.Office.Interop.O utlook.Att achment
'only attach if there is something there
If oAttachment <> "" And oAttachment <> Nothing Then
oAttachs = oMsg.Attachments
If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAtt achment) = True Then
oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
End If
End If
' Send
oMsg.Send()
' Clean up
oOutApp = Nothing
oMsg = Nothing
oAttach = Nothing
oAttachs = Nothing
Catch
MsgBox("An error occurred in trying to send an email! " & Err.Description)
End Try
End Sub
End Module
I am using the following code in my VB.net Excel add-in to send an email. It works just fine
if Outlook is open but crashes when Outlook is closed. How can I modify the code to send it even if Outlook is close?
Imports Microsoft.Office.Interop.O
Module modEmail
Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)
Try
' Create an Outlook application.
Dim oOutApp As Microsoft.Office.Interop.O
oOutApp = New Microsoft.Office.Interop.O
' Create a new MailItem.
Dim oMsg As Microsoft.Office.Interop.O
oMsg = oOutApp.CreateItem(Microso
oMsg.Subject = oSubject
oMsg.Body = oBody
' TODO: Replace with a valid e-mail address.
oMsg.To = oTo
oMsg.CC = oCc
' Add an attachment
Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"
'Dim sBodyLen As String = oMsg.Body.Length
Dim sBodyLen As Integer = oBody.Length
Dim oAttachs As Microsoft.Office.Interop.O
Dim oAttach As Microsoft.Office.Interop.O
'only attach if there is something there
If oAttachment <> "" And oAttachment <> Nothing Then
oAttachs = oMsg.Attachments
If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAtt
oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
End If
End If
' Send
oMsg.Send()
' Clean up
oOutApp = Nothing
oMsg = Nothing
oAttach = Nothing
oAttachs = Nothing
Catch
MsgBox("An error occurred in trying to send an email! " & Err.Description)
End Try
End Sub
End Module
ASKER
Do you know which line is generating the error? Does outlook start? Before running this account when outlook is closed, is the outlook.exe in processes list in task manager?
ASKER
Hi
The error occurs at the line oMsg.Send.
Nothing visible happens. Outlook doesn't start.
outlook.exe is not in processes list in task manager
Thanks
The error occurs at the line oMsg.Send.
Nothing visible happens. Outlook doesn't start.
outlook.exe is not in processes list in task manager
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much
ASKER
Here's the working code for anyone that views this:
Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)
Try
' Create an Outlook application.
Dim oOutApp As New Microsoft.Office.Interop.O utlook.App lication
oOutApp = New Microsoft.Office.Interop.O utlook.App lication
Dim ns As Microsoft.Office.Interop.O utlook.Nam eSpace = oOutApp.GetNamespace("MAPI ")
Dim f As Microsoft.Office.Interop.O utlook.MAP IFolder
f = ns.GetDefaultFolder(OlDefa ultFolders .olFolderI nbox)
System.Threading.Thread.Sl eep(3000) 'a bit of startup grace time.
' Create a new MailItem.
Dim oMsg As Microsoft.Office.Interop.O utlook.Mai lItem
oMsg = oOutApp.CreateItem(Microso ft.Office. Interop.Ou tlook.OlIt emType.olM ailItem)
oMsg.Subject = oSubject
oMsg.Body = oBody
' TODO: Replace with a valid e-mail address.
oMsg.To = oTo
oMsg.CC = oCc
' Add an attachment
Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"
'Dim sBodyLen As String = oMsg.Body.Length
Dim sBodyLen As Integer = oBody.Length
Dim oAttachs As Microsoft.Office.Interop.O utlook.Att achments
Dim oAttach As Microsoft.Office.Interop.O utlook.Att achment
'only attach if there is something there
If oAttachment <> "" And oAttachment <> Nothing Then
oAttachs = oMsg.Attachments
If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAtt achment) = True Then
oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
End If
End If
' Send
oMsg.Send()
' Clean up
oOutApp = Nothing
oMsg = Nothing
oAttach = Nothing
oAttachs = Nothing
Catch
MsgBox("An error occurred in trying to send an email! " & Err.Description)
End Try
End Sub
Public Sub oSendEmail(ByVal oTo As String, ByVal oCc As String, ByVal oSubject As String, ByVal oBody As String, ByVal oAttachment As String)
Try
' Create an Outlook application.
Dim oOutApp As New Microsoft.Office.Interop.O
oOutApp = New Microsoft.Office.Interop.O
Dim ns As Microsoft.Office.Interop.O
Dim f As Microsoft.Office.Interop.O
f = ns.GetDefaultFolder(OlDefa
System.Threading.Thread.Sl
' Create a new MailItem.
Dim oMsg As Microsoft.Office.Interop.O
oMsg = oOutApp.CreateItem(Microso
oMsg.Subject = oSubject
oMsg.Body = oBody
' TODO: Replace with a valid e-mail address.
oMsg.To = oTo
oMsg.CC = oCc
' Add an attachment
Dim sSource As String = oAttachment 'eg "C:\Temp\Hello.txt"
'Dim sBodyLen As String = oMsg.Body.Length
Dim sBodyLen As Integer = oBody.Length
Dim oAttachs As Microsoft.Office.Interop.O
Dim oAttach As Microsoft.Office.Interop.O
'only attach if there is something there
If oAttachment <> "" And oAttachment <> Nothing Then
oAttachs = oMsg.Attachments
If oAttachment <> Nothing And oAttachment <> "" And System.IO.File.Exists(oAtt
oAttach = oAttachs.Add(sSource, , sBodyLen + 1)
'oAttach = oAttachs.Add(sSource, , sBodyLen + 1, sDisplayName)
End If
End If
' Send
oMsg.Send()
' Clean up
oOutApp = Nothing
oMsg = Nothing
oAttach = Nothing
oAttachs = Nothing
Catch
MsgBox("An error occurred in trying to send an email! " & Err.Description)
End Try
End Sub
oOutApp = New Microsoft.Office.Interop.O
What is the error you get?