Temporarily Save File in Attachment Field to Email

tfisher9180

New member
Local time
Today, 01:26
Joined
Jan 7, 2016
Messages
2
Hello,

I have an attachment field in one of my tables, the name of the attachment field is "Printout". I want to use the SaveToFile method so that I can temporarily save the file and reference it using CDO Message AddAttachment. I think I've almost got it, but in my email it's attaching a blank file with "noname", what am I doing wrong here? This is the code:

Code:
Public Function SaveAttachments(strPath As String, Optional strPattern As String = "*.*") As Long

Dim dbs As DAO.Database
Dim rst As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fld As DAO.Field2
Dim strFullPath As String
Dim sql As String

Set dbs = CurrentDb()
sql = "SELECT Printout FROM Grade"
Set rst = dbs.OpenRecordset(sql, dbOpenDynaset)
Set fld = rst("Printout")

Do While Not rst.EOF

Set rsA = fld.Value

Do While Not rsA.EOF
    If rsA("FileName") Like strPattern Then
        strFullPath = strPath & "\" & rsA("FileName")
        
        If Dir(strFullPath) = "" Then
            rsA("FileData").SaveToFile strFullPath
        SaveAttachments = SaveAttachments + 1
        End If
        
        rsA.MoveNext
    Loop
    rsA.Close
    
    rst.MoveNext
Loop

rst.Close
dbs.Close

Set fld = Nothing
Set rsA = Nothing
Set rst = Nothing
Set dbs = Nothing

End Function



And then I didn't attach all of my CDO code because it has the schema links and I'm new to the forum and can't post links. The CDO code works just fine though.

Code:
cdomsg.AddAttachment strFullPath
 
You might try changing:
Code:
cdomsg.AddAttachment strFullPath
...to:
Code:
[COLOR="Navy"]With[/COLOR] cdomsg
    .AddAttachment strFullPath
    .Attachments(1).Fields.Item("urn:schemas:mailheader:content-disposition") = _
        "attachment; filename=" & Dir(strFullPath)
    .Attachments(1).Fields.Update
[COLOR="navy"]End With[/COLOR]
 
Hello ByteMyzer!

When I throw that in I'm getting a mismatch error. Is the top part of the code about saving the file correct? It saves the attachment field?
 
To determine this, you might try stepping through the code in Debug mode, then right after executing the line rsA("FileData").SaveToFile strFullPath, try to open the file and see if it has been written correctly to the drive.
 

Users who are viewing this thread

Back
Top Bottom