Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not able to send mail if outlook is closed.

Status
Not open for further replies.

mbowler9

IS-IT--Management
Sep 8, 2003
105
US
I have the code below which works flawlessly when Outlook is open, but if Outlook is closed, it sends the message the next time I open Outlook, not immediately. By the way, I am using redemption. Any suggestions? Thanks


Public Sub MailIt(name As String)

Dim olItem As Outlook.MailItem
Dim olApp As Outlook.Application
Dim SafeMail, oItem
Dim isOpen As Boolean
Dim body As Variant
Dim olNamespace As Variant

On Error Resume Next

isOpen = True 'initialize boolean variable
'try to grab current instance of Outlook
Set olApp = GetObject(, "Outlook.Application")

'if Outlook isn't open, boolean is false, create instance of Outlook
If olApp Is Nothing Then
isOpen = False
Set olApp = CreateObject("Outlook.Application")
End If

On Error GoTo 0

'Create an instance of Redemption.SafeMailItem
Set SafeMail = CreateObject("Redemption.SafeMailItem")
Set olItem = olApp.CreateItem(olMailItem)
SafeMail.Item = olItem

'set variable "body" to text in textbox
body = Forms!frmAutoRun!txtResults.Value

With SafeMail
.Recipients.Add "test@email.com" 'Addresses separated by ;
.Subject = "Allmid EOM Archive Results" 'Subject
.body = body & vbCrLf & vbCrLf 'Body
.Attachments.Add name 'attachments
.Send 'send email
End With

Ex: On Error Resume Next
Set olItem = Nothing
Set SafeMail = Nothing

' if isOpen is False, then close Outlook
'Returns outlook to the same state as when
'this function started
If Not isOpen Then
olApp.Quit
Set olApp = Nothing
End If
Exit Sub

Er:
MsgBox "MailIt: " & Err & " " & Err.Description
GoTo Ex

End Sub
 
Hi,
You need to set a reference to it using Tools, References from inside the Visual Basic editor. This reference is called "Microsoft Office #.0 Object Library" (replace the # sign with the version you have - in Access 2000, this is 9.0).

HTH, [pc2]
Randy Smith, MCP
rsmith@cta.org
California Teachers Association
 
I set a reference to the 10.0 Library, as it was the only one present. I do have office XP loaded.....I wonder if the number is higher for the version of MS office rather than Access.

Unfortunately, it still waits until I open Outlook to send the email.

Any other suggestions?

Thanks
 
HI mbowler9,

you have an coding error that is causing your problem.

change this:

'****start****
isOpen = True 'initialize boolean variable
'try to grab current instance of Outlook
Set olApp = GetObject(, "Outlook.Application")

'if Outlook isn't open, boolean is false, create instance of Outlook
If olApp Is Nothing Then
isOpen = False
Set olApp = CreateObject("Outlook.Application")
End If
'****end code****

to something like:

'****start code****
On Error Resume Next

'try to grab current instance of Outlook
Set olApp = GetObject(, "Outlook.Application")

If Err.number <> 0 Then
Set objOutlook = CreateObject(&quot;Outlook.application&quot;)
End If
'*****end code****

the problem with the code is your GetObject is throwing an error and bypassing your IF statement.

I have tried my suggestion and it does create the instance of Outlook you need.

HTH

Have A Great Day!!!, [bigglasses]

Nathan
Senior Test Lead
 
Still no luck for some reason. I had to change the &quot;objOutlook&quot; to &quot;olApp&quot; since that is the variable that I am using.

I even tried using the following line by itself and it did not work:
Set olApp = CreateObject(&quot;Outlook.Application&quot;)

I am thinking that my references are not correct.
Unfortunately, I don't know much about what the references should be.

As I said above, I have the Microsoft Office 10.0 Object Library referenced. Should I be using the 8.0 Library? I am using Access 97 and Outlook 2002.


Thanks
 
I dont believe its your references. If you are using Outlook 2002, you have to use 10.0 Outlook library.

have you stepped through your code to see where it breaks? that is what i did and how i found the problem I identified.

I do not have Redemption loaded on my pc so I cant stepped fully through your code.

the other option is to remove the On Error Resume Next. This bypasses errors and will cause your code to fail somewhere else.

instead use: On Error GoTo ErrHanlder: <---goes at the top

at the bottom of the code put:

ErrHandler:

msgbox err.number & &quot; &quot; & err.description

This will show a message box with the error. thats not going to show you exactly in the code where it went wrong but at least might point you in a direction.

Since this code work while Outlook is running, I still believe somewhere in there your Outlook application is not being created when Outlook is not up.

If all else fails, post back your code again with the changes you made. In the mean time, I am going to do some more looking around. I look forward to hearing back from ya. :)

HTH

Have A Great Day!!!, [bigglasses]

Nathan
Senior Test Lead
 
I don't understand what is happening here. I stepped through the code and it doesn't break unless I remove the &quot;On Error Resume Next&quot;. All variables get updated correctly. When it tries to create a new instance of Outlook, it works fine (olApp=Outlook). I even tried using &quot;CreateObject(&quot;C:\directory to outlook\OUTLOOK.exe&quot;) which didn't work.

I will be doing some more investigating too. From what I have seen, other people are doing this the same way.

I am fairly confident that the redemption is correct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top