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

How do you save the file to an attachment field via VBA

Status
Not open for further replies.

ToyFox

Programmer
Jan 24, 2009
161
0
0
US
I am trying to store a file in the attachment field with VBA. Does anyone have a sample code snipped that I can model after, I don't see any good examples on this site or others. Thx
 
Hi,
Code:
Sub dx()
Dim msg_txt As String, msg_sub As String, msg_to As String

msg_txt = "message text "

msg_sub = "message subject"
msg_to = "test@test.com"


CdoSend msg_to, "test@test.com", msg_sub, msg_txt


End Sub
'''''''''''''''''''''''''''''''''

Public Function CdoSend( _
    MailTo As String, _
    MailFrom As String, _
    Subject As String, _
    MessageText As String, _
    Optional CC As String, _
    Optional BCC As String, _
    Optional FileAttachment As String) As Boolean
On Error GoTo CdoSend_Err

' This example use late binding, you don't have to set a reference
' You must be online when you run the sub
    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant
 
    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")
 
        iConf.Load -1    ' CDO Source Defaults
        Set Flds = iConf.Fields
        With Flds
            .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
            .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "dfwmail.te.test.com"
            .Item("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
            .Update
        End With
 
    With iMsg
        Set .Configuration = iConf
        
        .To = MailTo
        .CC = CC
        .BCC = BCC
        .FROM = MailFrom
        
        .Subject = Subject
        .TextBody = MessageText

        
        If Len(FileAttachment & "") > 0 Then
            
            '## Last make sure the file actually exists and send it!:
            Dim fso
            Set fso = CreateObject("Scripting.FileSystemObject")
            If fso.FileExists(FileAttachment) Then
                .AddAttachment FileAttachment
            Else
                'otherwise return that the send failed and exit function:
                Debug.Print "[CdoSend.Error]=> File attachment path does not exist, quitting..."
                CdoSend = False
                Exit Function
            End If
        
        End If
    
        '## Send zee message! ##
        .sEnd
    
    End With

    Set fso = Nothing
    Set iMsg = Nothing
    Set iConf = Nothing
    
    CdoSend = True

CdoSend_Exit:
    Exit Function
    
CdoSend_Err:
    Debug.Print "[CdoSend.Error(" & Err.Number & ")]=> " & Err.Description
    CdoSend = False
    Resume CdoSend_Exit
End Function

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
If you are using Outlook (add a ref to the Outlook lib):
Code:
   1. Dim objOutlook As Object
   2. Dim objMail As Object
   3. Dim strTo As String
   4.  
   5. strTo = "me@me.com"
   6. Set objOutlook = Outlook.Application
   7. Set objMail = objOutlook.CreateItem(olMailItem)
   8.  
   9. With objMail
  10.     .To = strTo
  11.     .Subject = "Testing"
  12.     .Body = "Test msg."
  13.     .Attachments.Add "C:\pathname1"
  14.     .Attachments.Add "C:\pathname2"
  15.     .DeleteAfterSubmit = False
  16.     .Display
  17.     .Send
  18. End With
  19.  
  20. Set objMail = Nothing 
  21. Set objOutlook = Nothing

You would have to store the attachment C:\pathname, maybe with FileScripting code.

All I ask is a chance to prove that money can’t make me happy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top