每天我在Outlook中收到約100封電子郵件,其中包含類似的主題「經銷商銷售駕駛艙 - XXXXXXXX」,其中XXXXXXXX是一個可變數字。指定要保存Excel文件的文件夾
我需要獲得電子郵件主題末尾的數字,然後將它們寫入Excel電子表格的一個列(如果在Excel中不可能,則將其寫入一個.txt文件中)。
這是我找到的代碼。問題是,它從不寫在「test.xlsx」中。我無法解決它從來沒有真正與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
當我運行它,一個彈出出現,我覺得它要求對電子表格中的名字,但我選擇的任何名稱似乎已經存在,所以沒有被創建。它仍會掃描我的收件箱,因爲它會統計我的電子郵件,並說「共導出了X條消息」。 X因我收件箱中的電子郵件數量而異。
彈出窗口出現在哪一行,它實際上要求你說什麼? –
除非你爲'InputBox'提供一個*完整路徑*,否則你只是給它一個文件名,而'SaveAs'將它放在* current *目錄中。所以它絕對*是*保存文件,它不會將它保存到您認爲是的'D:\ ...'路徑中。 –
彈出窗口 'strFil = InputBox(「D:\ LocalData \ xl02926 \ Documents \ Samuel \ AutoCockpit \ test.xlsx」,「將郵件導出到Excel」) – SamRosignoli