Need help with email function

Russ9517

Registered User.
Local time
Today, 21:54
Joined
Sep 15, 2006
Messages
30
i'm writing a function to send an email with the option of attachements
This is the code i'm using, it works fine but i need to be able to sne mulitple attachments. What would be the best way of doing this?

Thanks

code:
Public Function SendEmail(strTo As String, strSubject As String, strBody As String, Optional strCC As String, Optional strAttached As String)
Dim olkapps As Outlook.Application
Dim olknamespaces As Outlook.Namespace
Dim objmailitems As Outlook.MailItem
Set olkapps = New Outlook.Application
Set olknamespaces = GetNamespace("MAPI")
Set objmailitems = olkapps.CreateItem(olMailItem)
With objmailitems
.To = strTo
If IsNull(strCC) = False Then
.CC = strCC
End If
.Subject = strSubject
.Body = strBody
If IsNull(strAttached) = False Then
.Attachments.Add strAttached
End If
.Send
End With
Set objmailitems = Nothing
Set olknamespaces = Nothing
Set olkapps = Nothing
End Function
 
Last edited:
Consider something like this...
Code:
Public Sub SendEmail(strTo As String, strSubject As String, strBody As String, strCC As String, ParamArray strAttached() As Variant)
   Dim olkapps As Outlook.Application
   Dim olknamespaces As Outlook.Namespace
   Dim objmailitems As Outlook.MailItem
   Dim var As Variant

   Set olkapps = New Outlook.Application
   Set olknamespaces = GetNamespace("MAPI")
   Set objmailitems = olkapps.CreateItem(olMailItem)
   
   With objmailitems
      .To = strTo
      .CC = strCC
      .Subject = strSubject
      .Body = strBody
      For Each var In strAttached
         .Attachments.Add var
      Next var
      .Send
   End With
   
   Set objmailitems = Nothing
   Set olknamespaces = Nothing
   Set olkapps = Nothing

End Sub
Note that strCC is no longer optional so calling code must supply "" if there is not data. ParamArray allows you a variable length array of elements. A loop is included to add the attachements.
 

Users who are viewing this thread

Back
Top Bottom