VBA - Generate Email with Attachment (Runtime Error)

tomexcel1

New Member
Joined
Feb 22, 2018
Messages
47
Hi All

I have the following code that essentially generates an email with the current file as an attachment. It keeps coming up with a runtime error on the highlighted line below. I just cant seam to fix it, the code is in a user form if that makes any difference.

The Error is: Run-Time error '429' ActiveX component can't create object

Any help will be much appreciated!

VBA Code:
Private Sub CommandButton1_Click()

If Sheet11.Range("D83") > 0 Then
CONFIRM = MsgBox("Please fill in all fields before submitting", vbOKOnly + vbExclamation, "MISSING INFORMATION!")
Else

            Application.ScreenUpdating = False
            Application.EnableEvents = False
            
            Sheet2.Unprotect ("password")
            Sheet2.Range("A1").Select
            Range("D39").Value = Environ("UserName")
            Range("G39").Value = Now
            Range("D38").Value = Environ("UserName")
            Range("G38").Value = Now
            
            ActiveSheet.Shapes("CommandButton1").Visible = False
            ActiveSheet.Shapes("CommandButton3").Visible = True
            ActiveSheet.Shapes("CommandButton4").Visible = True
            Sheet2.Protect ("password")
            Sheet11.Range("AK2").Value = TeamName.Text
  
  
Dim wb1 As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim OutApp As Object
    Dim OutMail As Object


    Set wb1 = ActiveWorkbook

    TempFilePath = Environ$("temp") & "\"
    TempFileName = "File NAME -" & " " & Sheet2.Range("D9").Value & " - " & Format(Now, "dd.mm.yy h.mm")
    FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))
    
    strsubject = "Subject Name - " & Sheet2.Range("D9").Value & " - " & Sheet2.Range("D8").Value & " " & Sheet2.Range("G8")
    strbody = "Body Text"
    strto = Sheet11.Range("AK3").Value & "@email.co.uk"
    wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr

 [COLOR=rgb(226, 80, 65)]  [/COLOR][B][COLOR=rgb(226, 80, 65)] Set OutApp = CreateObject("Outlook.Application")[/COLOR][/B]
    Set OutMail = OutApp.CreateItem(0)

    On Error Resume Next
    With OutMail
        .To = strto
        .CC = ""
        .BCC = ""
        .Subject = strsubject
        .Body = strbody
        .Attachments.Add TempFilePath & TempFileName & FileExtStr
        .display
    End With
    On Error GoTo 0

    'Delete the file
    Kill TempFilePath & TempFileName & FileExtStr

    Set OutMail = Nothing
    Set OutApp = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
Unload Me
End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,215,366
Messages
6,124,514
Members
449,168
Latest member
CheerfulWalker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top