CreateObject("Outlook.Application")

H

Howard31

Hi,

I have procedure for sending mail. The problem is that the procedure will
work the first time it is executed but not the second time then the third
time it will work but not the fourth time and so on.

The sendEmail procedure is below.

Any help will be very much appreciated!

Public Sub sendMail()
On Error Resume Next

Dim oLApp As Object
Dim eMail As Object
Dim thisWb As String


thisWb = ThisWorkbook.Path & "\" & ThisWorkbook.Name
Set oLApp = CreateObject("Outlook.Application")
Set eMail = oLApp.CreateItem(olMailItem)

With eMail
.To = "(e-mail address removed)"
.Subject = "some subject"
.Attachments.Add (thisWb)
.Display
End With

Set eMail = Nothing
Set oLApp = Nothing
End Sub
 
B

Barb Reinhardt

Try this

Set olApp = Nothing
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
Set olApp = CreateObject("Outlook.Application")
End If


I'm betting that the first time you run it, Outlook is not open and the
second time it is.
 
B

Barb Reinhardt

FWIW, you can replace



thisWb = ThisWorkbook.Path & "\" & ThisWorkbook.Name

with

thisWS = Thisworkbook.fullname
 
H

Howard31

Thanks Barb Reinhardt! I'll try it out. By the way is "Thisworkbook.fullname"
availble in xl2003?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top