the code below goes into a form for user to choose the file to email.
the Pickfile() allows user to choose the file
the Email1 sends it, but NOTE: YOU MUST ADD THE OUTLOOK APP IN REFERENCES!!
vTo = "bob@acme.com"
vSubj = "your file"
vBody = "here is the data from the thing"
vFile = UserPick1File("c:\folder")
if vFile <> "" then Call Email1(vTo, vSubj, vBody, vFile)
paste the code below into a public module for all to use
Code:
Public Function UserPick1File(pvPath)
Dim strTable As String
Dim strFilePath As String
Dim sDialog As String, sDecr As String, sExt As String
If IsMissing(pvPath) Then pvPath = "c:\"
With Application.FileDialog(msoFileDialogFilePicker) 'MUST ADD REFERENCE : Microsoft Office 11.0 Object Library
.AllowMultiSelect = False
.Title = "Locate a file to Import"
.ButtonName = "Attach"
.Filters.Clear
.Filters.Add sDecr, sExt
.InitialFileName = pvPath
.InitialView = msoFileDialogViewList 'msoFileDialogViewThumbnail
If .show = 0 Then
'There is a problem
Exit Function
End If
'Save the first file selected
UserPick1File = Trim(.SelectedItems(1))
End With
End Function
'-------
'YOU MUST ADD THE OUTLOOK APP IN REFERENCES!!! checkmark OUTLOOK OBJECT LIBRARY in the vbE menu, Tools, References
'-------
Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody,optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem
On Error GoTo ErrMail
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)
With oMail
.To = pvTo
.Subject = pvSubj
.Body = pvBody
If Not Ismissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
.Send
End With
Email1 = True
Set oMail = Nothing
Set oApp = Nothing
Exit Function
ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume Next
End Function