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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add a database attachment to an outlook mail 1

Status
Not open for further replies.

georgesOne

Technical User
Jul 2, 2004
176
JP
Hi All,

I have a database table with an attachment field. I want to e-mail this attachment... how can I do that?
I am familiar with the code for e-mails...: but the code ...Attachments Add... works only for files in folders:

With objOutlookMsg
.Attachments.Add "C:\Users\georgesOne\Desktop\Cost Reduction 2014.xlsx"
End With (= OK)

With objOutlookMsg
.Attachments.Add rst.Fields("CostDetails")
End With (= gives an error message: Error 3251 Operation is not supported for this type of object)

Any hint is welcome, georgesOne
 
What is the value of
Code:
rst.Fields("CostDetails")

I use this syntax all the time
Code:
        ' Add normal file attachments
        If cFiles.Count > 0 Then
            Call AddProgress("Adding standard attachments.")
            For Each vAtt In cFiles
                [highlight #FCE94F]oMsg.Attachments.Add (vAtt)[/highlight]
            Next
        End If
where a variable contains the string value of the path and file name to attach.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
You must save the attachment temporarily to disk, send the email, then delete from disk. Here is how to save to disk.
The rest is easy.
Code:
Public Sub SaveAttachToFile(rsAll As DAO.Recordset, attachmentFieldName As String, Optional SavePath As String = "")
  'An attachment field has a recordset of attachments stored behind the scenes
  Dim rsAtt As DAO.Recordset
  Dim fileName As String
  Set rsAtt = rsAll.Fields(attachmentFieldName).Value
  If rsAtt.BOF And rsAtt.EOF Then
    MsgBox "No Attachment"
    Exit Sub
  End If
  fileName = rsAtt.Fields("FileName").Value
  If SavePath = "" Then SavePath = CurrentProject.Path & "\"
  If Right(SavePath, 1) <> "\" Then SavePath = SavePath & "\"
  If Dir(SavePath & fileName) <> "" Then ' the file already exists--delete it first.
    If MsgBox("File already exists. Do you want to overwrite?", vbYesNo, "Overwrite?") = vbYes Then
      VBA.SetAttr SavePath & fileName, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
      VBA.Kill SavePath & fileName ' delete the file.
    Else
      Exit Sub
    End If
  End If
  rsAtt.Fields("FileData").SaveToFile (SavePath & fileName)
End Sub
 
Thanks to both of you...
I actually have found something similar to what MajP has posted (I can't see the star I gave you) and it worked as I wanted, so I guess that is the (only) solution.
It is good that it is posted now... thanks a lot.
But I have another question... will sleep over it and post it eventually tomorrow.

Best, georgesOne
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top