Every day I receive about 100 emails in Outlook with similar subjects "Dealer Sales Cockpit - XXXXXXXX", being XXXXXXXX a variable number.
I need to get the numbers at the end of the email subject and write them in a column in an Excel spreadsheet (or in a .txt file, if it's not possible in Excel).
This is code I've found. Problem is, it never writes in "test.xlsx". I can't solve it having never really worked with VBA.
Sub ExportMessagesToExcel()
Dim olkMsg As Object, _
excApp As Object, _
excWkb As Object, _
excWks As Object, _
lngRow As Long, _
lngCnt As Long, _
intPos As Integer, _
strFil As String
strFil = InputBox("D:\LocalData\xl02926\Documents\Samuel\AutoCockpit\test.xlsx", "Export Messages to Excel")
If strFil <> "" Then
Set excApp = CreateObject("Excel.Application")
Set excWkb = excApp.Workbooks.Add()
Set excWks = excWkb.ActiveSheet
'Write Excel Column Headers
With excWks
.Cells(1, 1) = "Computer"
End With
lngRow = 2
'Write messages to spreadsheet
For Each olkMsg In Application.ActiveExplorer.CurrentFolder.Items
'Only export messages, not receipts or appointment requests, etc.
If olkMsg.Class = olMail Then
'Add a row for each field in the message you want to export
intPos = InStrRev(olkMsg.Subject, " ")
excWks.Cells(lngRow, 1) = Mid(olkMsg.Subject, intPos + 1)
lngRow = lngRow + 1
lngCnt = lngCnt + 1
End If
Next
Set olkMsg = Nothing
excWkb.SaveAs strFil
excWkb.Close
End If
Set excWks = Nothing
Set excWkb = Nothing
Set excApp = Nothing
MsgBox "Process complete. A total of " & lngCnt & " messages were
exported.", vbInformation + vbOKOnly, "Export messages to Excel"
End Sub
When I run it, a pop-up appears and I think it asks for a name for the spreadsheet, but any name I choose seems to already exist, so nothing is created. It still scans my Inbox, as it counts my emails and says "A total of X messages were exported". X varies with the number of emails in my Inbox.
InputBox
, you're just giving it a filename and theSaveAs
will put it in the current directory. So it absolutely is saving the file, it's just not saving it to theD:\...
path that you think it is. – David ZemensstrFil = InputBox("D:\LocalData\xl02926\Documents\Samuel\AutoCockpit\test.xlsx", "Export Messages to Excel")
– SamRosignoliInputBox
because it already came with the code, I didn't write anything, I've never used VBA before. So, if you say it's definitely saving somewhere, how can I save in the right folder? – SamRosignoli