Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending e mail with an attached file...

Status
Not open for further replies.

chiuso2

MIS
Feb 22, 2002
58
Hi there,

actually i use my application to send e mails, but i don't know how automatically attach a file ( always the same in the same path)

Thanks in advance for any suggestion

Mario from Italy
 
Use the SendObject Method. Search on this in help and it walks you through how to set it up. A code snipet is below:

Example

DoCmd.SendObject acSendTable, "Employees", acFormatXLS, _
"Nancy Davolio; Andrew Fuller", "Joan Weber", , _
"Current Spreadsheet of Employees", , False

 
If you have Outlook you can access its object library which is pretty powerful:

Code:
Public Function SendMailAttachment(ByVal strFile As String) As Boolean
On Error GoTo ErrHandler
  Dim ol As Outlook.Application
  Dim msg As Outlook.MailItem
  Dim fldOut As Outlook.MAPIFolder
  Dim cbrMenu As Office.CommandBar
  Dim cbrTools As Office.CommandBarPopup
  Dim cbrSend As Office.CommandBarControl
  Dim lngCount As Long
  Dim blnOpen As Boolean
  
  If Dir(strFile) <> &quot;&quot; Then
    
    On Error Resume Next
    Set ol = GetObject(, &quot;Outlook.Application&quot;)
    
    If Err = 0 Then
      blnOpen = True
    Else
      Set ol = CreateObject(&quot;Outlook.Application&quot;)
    End If
    
    On Error GoTo ErrHandler
   
    Set msg = ol.CreateItem(olMailItem)
    
    With msg
      .To = &quot;pnorton@symantec.om&quot;
      .Attachments.Add strFile
      .Subject = &quot;Please see attached file&quot;
      .Body = &quot;Open attached file to view virus&quot; & vbCrLf
      .Send   'This just places mail in outbox
    End With
    
    'open an explorer
    Call ol.Session.GetDefaultFolder(olFolderOutbox).GetExplorer.ShowPane(olFolderList, False)
    
    'count items in outbox
    Set fldOut = ol.Session.GetDefaultFolder(olFolderOutbox)
    lngCount = fldOut.Items.Count

    'capture the send action menu
    Set cbrMenu = ol.ActiveExplorer.CommandBars(&quot;Menu Bar&quot;)
    Set cbrTools = cbrMenu.Controls(&quot;Tools&quot;)
    Set cbrSend = cbrTools.Controls(&quot;Send&quot;)
    
    'Send now
    cbrSend.Execute
    
    If Not blnOpen Then
      Do While fldOut.Items.Count >= lngCount
        DoEvents
      Loop
    End If
    
    SendMailAttachment = True
    
  End If

ExitHere:
  On Error Resume Next
  Set msg = Nothing
  Set cbrSend = Nothing
  Set cbrTools = Nothing
  Set cbrMenu = Nothing
  If Not blnOpen Then
    ol.Quit
  End If
  Set fldOut = Nothing
  Set ol = Nothing
  Exit Function
ErrHandler:
  MsgBox &quot;Error (&quot; & Err & &quot;) - &quot; & Err.Description
  Resume ExitHere
End Function


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
It's my understanding that this method

'Send now
cbrSend.Execute

won't work after you've installed the Outlook E-mail Security Update.
 
I think it still works, although with the higher security settings a message box will alert the user that a program is trying to send mail using outlook and ask whether to proceed or not.

This could be acceptable for a single email but I'm not sure if you would get a prompt for each email if doing a mass mailout.

CDO might be a better choice if security is a problem.



VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top