PC Review Forums Newsgroups Microsoft Outlook Microsoft Outlook VBA Programming VBA Outlook, attachments question

Reply

VBA Outlook, attachments question

 
Thread Tools Rate Thread
Old 01-07-2003, 11:55 AM   #1
newboy18
Guest
 
Posts: n/a
Default VBA Outlook, attachments question


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
  Reply With Quote
Old 01-07-2003, 02:14 PM   #2
Ken Slovak - [MVP - Outlook]
Guest
 
Posts: n/a
Default Re: VBA Outlook, attachments question

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" <newboy18@msn.com> wrote in message
news:041701c33fc7$b0a41150$a001280a@phx.gbl...
> 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



  Reply With Quote
Old 01-07-2003, 09:01 PM   #3
newboy18
Guest
 
Posts: n/a
Default Re: VBA Outlook, attachments question

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" <newboy18@msn.com> wrote in message
>news:041701c33fc7$b0a41150$a001280a@phx.gbl...
>> 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

>
>
>.
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off