Hey All,
Trying to send an email with an attachment. I keep getting this error
"Can't find this file. Make sure the path and file name are correct" They are correct I am using a code sample I found on the forum.
I'm stumped. HELP
Thanks
Rich
Here's the code.
Option Compare Database
Option Explicit
' You need to declare a reference to the Outlook library, and the filesystemobject.
' this is not as hard as it sounds.
'
' Look in the menu above, and click Tools, then select References
'
' Scroll down the list until you see
' Microsoft Scripting Runtime -- and put a check next to it (if one is not there already)
'
' Microsoft Outlook Object Library -- check that.
' There will be some version number there as well; it doesn't matter.
' This will work with Outlook98 and Outlook2000 and OutlookXP. It hasn't been tested on Outlook 2003 yet.
Public Function SendEmail()
Dim MailList As String
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
' First, we need to know the subject.
' We can't very well be sending around blank messages...
Subjectline$ = "Accurate Public Records, LLC."
' If there's no subject, call it a day.
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
' Now we need to put something in our letter...
'BodyFile$ = "C:\Proposal.Txt"
' If there's nothing to say, call it a day.
'If BodyFile$ = "" Then
'MsgBox "No body, no message." & vbNewLine & vbNewLine & _
' "Quitting...", vbCritical, "I Ain't Got No-Body!"
'Exit Function
'End If
' Check to make sure the file exists...
'If fso.FileExists(BodyFile$) = False Then
'MsgBox "The body file isn't where you say it is. " & vbNewLine & vbNewLine & _
'"Quitting...", vbCritical, "I Ain't Got No-Body!"
'Exit Function
'End If
' Since we got a file, we can open it up.
'Set MyBody = fs
penTextFile(BodyFile, ForReading, False, TristateUseDefault)
' and read it into a variable.
MyBodyText = "This Is A Test"
' and close the file.
'MyBody.Close
' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application
' This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it
MyMail.To = [Forms]![Marketing]!
'This gives it a subject
MyMail.Subject = Subjectline$
'This gives it the body
MyMail.Body = MyBodyText
'If you want to send an attachment
'uncomment the following line
MyMail.Attachments.add ("C:\APR Docs\APR Application"), olByValue, 1, "Apr Application"
'MyMail.Attachments.add "c:\dbgout.txt", olByValue, 1, "My Displayname"
' To briefly describe:
' "c:\myfile.txt" = the file you want to attach
'
' olByVaue = how to pass the file. olByValue attaches it, olByReference creates a shortcut.
' the shortcut only works if the file is available locally (via mapped or local drive)
'
' 1 = the position in the outlook message where to attachment goes. This is ignored by most
' other mailers, so you might want to ignore it too. Using 1 puts the attachment
' first in line.
'
' "My Displayname" = If you don't want the attachment's icon string to be "c:\myfile.txt" you
' can use this property to change it to something useful, i.e. "4th Qtr Report"
'This sends it!
'MyMail.Send
'Some people have asked how to see the e-mail
'instead of automaticially sending it.
'Uncomment the next line
'And comment the "MyMail.Send" line above this.
MyMail.Display
'Cleanup after ourselves
Set MyMail = Nothing
'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.
'MyOutlook.Quit
Set MyOutlook = Nothing
End Function
Trying to send an email with an attachment. I keep getting this error
"Can't find this file. Make sure the path and file name are correct" They are correct I am using a code sample I found on the forum.
I'm stumped. HELP
Thanks
Rich
Here's the code.
Option Compare Database
Option Explicit
' You need to declare a reference to the Outlook library, and the filesystemobject.
' this is not as hard as it sounds.
'
' Look in the menu above, and click Tools, then select References
'
' Scroll down the list until you see
' Microsoft Scripting Runtime -- and put a check next to it (if one is not there already)
'
' Microsoft Outlook Object Library -- check that.
' There will be some version number there as well; it doesn't matter.
' This will work with Outlook98 and Outlook2000 and OutlookXP. It hasn't been tested on Outlook 2003 yet.
Public Function SendEmail()
Dim MailList As String
Dim MyOutlook As Outlook.Application
Dim MyMail As Outlook.MailItem
Dim Subjectline As String
Dim BodyFile As String
Dim fso As FileSystemObject
Dim MyBody As TextStream
Dim MyBodyText As String
Set fso = New FileSystemObject
' First, we need to know the subject.
' We can't very well be sending around blank messages...
Subjectline$ = "Accurate Public Records, LLC."
' If there's no subject, call it a day.
If Subjectline$ = "" Then
MsgBox "No subject line, no message." & vbNewLine & vbNewLine & _
"Quitting...", vbCritical, "E-Mail Merger"
Exit Function
End If
' Now we need to put something in our letter...
'BodyFile$ = "C:\Proposal.Txt"
' If there's nothing to say, call it a day.
'If BodyFile$ = "" Then
'MsgBox "No body, no message." & vbNewLine & vbNewLine & _
' "Quitting...", vbCritical, "I Ain't Got No-Body!"
'Exit Function
'End If
' Check to make sure the file exists...
'If fso.FileExists(BodyFile$) = False Then
'MsgBox "The body file isn't where you say it is. " & vbNewLine & vbNewLine & _
'"Quitting...", vbCritical, "I Ain't Got No-Body!"
'Exit Function
'End If
' Since we got a file, we can open it up.
'Set MyBody = fs

' and read it into a variable.
MyBodyText = "This Is A Test"
' and close the file.
'MyBody.Close
' Now, we open Outlook for our own device..
Set MyOutlook = New Outlook.Application
' This creates the e-mail
Set MyMail = MyOutlook.CreateItem(olMailItem)
' This addresses it
MyMail.To = [Forms]![Marketing]!
'This gives it a subject
MyMail.Subject = Subjectline$
'This gives it the body
MyMail.Body = MyBodyText
'If you want to send an attachment
'uncomment the following line
MyMail.Attachments.add ("C:\APR Docs\APR Application"), olByValue, 1, "Apr Application"
'MyMail.Attachments.add "c:\dbgout.txt", olByValue, 1, "My Displayname"
' To briefly describe:
' "c:\myfile.txt" = the file you want to attach
'
' olByVaue = how to pass the file. olByValue attaches it, olByReference creates a shortcut.
' the shortcut only works if the file is available locally (via mapped or local drive)
'
' 1 = the position in the outlook message where to attachment goes. This is ignored by most
' other mailers, so you might want to ignore it too. Using 1 puts the attachment
' first in line.
'
' "My Displayname" = If you don't want the attachment's icon string to be "c:\myfile.txt" you
' can use this property to change it to something useful, i.e. "4th Qtr Report"
'This sends it!
'MyMail.Send
'Some people have asked how to see the e-mail
'instead of automaticially sending it.
'Uncomment the next line
'And comment the "MyMail.Send" line above this.
MyMail.Display
'Cleanup after ourselves
Set MyMail = Nothing
'Uncomment the next line if you want Outlook to shut down when its done.
'Otherwise, it will stay running.
'MyOutlook.Quit
Set MyOutlook = Nothing
End Function