VBA Outlook, attachments question

N

newboy18

Please help, I have 2 questions.
1/. I want my routine to send all .doc files in a folder
to a contact but how, the Attachments.Add will not allow
me to put "c:\docsemail\*.doc
2/. I want to start Outlook before running this sub
routine because it is part of a loop, it will be very slow
if I open Outlook for every email. I tried to move the
fist 2 lines to the main routine but the sub routine would
then fail at the "Set olMail = olApp.CreateItem
(olMailItem" line.

Sub EmailDoc()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olMail As MailItem
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = "Smith, John"
.Subject = "New documents"
.Attachments.Add "c:\docsemail\test.txt"
.Attachments.Add "c:\docsemail\test2.txt"
.Send
End With
Set olMail = Nothing
olApp.Quit
Set olApp = Nothing
End Sub
 
K

Ken Slovak - [MVP - Outlook]

You have to loop and add only 1 attachment at a time to the
Attachments collection.

Declare your Outlook object at the module level so it is global to
your code and then instantiate it before you call your processing
procedure.

Dim olApp As Outlook.Application

Sub Something()
Set olApp = CreateObject("Outlook.Application")
Call EmailDoc
Set olApp = Nothing
End Sub

Sub EmailDoc()
'whatever
End Sub


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Lead Author, Professional Outlook 2000 Programming, Wrox Press
Lead Author, Beginning VB 6 Application Development, Wrox Press
Attachment Options
http://www.slovaktech.com/attachmentoptions.htm
Extended Reminders
http://www.slovaktech.com/extendedreminders.htm
 
N

newboy18

Thanks Ken,
It works fine now, I did a Do until loop for just the
Attachment and followed your advise for the rest
-----Original Message-----
You have to loop and add only 1 attachment at a time to the
Attachments collection.

Declare your Outlook object at the module level so it is global to
your code and then instantiate it before you call your processing
procedure.

Dim olApp As Outlook.Application

Sub Something()
Set olApp = CreateObject("Outlook.Application")
Call EmailDoc
Set olApp = Nothing
End Sub

Sub EmailDoc()
'whatever
End Sub


--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Lead Author, Professional Outlook 2000 Programming, Wrox Press
Lead Author, Beginning VB 6 Application Development, Wrox Press
Attachment Options
http://www.slovaktech.com/attachmentoptions.htm
Extended Reminders
http://www.slovaktech.com/extendedreminders.htm


newboy18 said:
Please help, I have 2 questions.
1/. I want my routine to send all .doc files in a folder
to a contact but how, the Attachments.Add will not allow
me to put "c:\docsemail\*.doc
2/. I want to start Outlook before running this sub
routine because it is part of a loop, it will be very slow
if I open Outlook for every email. I tried to move the
fist 2 lines to the main routine but the sub routine would
then fail at the "Set olMail = olApp.CreateItem
(olMailItem" line.

Sub EmailDoc()
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olMail As MailItem
Set olMail = olApp.CreateItem(olMailItem)
With olMail
.To = "Smith, John"
.Subject = "New documents"
.Attachments.Add "c:\docsemail\test.txt"
.Attachments.Add "c:\docsemail\test2.txt"
.Send
End With
Set olMail = Nothing
olApp.Quit
Set olApp = Nothing
End Sub


.
 
Joined
Nov 27, 2011
Messages
1
Reaction score
0
I have a similar question. I am trying to add from 1 to 20 attachments. The user will pick the attachments to send.

My questions is, I have AttachmentAdd as a variable that contains all the files necessary to be sent. Obviously it crashes here. What do I need to do to get the variable files attached to 1 e-mail and sent?

With Mail_Single
.Subject = "Location Report for " & Current_Date
.To = E_Mail_Name
.CC = Email_Cc
.Bcc = Email_Bcc
.Body = Email_Body
.Attachments.Add = AttachmentAdd
.ReadReceiptRequested = True
.Send
End With

Thanks.

Dean
 

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