Error 438 - Object doesn't support this property or method (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 23:02
Joined
Aug 15, 2010
Messages
954
I have an Access FE and an SQL Express BE.

I tried using the following code which works fine in an Access13 FE and ACE BE.

I call the SendEmail fumction using

Code:
Dim varDum As Variant
    varDum = SendEmail("MyEmailExample.com", "", "", True, "", Me.strPath)

The SendEmail function
Code:
Function SendEmail(strTo As String, strSubject As String, strBody As String, bEdit As Boolean, _
                   Optional strBCC As Variant, Optional AttachmentPath As Variant)
'Send Email using late binding to avoid reference issues
   Dim objOutlook As Object
   Dim objOutlookMsg As Object
   Dim objOutlookRecip As Object
   Dim objOutlookAttach As Object
   Dim i As Integer
   Const olMailItem = 0

   On Error GoTo ErrorMsgs

   Set objOutlook = CreateObject("Outlook.Application")

   Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
   With objOutlookMsg

      .Subject = strSubject
      .Body = strBody
      .Importance = 2
    
      ' Add attachments to the message.
      If Not IsMissing(AttachmentPath) Then
        If IsArray(AttachmentPath) Then
           For i = LBound(AttachmentPath) To UBound(AttachmentPath) - 1
              If AttachmentPath(i) <> "" And AttachmentPath(i) <> "False" Then
                Set objOutlookAttach = .Attachments.Add(AttachmentPath(i))
              End If
           Next i
        Else
            If AttachmentPath <> "" Then
                '***********************************************************************
                Set objOutlookAttach = .Attachments.Add(AttachmentPath)  '**************
                '***********************************************************************
            End If
        End If
      End If

      If bEdit Then
        .display
      Else
        .Send
      End If
   End With

   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
   Set objOutlookRecip = Nothing
   Set objOutlookAttach = Nothing

ErrorMsgs:
      MsgBox Err.Number & " - " & Err.Description
      Exit Function
End Function

I receive the following error, on execution of the following line

Code:
Set objOutlookAttach = .Attachments.Add(AttachmentPath)



1687421700686.png
 

Josef P.

Well-known member
Local time
Today, 22:02
Joined
Feb 2, 2023
Messages
826
Probably MailItem.Attachments.Add does not know what to do with a textbox reference. ;-)

Code:
varDum = SendEmail("MyEmailExample.com", "", "", True, "", Me.strPath)
vs.
varDum = SendEmail("MyEmailExample.com", "", "", True, "", Me.strPath.Value)

/edit:
[OT]
Code:
If AttachmentPath(i) <> "" ...
When is a string (in VBA) smaller than ""?
=>
Code:
If AttachmentPath(i) > "" ...
 

JohnPapa

Registered User.
Local time
Today, 23:02
Joined
Aug 15, 2010
Messages
954
JosefP it did it, many thanks.

My previous code has been working for many years (ACCESS13 FE and ACE BE). Do you know why this is the case?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 04:02
Joined
May 7, 2009
Messages
19,245
also, your code is only sending "less than 1" attachment due to this:

Code:
 For i = LBound(AttachmentPath) To UBound(AttachmentPath) - 1

you should not deduct 1 since you are using Ubound().
 

cheekybuddha

AWF VIP
Local time
Today, 21:02
Joined
Jul 21, 2014
Messages
2,280
Josef and Arnel have suggested possible solutions which affect different parts of the code than you specify.

If those solutions don't solve your problem, then try and debug a little further. Add some code like:
Code:
' ...
            If AttachmentPath <> "" Then
                Debug.Print "AttachmentPath: ", Nz(AttachmentPath, "NULL")
                '***********************************************************************
                Set objOutlookAttach = .Attachments.Add(AttachmentPath)  '**************
                '***********************************************************************
            End If
' ...
Then run and check the Immediate Window (Crtr+G) for any output, and post back here.
 

JohnPapa

Registered User.
Local time
Today, 23:02
Joined
Aug 15, 2010
Messages
954
Josef and Arnel have suggested possible solutions which affect different parts of the code than you specify.

If those solutions don't solve your problem, then try and debug a little further. Add some code like:
Code:
' ...
            If AttachmentPath <> "" Then
                Debug.Print "AttachmentPath: ", Nz(AttachmentPath, "NULL")
                '***********************************************************************
                Set objOutlookAttach = .Attachments.Add(AttachmentPath)  '**************
                '***********************************************************************
            End If
' ...
Then run and check the Immediate Window (Crtr+G) for any output, and post back here.
As mentioned in #2 the problem was solved with the following replacement

Code:
varDum = SendEmail("MyEmailExample.com", "", "", True, "", Me.strPath)
vs.
varDum = SendEmail("MyEmailExample.com", "", "", True, "", Me.strPath.Value)

I had checked "AttachmentPath" and it arrived not Null
 

JohnPapa

Registered User.
Local time
Today, 23:02
Joined
Aug 15, 2010
Messages
954
also, your code is only sending "less than 1" attachment due to this:

Code:
 For i = LBound(AttachmentPath) To UBound(AttachmentPath) - 1

you should not deduct 1 since you are using Ubound().
You are correct, I wonder why was this the case. I will check. Thanks.
 

Users who are viewing this thread

Top Bottom