Lartk
03-14-2013, 09:50 AM
Is there a way to change the below code to attach the most recent file in a folder instead of referencing a specific file name?
Sub EmailTest()
Const FILE_ATTACH As String = _
"G:\KL\Test.xls"
Dim oOutlook As Object
Dim oMailItem As Object
Dim oRecipient As Object
Dim oNameSpace As Object
Dim Lastrow As Long
Dim bodyText As String
Dim i As Long
Set oOutlook = CreateObject("Outlook.Application")
Set oNameSpace = oOutlook.GetNameSpace("MAPI")
oNameSpace.Logon , , True
Set oMailItem = oOutlook.CreateItem(0)
With oMailItem
Set oRecipient = .Recipients.Add("test@aol.com")
oRecipient.Type = 1
.Subject = "Test"
If Dir(FILE_ATTACH, vbNormal) <> "" Then
.Attachments.Add ("G:\KL\Test.xls")
.Save
End If
End With
End Sub
Sub EmailTest()
Const FILE_ATTACH As String = _
"G:\KL\Test.xls"
Dim oOutlook As Object
Dim oMailItem As Object
Dim oRecipient As Object
Dim oNameSpace As Object
Dim Lastrow As Long
Dim bodyText As String
Dim i As Long
Set oOutlook = CreateObject("Outlook.Application")
Set oNameSpace = oOutlook.GetNameSpace("MAPI")
oNameSpace.Logon , , True
Set oMailItem = oOutlook.CreateItem(0)
With oMailItem
Set oRecipient = .Recipients.Add("test@aol.com")
oRecipient.Type = 1
.Subject = "Test"
If Dir(FILE_ATTACH, vbNormal) <> "" Then
.Attachments.Add ("G:\KL\Test.xls")
.Save
End If
End With
End Sub