saurabhlotankar
New Member
Hello,
I have created a macro for sending maiils through my outlook account with attchments. In that macro i have given path of a folder on my desktop. Ideally i want the macro to attach all the excel files in the folder, but it's picking up only one excel file and attaching it to the mail. Please go through the code below and suggest me wht changes should i do in the below mentioned code so that it will attchment all the files in the folder path i have given. Here is the code:
Sub macro1()
Dim sFolder As String
Dim Folder As Object
Dim file As Object
Dim fso As Object
Dim i As Long
Dim a As String
i = 3
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:UsersabcdDesktopjoy new"
Set Folder = fso.GetFolder(sFolder)
For Each file In Folder.Files
a = file.Name
If LCase(file) Like "*.xls" Then
i = i + 2
Range("A" & i).Value = a
End If
Next file
Columns("A:A").Select
Selection.Replace What:=".*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Dim count As Long
Dim n As Integer
Dim filename As String
Range("A1").Select
Selection.End(xlDown).Select
count = Selection.Row
For n = 2 To count
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
filename = Range("A" & n).Value
With OutMail
.to = "joy.khanna@outlook.com"
.Subject = "Hello"
.Body = "Hello Joy"
.Attachments.Add ("C:UsersabcdDesktopjoy new" & filename & ".xls")
.Send 'or use .Display
End With
Next n
End Sub
I have created a macro for sending maiils through my outlook account with attchments. In that macro i have given path of a folder on my desktop. Ideally i want the macro to attach all the excel files in the folder, but it's picking up only one excel file and attaching it to the mail. Please go through the code below and suggest me wht changes should i do in the below mentioned code so that it will attchment all the files in the folder path i have given. Here is the code:
Sub macro1()
Dim sFolder As String
Dim Folder As Object
Dim file As Object
Dim fso As Object
Dim i As Long
Dim a As String
i = 3
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:UsersabcdDesktopjoy new"
Set Folder = fso.GetFolder(sFolder)
For Each file In Folder.Files
a = file.Name
If LCase(file) Like "*.xls" Then
i = i + 2
Range("A" & i).Value = a
End If
Next file
Columns("A:A").Select
Selection.Replace What:=".*", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("A1").Select
Dim count As Long
Dim n As Integer
Dim filename As String
Range("A1").Select
Selection.End(xlDown).Select
count = Selection.Row
For n = 2 To count
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
filename = Range("A" & n).Value
With OutMail
.to = "joy.khanna@outlook.com"
.Subject = "Hello"
.Body = "Hello Joy"
.Attachments.Add ("C:UsersabcdDesktopjoy new" & filename & ".xls")
.Send 'or use .Display
End With
Next n
End Sub