Outlook email with code - adding attachments

S

SAC

I'm using code to email from a form. I'm not sure how to add more than one
attachment to a single email. Do I loop through the attachments add part?

Here's the code:

Sub SendMessageNew(strTo As String, strCC As String, strBCC As String, _
strSubject As String, strBody As String, DisplayMsg As Boolean,
Optional AttachMentPath)
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim strRecipTo As String

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo

' Add the CC recipient(s) to the message.
If strCC > "" Then Set objOutlookRecip =
..Recipients.Add(strCC)

objOutlookRecip.Type = olCC

' Add the BCC recipient(s) to the message.
If strBCC > "" Then Set objOutlookRecip =
..Recipients.Add(strBCC)
objOutlookRecip.Type = olBCC

' Set the Subject, Body, and Importance of the message.
.Subject = strSubject
.Body = strBody & vbCrLf & vbCrLf
.Importance = olImportanceHigh 'High importance

' Add attachments to the message.
If Not IsMissing(AttachMentPath) Then
If AttachMentPath > "" Then Set objOutlookAttach =
..Attachments.Add(AttachMentPath)
End If

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
End Sub

Thanks.
 
J

Jim/Chris

Your code looks good except you are allowing for only 1
attachment. Below is some code that I use allowing for up
to 5 attachemnts.

Function SendEMail()
Dim strTo As String, strSubject As String, _
varBody As Variant, strCC As String, _
strBCC As String, strAttachment As String, _
strAttachment1 As String, strAttachment2 As String, _
strAttachment3 As String, strAttachment4 As String

strTo = "email address"
strSubject = "put subject here"
varBody = "put message for body here"
' Add more strattachments if needed and modify IF statement
' below
strAttachment = "attachment1"
strAttachment1 = "attachment2"
strAttachment2 = "attachment3"
strAttachment3 = "attachment4"
strAttachment4 = "attachment5"
'Start Outlook
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

'Logon
Dim olNs As Outlook.NameSpace
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon

'Send a message
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
'Fill Out and Send Message
olMail.To = strTo
olMail.CC = strCC
olMail.BCC = strBCC
olMail.Subject = strSubject
olMail.Body = varBody
' Modify these statements if more attachments are needed
If Len(strAttachment) <> 0 Then
olMail.Attachments.Add (strAttachment)
If Len(strAttachment1) <> 0 Then
olMail.Attachments.Add (strAttachment1)
If Len(strAttachment2) <> 0 Then
olMail.Attachments.Add (strAttachment2)
If Len(strAttachment3) <> 0 Then
olMail.Attachments.Add (strAttachment3)
If Len(strAttachment4) <> 0 Then
olMail.Attachments.Add (strAttachment4)
End If
End if
End if
End if
End If
olMail.Send

Set olNs = Nothing
Set olMail = Nothing
Set olApp = Nothing

End Function

Good Luck

Jim

*****************************************************
 
S

SAC

Can I make a loop in case I don't know how many attachments there will be.

In one case I want all the files (8 of thiem in this case) to be added as
attachments.

Is there a reasonable number of attachments that could/should be send with
an email?

These would be engineering drawings in pdf format.

Thanks.
 
T

Tony Toews

SAC said:
Can I make a loop in case I don't know how many attachments there will be.

Yes, you will have to.
Is there a reasonable number of attachments that could/should be send with
an email?

That's an Outlook question which I'd suggest visiting the Outlook
newsgroup(s) to ask.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
 
C

Cheryl Fischer

Most email providers (msn, yahoo, etc.) have a size limitation for
attachments. For example, MSN's is 3072K in total attachments. You might
want to check out that aspect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top