|
-
Jun 11th, 2010, 03:39 PM
#1
Thread Starter
New Member
[RESOLVED] [EXCEL] Attach multiple files in Outlook
Hi,
The title may be confusing but I'm using Excel to attach files to an Outlook email.
Anyway, here's how it goes..
I have several files in a folder and I want to be able to in a click of a button (Email Reports) will send an email attaching all the files in a folder without having to specify the files one by one.
Is there a way to do this because I cannot guarantee that the exact file names will be in the folder. It may change from time to time and I just want to automatically attach whatever files are in the folder.
This is what I have which obviously would not run:
Private Sub CommandButton1_Click()
Dim oApp As Outlook.Application
Set oApp = New Outlook.Application
Dim oMsg As MailItem
Dim oAttachment As Outlook.Attachment
Set oMsg = oApp.CreateItem(olMailItem)
oMsg.SentOnBehalfOfName = "CSS"
oMsg.To = ThisWorkbook.Sheets("Sheet1").Range("C15").Value
oMsg.CC = "CSS"
oMsg.Subject = ThisWorkbook.Sheets("Sheet1").Range("B6").Value
oMsg.Body = ThisWorkbook.Sheets("Sheet1").Range("B10").Value
Set oAttachment = oMsg.Attachments.Add("C:\Documents and Settings\NC\Desktop\test\")
oMsg.Save
Set oMsg = Nothing
Set oApp = Nothing
End Sub Any help will be very helpful!
Thank you!
-
Jun 12th, 2010, 03:18 AM
#2
Re: [EXCEL] Attach multiple files in Outlook
Is this what you are trying? 
Code:
'~~> Add Reference to MS Outlook xx.xx Library
Sub Example()
Dim oOutlook As Outlook.Application, oEmailItem As MailItem
Dim strDir As String, strFile As String, colFiles As New Collection
Dim i As Integer
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application")
On Error GoTo 0
'~~> Sample Directory
strDir = "C:\Documents and Settings\NC\Desktop\test\"
strFile = Dir(strDir)
'~~> Add all files in that directory to a collection
While strFile <> ""
colFiles.Add strFile
strFile = Dir
Wend
Set oEmailItem = oOutlook.CreateItem(olMailItem)
With oEmailItem
'~~> Add files in the collections as attachments
If colFiles.Count > 0 Then
For i = 1 To colFiles.Count
.Attachments.Add strDir & colFiles(i)
Next i
End If
.To = ThisWorkbook.Sheets("Sheet1").Range("C15").Value
.Subject = ThisWorkbook.Sheets("Sheet1").Range("B6").Value
.Body = ThisWorkbook.Sheets("Sheet1").Range("B10").Value
.Display
End With
Set oEmailItem = Nothing
Set oOutlook = Nothing
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Jun 14th, 2010, 10:56 AM
#3
Thread Starter
New Member
Re: [EXCEL] Attach multiple files in Outlook
That's amazingly correct!
Thank you so much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|