Run-time error '-2147417851 (80010105)': Method 'To' of object'_MailItem' failed

USAMax

Well-known Member
Joined
May 31, 2006
Messages
843
Office Version
  1. 365
Platform
  1. Windows
This function has been working perfectly fine until now and nothing has changed. The OutMail.To function is failing and if I replace the Named Range with the string value of cell B11 it works. This fails everywhere this command uses a Named Range.

Rich (BB code):
Function Mail_workbook_Outlook(Recipient As String)
'Working in Excel 2000-2013
'Mail a copy of the ActiveWorkbook with another file name
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim wb1 As Workbook
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileExtStr As String
    Dim OutApp As Object
    Dim OutMail As Object

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    Set wb1 = ActiveWorkbook

    'Make a copy of the file/Open it/Mail it/Delete it
    'If you want to change the file name then change only TempFileName
    TempFilePath = Environ$("temp") & "\"
    TempFileName = "Johns New Item Form " & Format(Now, "mm-dd-yy")
    FileExtStr = "." & LCase(Right(wb1.Name, Len(wb1.Name) - InStrRev(wb1.Name, ".", , 1)))

    wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    'On Error Resume Next
    Select Case Recipient
        Case "Buyer"
            With OutMail
                .To = Range("SF.BuyerEmail")
                .cc = ""
                .BCC = ""
                .Subject = "Johns New Item Form " & Range("SF.SupplierContactName") & " " & Range("SF.SupplierInformation")(1) & " " & Format(Now(), "mm/dd/yyyy")
                .Body = "Attention " & Range("SF.BuyerName") & " -  Attached is the Johns New Item Form for your review."
                .Attachments.Add TempFilePath & TempFileName & FileExtStr
                .Send   'or use .Display
            End With
        Case "Supplier"
            With OutMail
                .To = Range("SF.SupplierEmail")
                .cc = ""
                .BCC = ""
                .Subject = "Johns New Item Form " & Range("SF.SupplierContactName") & " " & Range("SF.SupplierInformation")(1) & " " & Format(Now(), "mm/dd/yyyy")
                .Body = "Attention " & Range("SF.SupplierContactName") & " -  Attached is the Johns New Item Form for your review."
                .Attachments.Add TempFilePath & TempFileName & FileExtStr
                .Send   'or use .Display
            End With
        Case "Final"
            With OutMail
                .To = "MDSM@Johns.com"
                .cc = ""
                .BCC = ""
                .Subject = "Johns New Item Form " & Range("SF.BuyerName") & " " & Range("SF.SupplierInformation")(1) & " " & Format(Now(), "mm/dd/yyyy")
                .Body = "Attention " & Range("SF.SupplierContactName") & " -  Attached is the Johns New Item Form for your review."
                .Attachments.Add TempFilePath & TempFileName & FileExtStr
                .Send   'or use .Display
            End With
        Case Else
            End
    End Select
    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
End Function
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I don't know but I suspect Outlook does not like something it finds.

Try to do it in pieces. Define a temporary string along with the other Dims, then assign the range to the string , finally assign the string to .To.

For example:

Code:
    Dim TempEmail As String
...
...
            With OutMail
                TempEmail = Range("SF.BuyerEmail")
                .To = TempEmail
...
...
            With OutMail
                TempEmail = Range("SF.SupplierEmail")
                .To = TempEmail

If it works then good, if not, then you will know which half of the operation the problem is in. That is, either in the part that assigns the range to the string or the string to the property.

Regards,
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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