Userform Attach Item to Email: Runtime Error(s)

Qwest336

Board Regular
Joined
Jun 24, 2015
Messages
53
Hello all...

I have a Userform Module that calls GetOpenFilename to attach a file to an email. I used the following two subs to do that:

Code:
Private Sub cmdRescAttach_Click()
'The following code is for designating an attachment from the user's computer. Works in conjunction with Attachment.Add method in Outmail.
Dim strFileName As Variant
 
    ChDir "C:\" ' change to path to start dialog in
    
    strFileName = Application.GetOpenFilename(, , "Choose file for Escalation", , False)
    
    If TypeName(strFileName) = "String" Then
        tbRescDocAttached.Value = strFileName
    Else
        MsgBox "No attachment selected"
    End If

 End Sub

Code:
        .Attachments.Add EscalationsForm.tbRescDocAttached.Value & EscalationsForm.tbUHADocAttached.Value & EscalationsForm.tbRSCDocAttached.Value
        
    If tbRescDocAttached.Value <> "" Then
            .Attach tbRescDocAttached.Value
    ElseIf tbUHADocAttached.Value <> "" Then
            .Attach tbUHADocAttached.Value
    ElseIf tbRSCDocAttached.Value <> "" Then
            .Attach tbRSCDocAttached.Value
    End If
    End With

Now, it seems to work locally as, when I put the item in Display mode, the email shows with the attachment. However, when I put the form into Send mode, I get the following Run-time error:

SNAGHTMLf7d36f8.PNG


(Not sure if it is going to display)

"Run-time error '-2147221238 (8004010a)': The item has been moved or deleted."

The Error is breaking on the Attachments.Add property. However, when I add a breakpoint and hover, it does display the path to the selected document. Due to error-handling, the email does send but the attachment is not included.

In display mode, I get a Run-time 438 error message (Object doesn't support this property or method)on the If Else statement. However, again, the item does show as attached.

Does anyone have any suggestions for this? Any help is, as always, greatly appreciated!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
An educated guess:
Code:
    If tbRescDocAttached.Value <> "" Then
            .Attachments.Add tbRescDocAttached.Value
    ElseIf tbUHADocAttached.Value <> "" Then
            .Attachments.Add tbUHADocAttached.Value
    ElseIf tbRSCDocAttached.Value <> "" Then
            .Attachments.Add tbRSCDocAttached.Value
    End If
    End With
 
Upvote 0
Good morning John...thanks for the response!

I made that change today, and ran the form in Send mode and it still gave me the same runtime error.

"Run-time error '-2147221238 (8004010a)': The item has been moved or deleted."

It also broke on the same line:

Code:
.Attachments.Add EscalationsForm.tbRescDocAttached.Value & EscalationsForm.tbUHADocAttached.Value & EscalationsForm.tbRSCDocAttached.Value

As before, the email completed but the attachment was missing. Any additional ideas?

An educated guess:
Code:
    If tbRescDocAttached.Value <> "" Then
            .Attachments.Add tbRescDocAttached.Value
    ElseIf tbUHADocAttached.Value <> "" Then
            .Attachments.Add tbUHADocAttached.Value
    ElseIf tbRSCDocAttached.Value <> "" Then
            .Attachments.Add tbRSCDocAttached.Value
    End If
    End With
 
Upvote 0
I've tried to reproduce the problem with the file not being attached to the Outlook email, but all my tests have been successful.

Try using the routine below (called via a command button named "cmdSend" on the userform) to send/display the email:
Code:
Private Sub cmdSend_Click()

    Dim OutApp As Object 'Outlook.Application
    Dim OutMail As Object 'Outlook.MailItem

    Set OutApp = CreateObject("Outlook.Application") 'New Outlook.Application

    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = "an.email@address.com"  'CHANGE THIS TO A VALID EMAIL ADDRESS
        .Subject = "Subject here"
        .Body = "Email body text."
        
        If tbRescDocAttached.Value <> "" Then
            If Dir(tbRescDocAttached.Value, vbNormal) = "" Then MsgBox tbRescDocAttached.Value & " does not exist"
            .Attachments.Add tbRescDocAttached.Value
        ElseIf tbUHADocAttached.Value <> "" Then
            .Attachments.Add tbUHADocAttached.Value
        ElseIf tbRSCDocAttached.Value <> "" Then
            .Attachments.Add tbRSCDocAttached.Value
        End If

        .Send
        '.Display
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub
 
Upvote 0
Wow...I have no idea why, but that worked. I'm going to try to edit the sub to include the formatting of the email and the other things that the coding does prior to sending and test again. Thanks for this! I'll let you know if it works after the additions!

I've tried to reproduce the problem with the file not being attached to the Outlook email, but all my tests have been successful.

Try using the routine below (called via a command button named "cmdSend" on the userform) to send/display the email:
Code:
Private Sub cmdSend_Click()

    Dim OutApp As Object 'Outlook.Application
    Dim OutMail As Object 'Outlook.MailItem

    Set OutApp = CreateObject("Outlook.Application") 'New Outlook.Application

    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = "an.email@address.com"  'CHANGE THIS TO A VALID EMAIL ADDRESS
        .Subject = "Subject here"
        .Body = "Email body text."
        
        If tbRescDocAttached.Value <> "" Then
            If Dir(tbRescDocAttached.Value, vbNormal) = "" Then MsgBox tbRescDocAttached.Value & " does not exist"
            .Attachments.Add tbRescDocAttached.Value
        ElseIf tbUHADocAttached.Value <> "" Then
            .Attachments.Add tbUHADocAttached.Value
        ElseIf tbRSCDocAttached.Value <> "" Then
            .Attachments.Add tbRSCDocAttached.Value
        End If

        .Send
        '.Display
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub
 
Upvote 0
John_w....you are a life saver!!

Thank you so much. I integrated your If statement into the With Outmail Statement, adding the If Dir check into all three ElseIf conditions and, IT WORKED!

I appreciate your help a ton! I've learned a bunch from this site and, at some point, I'll have a second to pour through it and find out why it fixed the issue. But this solves an immediate issue that I had to get taken care of. Thanks so much for your time, effort, and knowledge!

Wow...I have no idea why, but that worked. I'm going to try to edit the sub to include the formatting of the email and the other things that the coding does prior to sending and test again. Thanks for this! I'll let you know if it works after the additions!
 
Upvote 0
Thank you so much. I integrated your If statement into the With Outmail Statement, adding the If Dir check into all three ElseIf conditions and, IT WORKED!
Thanks for the feedback.

The Dir function call would not cause the code to work, so it must be another part of your code. Dir simply checks whether a file exists, however the correct code should be:
Code:
        If tbRescDocAttached.Value <> "" Then
            If Dir(tbRescDocAttached.Value, vbNormal) = "" Then 
                MsgBox tbRescDocAttached.Value & " does not exist"
            Else
                .Attachments.Add tbRescDocAttached.Value
            End If
        ElseIf tbUHADocAttached.Value <> "" Then
            .Attachments.Add tbUHADocAttached.Value
        ElseIf tbRSCDocAttached.Value <> "" Then
            .Attachments.Add tbRSCDocAttached.Value
        End If
and repeat the same structure for the ElseIf parts.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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