Runtime error when trying to attach a file to email (1 Viewer)

gmatriix

Registered User.
Local time
Today, 16:54
Joined
Mar 19, 2007
Messages
365
Hello All,

I am trying to attach a excel file to email. I have the file being exported to a location then I would like for the email to attach but I keep getting runtime saying that it has been moved or deleted....and the file is there...

Do I need to put a pause in or something to give time to save?

Any Ideas?

Code:
DoCmd.SetWarnings False

Dim FilNam As String
Dim Seeit As String
Dim iTemp As Integer

FilNam = "\\blah blah blah"

DoCmd.OpenQuery "la la report", acViewNormal, acEdit
DoCmd.OutputTo acOutputTable, "la la report", "Excel97-Excel2003Workbook(*.xls)", FilNam, False, "", , acExportQualityPrint

'Ignore errors to allow for error evaluation
    On Error Resume Next
    iTemp = GetAttr(FilNam)
    
'Check if error exists and set response appropriately
    Select Case Err.Number
    Case Is = 0
        FileOrDirExists = True
    Case Else
        FileOrDirExists = False
    End Select
    
     'Resume error checking
    On Error GoTo 0
    

    
If FileOrDirExists Then

Dim outl As Outlook.Application
Set outl = New Outlook.Application
Dim mi As Outlook.MailItem
Set mi = outl.CreateItem(olMailItem)
mi.HTMLBody = "<p>Hello,</p>" & "<p>Give me some chicken"
mi.Subject = "Ribs and Chicken report"
mi.To = "greezychick@blast.com"
mi.Send
mi.Attachments.Add ("The location where the file is at...lol")
Set mi = Nothing
Set outl = Nothing
Application.Quit
Else

Dim outll As Outlook.Application
Set outll = New Outlook.Application
Dim mi1 As Outlook.MailItem
Set mi1 = outl.CreateItem(olMailItem)
mi1.HTMLBody = "Hello"
mi1.Subject = "Tabot sauce"
mi1.To = "greezychick@blast.com"
mi1.Send
Set mi1 = Nothing
Set outll = Nothing
Application.Quit
End If


Proc_702_Auto_Qry_Err:
    MsgBox Error$
 

Isaac

Lifelong Learner
Local time
Today, 13:54
Joined
Mar 14, 2017
Messages
8,777
1. use something like Dir() or Scripting.Filesystemobject.FileExists() to check if a file exists. Don't hackaround with GetAttr and On Error stuff.

2. Try changing this line:
mi.Attachments.Add ("The location where the file is at...lol")
to
mi.Attachments.Add Filnam
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:54
Joined
Feb 28, 2001
Messages
27,175
You are not using this as a function (i.e. x = mi.attachments.add). You also are not using it as an explicit CALL,

I believe you need to omit the parentheses in this case. Sometimes parentheses force an oddball evaluation of the argument.

Code:
mi.Attachments.Add "The location where the file is at...lol"

EDIT: I see Isaac and I caught the same thing.
 

gmatriix

Registered User.
Local time
Today, 16:54
Joined
Mar 19, 2007
Messages
365
1. use something like Dir() or Scripting.Filesystemobject.FileExists() to check if a file exists. Don't hackaround with GetAttr and On Error stuff.

2. Try changing this line:
mi.Attachments.Add ("The location where the file is at...lol")
to
mi.Attachments.Add Filnam
Thank you I have removed the ( ) and I get the same thing....do I need to refresh something...folder maybe?
 

Isaac

Lifelong Learner
Local time
Today, 13:54
Joined
Mar 14, 2017
Messages
8,777
Please post your full, current code.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:54
Joined
Oct 29, 2018
Messages
21,469
Exactly!!! Thank you very much.....I changed it and wala!!! it works....Thanks to all!!
Hi. Congratulations! We were all happy to assist. Good luck with your project.
 

Users who are viewing this thread

Top Bottom