Home > Back-end >  Problem Adding Attachment Using Variables with Outlook.MailItem
Problem Adding Attachment Using Variables with Outlook.MailItem

Time:12-16

I can't send email with attachments, if the path of the attachment isn't hard-coded into the vba, itself.

I get "error 438: Object doesn't support this property or method" at this line:

.Attachments.Add Me.LinkedFile1

On my form is a simple textbox called LinkedFile1.

Right now, Me.LinkedFile1 = "C:\Users\pat.lewis\Desktop\description.docx"

It works just fine when I send an email from Outlook using this code:

With olMail
    .To = Me.EmailTo
    .CC = Me.EmailCC
    .Subject = Me.EmailSubject
    .BodyFormat = olFormatPlain
    .Body = Me.EmailMessage
    .Attachments.Add "C:\Users\pat.lewis\Desktop\description.docx"
    .Send
End With

But it doesn't work if I assign the value of Me.LinkedFile1 to the .Attachments.Add property:

With olMail
    .To = Me.EmailTo
    .CC = Me.EmailCC
    .Subject = Me.EmailSubject
    .BodyFormat = olFormatPlain
    .Body = Me.EmailMessage
    .Attachments.Add Me.LinkedFile1 'DOESN'T WORK!!
    .Send
End With

It Also does not work if I assign the value of Me.LinkedFile1 to a string variable then reference that:

Dim olAttachment1 As String

If IsNull(Me.AttachedFile1) = False Then
    olAttachment1 = Me.LinkedFile1
End If

With olMail
    .To = Me.EmailTo
    .CC = Me.EmailCC
    .Subject = Me.EmailSubject
    .BodyFormat = olFormatPlain
    .Body = Me.EmailMessage
    .Attachments.Add olAttachment1 'DOESN'T WORK EITHER!!
    .Send
End With

I have confirmed multiple ways that the value of Me.LinkedFile1 does actually equal "C:\Users\pat.lewis\Desktop\description.docx".

Any ideas on what I could be missing here?

CodePudding user response:

Using UNBOUND textbox set with result of FilePicker works just fine for me.

Only issue with textbox I can think of that would cause this failure is corruption. If running Decompile/Compile and/or Compact & Repair and/or deleting & replacing textbox does not resolve, eliminate it from the process. Set olAttachment1 variable directly with result of FilePicker. You can still set the textbox if you want to display to user.

  • Related