Here is some code I cut from various projects that I thought would help you accomplish your goal. I didn't verify every variable name, so you have to be careful. Naturally, you will have to modify it to suit your needs. I included all the public variables names and modified others, but your a smart fellow and I'm sure you can step through this code very easily. Also check available references libraries. I have OLE Automation, Microsoft ActiveX Data Objects 2.5 Library, Visual basic for application, and microsoft Excel 9.0 object library and Microsoft Office 9.0 Objects Library checked off. Good luck.
Public MyFullName As String
Public MyFileName As String
Public LenOfMyFullName As Integer
Public LenOfMyFileName As Integer
Public My_Path As String
Public My_OpenFileName As String
Public TempArray() As String
Public MyCounter As Integer
Public strMyDateTime As String
Public SpecList_b As Boolean
Sub GetListOfFiles()
' Gets a list of text files and holds the names in a
' temporary array while we use them in different sub routines
Application.ScreenUpdating = False
MyFileName = ThisWorkbook.Name
MyFullName = ThisWorkbook.FullName
LenOfMyFullName = Len(MyFullName)
LenOfMyFileName = Len(MyFileName)
My_Path = Left(MyFullName, LenOfMyFullName - LenOfMyFileName) MyCounter = 0
MyStem = False
file = Dir(My_Path)
Do While file <> ""
If file <> "." Then
If file <> ".." Then
If Right(file, 3) = "txt" Then
MyCounter = MyCounter + 1
ReDim Preserve TempArray(1 To MyCounter)
TempArray(MyCounter) = file
If MyStem = False Then
MyStartValue = Mid(file, 1, 1)
Do While MyStartValue <> ""
If MyStartValue = "_" Then
' example of file I expect
' filename_20040309-1606.txt
MyDate = Mid(file, start1 + 5, 2) & "/" & Mid(file, start1 + 7, 2) & "/" & Mid(file, start1 + 1, 4)
MyTime = Mid(file, start1 + 10, 2) & ":" & Mid(file, start1 + 12, 2)
strMyDateTime = MyDate & " - " & MyTime
Exit Do
Else
start1 = start1 + 1
MyStartValue = Mid(file, start1, 1)
End If
Loop
MyStem = True
End If
End If
End If
End If
file = Dir()
Loop
End Sub
Sub MoveFilesFromDirectoryToSubDir()
For i = 1 To MyCounter
MyOldFileName = My_Path & TempArray(i)
MyNewFileName = My_Path & "\newdir\" & TempArray(i)
FileCopy MyOldFileName, MyNewFileName
Kill MyOldFileName
Next i
End Sub
Sub Sends_Successful_Email()
Dim fDerEmail1 As String
Dim fDerEmail2 As String
Dim fDer As Double
Dim fDerDesc As String
Dim NbrOfDers As Integer
Dim MyBody As String
Dim MySubject As String
Worksheets("Sheet1").Activate
Range("a1").Select
Set Table1 = ActiveCell.CurrentRegion
Set Email_tbl = Table1.Offset(1, 0).Resize(Table1.Rows.Count - 1, Table1.Columns.Count)
NbrOfDers = Email_tbl.Rows.Count
MySubject = "Changes have been successfully changed"
fDerEmail1 = "EmailAddress1@someplace.com"
fDerEmail2 = "EmailAddress2@someplace.com"
For x = 1 To NbrOfDers
fDer = Email_tbl.Cells(x, 1).Value
fDerDesc = Email_tbl.Cells(x, 2).Value
fOldSISG = Email_tbl.Cells(x, 3).Value
fNewSISG = Email_tbl.Cells(x, 4).Value
MyBody = fDer & " - " & fDerDesc & " has changed from " & fOldSISG & " to " & fNewSISG & vbCrLf & MyBody
Next x
Set MyOlApp = CreateObject("Outlook.Application")
Set MyItem = MyOlApp.CreateItem(olMailItem)
Set MyAttachments = MyItem.Attachments
Set MyRecipients = MyItem.Recipients
MyItem.Recipients.Add fDerEmail1
MyItem.Recipients.Add fDerEmail2
With MyItem
.Subject = MySubject
.Body = MyBody
End With
'MyItem.display
MyItem.send
Set MyOlApp = Nothing
Set MyItem = Nothing
Set MyAttachments = Nothing
Set MyRecipients = Nothing
End Sub