First, I make sure no outlook is running. The "GetObject" will set the oOApp to outlook and the code works fine. One problem is that if I comment out the ".Display" the "Quit" will not stop the outlook process.
Second, if the outlook process is on, the "GetObject" and "CreateObject" failed, "Outlook.Application.CreateItem(olMailItem)" also failed.
I don't know why "GetObject" won't detect that outlook is on.
Does anyone know why?
Expand|Select|Wrap|Line Numbers
- Public Sub SendMail(strMailAddr As String, strNote As String)
- On Error GoTo ErrorHandler
- Dim oOApp As Outlook.Application
- Dim oOMail As Outlook.MailItem
- Dim bStart As Boolean
- bStart = False
- Set oOApp = GetObject("", "Outlook.Application")
- If oOApp Is Nothing Then
- Set oOApp = CreateObject("Outlook.Application")
- End If
- If oOApp Is Nothing Then
- Set oOMail = Outlook.Application.CreateItem(olMailItem)
- Else
- bStart = True
- Set oOMail = oOApp.CreateItem(olMailItem)
- End If
- If Not oOMail Is Nothing Then
- With oOMail
- .Subject = "NABP Call Message"
- .To = strMailAddr
- .body = strNote
- .Importance = olImportanceHigh
- .Display
- .Send
- End With
- If bStart Then
- oOApp.Quit
- End If
- Else
- MsgBox "Email sending is unsuccessful."
- End If
- Exit_ErrorHandler:
- ' clean up
- Set oOApp = Nothing
- Set oOMail = Nothing
- Exit Sub
- ErrorHandler:
- ' Display error information.
- MsgBox "(" & Err.Number & ") " & Err.Description
- Resume Next
- End Sub