A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Emailing attachments in VBA



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 6th 08, 10:46 AM posted to microsoft.public.outlook.program_vba
Glenn Suggs
external usenet poster
 
Posts: 4
Default Emailing attachments in VBA

Can someone give me a VBA example of emailing one or more attachments
contained in a string? It would be nice to use the SendObject method since
the application is already set up that way but I'm not sure if that will work
(other than with one object being sent). An alternative example would be
great too.
Thanks in advance,
--
Glenn
Ads
  #2  
Old May 6th 08, 01:12 PM posted to microsoft.public.outlook.program_vba
Robert Martim, Excel
external usenet poster
 
Posts: 4
Default Emailing attachments in VBA

Glenn,

You can do as follows:

Sub Example()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook =
CreateObject("Outlook.Application")

Set oEmailItem = oOutlook.CreateItem(olMailItem)

With oEmailItem
.Attachments.Add "C:\MyFile.doc"
.To = "
.Display
End With

Set oEmailItem = Nothing
Set oOutlook = Nothing

End Sub

--
Best regards
Robert, Excel MVP
Author of RibbonX: Customizing the Office 2007 Ribbon:
Find me at http://www.msofficegurus.com - be part of it!
Join our forum: http://www.msofficegurus.com/forum/

  #3  
Old May 6th 08, 01:57 PM posted to microsoft.public.outlook.program_vba
Glenn Suggs
external usenet poster
 
Posts: 4
Default Emailing attachments in VBA

Robert,

Thanks for the quick response. When adding multiple attachments, does a
semi-colon work between each one? Like this...
..Attachments.Add "C:\Myfile1.doc;C:\MyFile2.doc;C:\MyFile3.doc"

--
Glenn


"Robert Martim, Excel" wrote:

Glenn,

You can do as follows:

Sub Example()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook =
CreateObject("Outlook.Application")

Set oEmailItem = oOutlook.CreateItem(olMailItem)

With oEmailItem
.Attachments.Add "C:\MyFile.doc"
.To = "
.Display
End With

Set oEmailItem = Nothing
Set oOutlook = Nothing

End Sub

--
Best regards
Robert, Excel MVP
Author of RibbonX: Customizing the Office 2007 Ribbon:
Find me at http://www.msofficegurus.com - be part of it!
Join our forum: http://www.msofficegurus.com/forum/

  #4  
Old May 6th 08, 02:28 PM posted to microsoft.public.outlook.program_vba
Robert Martim, Excel
external usenet poster
 
Posts: 4
Default Emailing attachments in VBA

Glenn

A better option might be to loop through the files in a specific folder. The
reason is that you may have files with different extensions and the names may
also change in future, which would require a change in your code.

In this example, you will need to install the references to Windows Script
Host Model (if you do not wish to install it, you will require to create the
scripting object instead):

Sub Example()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem
Dim oFileSystem As New FileSystemObject
Dim oFolder As Object
Dim oFile As File

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook =
CreateObject("Outlook.Application")

On Error GoTo 0
Set oEmailItem = oOutlook.CreateItem(olMailItem)
Set oFolder = oFileSystem.GetFolder("C:\For Deletion")

With oEmailItem
For Each oFile In oFolder.Files
.Attachments.Add oFile.Path
Next oFile
.To = "
.Display
End With

Set oFolder = Nothing
Set oFileSystem = Nothing
Set oEmailItem = Nothing
Set oOutlook = Nothing

End Sub



--
Best regards
Robert, Excel MVP
Author of RibbonX: Customizing the Office 2007 Ribbon:
Find me at http://www.msofficegurus.com - be part of it!
FORUM: http://www.msofficegurus.com/forum/

  #5  
Old May 6th 08, 03:32 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Emailing attachments in VBA

No, that won't work at all. You must call Attachments.Add each time you want to add a file. 3 files, three calls to Attachments.Add

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


"Glenn Suggs" wrote in message ...
Robert,

Thanks for the quick response. When adding multiple attachments, does a
semi-colon work between each one? Like this...
.Attachments.Add "C:\Myfile1.doc;C:\MyFile2.doc;C:\MyFile3.doc"

--
Glenn


"Robert Martim, Excel" wrote:

Glenn,

You can do as follows:

Sub Example()
Dim oOutlook As Outlook.Application
Dim oEmailItem As MailItem

On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook =
CreateObject("Outlook.Application")

Set oEmailItem = oOutlook.CreateItem(olMailItem)

With oEmailItem
.Attachments.Add "C:\MyFile.doc"
.To = "
.Display
End With

Set oEmailItem = Nothing
Set oOutlook = Nothing

End Sub

--
Best regards
Robert, Excel MVP
Author of RibbonX: Customizing the Office 2007 Ribbon:
Find me at http://www.msofficegurus.com - be part of it!
Join our forum: http://www.msofficegurus.com/forum/

  #6  
Old May 6th 08, 04:22 PM posted to microsoft.public.outlook.program_vba
Robert Martim, Excel
external usenet poster
 
Posts: 4
Default Emailing attachments in VBA

Sue

You can try the code and you will see it works... On the other hand, I am
not clear whether the post was addressed to Glenn (which I guess it was).

--
Best regards
Robert, Excel MVP
Author of RibbonX: Customizing the Office 2007 Ribbon:
Find me at http://www.msofficegurus.com - be part of it!
FORUM: http://www.msofficegurus.com/forum/

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Emailing" SavvySurfer[_2_] Outlook Express 2 March 6th 08 05:04 PM
IE seetings for emailing attachments WillB Outlook - Installation 1 October 9th 07 01:11 PM
Emailing Brianna Beverley Outlook Express 2 May 23rd 06 02:34 PM
emailing Carly Luedtke Outlook Express 2 February 23rd 06 06:49 AM
mass emailing Jan O Outlook - Using Contacts 1 January 13th 06 09:33 PM


All times are GMT +1. The time now is 02:55 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.