Can someone give me a hand with this. I'm trying to attach a new text file that is generated everyday and send it via an email. I keep getting a Run-time error '9': Subscript out of range when I try to debug it.
Sub Attachtxt()
Dim folder As String, file As String
folder = "X:\test"
With CreateObject("Outlook.Application").CreateItem(0)
.To = "any.mouse@gmail.com"
.Subject = "Test"
file = Split(CreateObject("wscript.shell").exec("cmd /c Dir /b /o-d """ & folder & """*.txt").stdout.readall, vbCrLf)(0)
.Attachments.Add folder & file
.Send
End With
End Sub
Sub Attachtxt()
Dim folder As String, file As String
folder = "X:\test"
With CreateObject("Outlook.Application").CreateItem(0)
.To = "any.mouse@gmail.com"
.Subject = "Test"
file = Split(CreateObject("wscript.shell").exec("cmd /c Dir /b /o-d """ & folder & """*.txt").stdout.readall, vbCrLf)(0)
.Attachments.Add folder & file
.Send
End With
End Sub