我已经收到
(1004)应用程序定义或对象定义错误
,同时尝试将Outlook中的电子邮件发件人和主题导出到Excel。
Sub GetFolderStats()
10 On Error GoTo Err
11 Dim objOutlook As Object
12 Dim objnSpace As Object
13 Dim objRootFolder As Object
14 Dim objInbox As Object
15 Dim mailSel As Outlook.Selection
16 Dim senderVal As String, titleVal As String
17 Dim path As String
18 Dim objExcel As Object
19 Dim objWorkbook As Object
20 Dim objSheet As Object
22 path = "example.xlsx"
23 Set objExcel = CreateObject("Excel.Application")
24 Set objWorkbook = objExcel.Workbooks.Open(path)
25 Set objSheet = objWorkbook.Sheets("Sheet1")
26 Set objOutlook = CreateObject("Outlook.Application")
27 Set objnSpace = objOutlook.GetNamespace("MAPI")
28 Set objRootFolder = objnSpace.Folders("RootFolder")
29 Set objInbox = objRootFolder.Folders("Inbox")
30 For Each Folder In objInbox.Folders
31 For Each i In Folder.Items
32 If (i.UnRead) Then
33 senderVal = i.SenderEmailAddress
34 titleVal = i.Subject
35 MsgBox senderVal + " " + titleVal
37 NextEmptyRow = objSheet.Range("A" & objExcel.Rows.Count).End(xlUp).Row + 1
38 objSheet.Range("A" & NextEmptyRow) = senderVal
39 objSheet.Range("B" & NextEmptyRow) = titleVal
51 objWorkbook.Save
41 End If
42 Next
43 Next
Err:
140 MsgBox "Value: " & dblRnd & vbCrLf & _
"Error Line: " & Erl & vbCrLf & _
"Error: (" & Err.Number & ") " & Err.Description, vbCritical
objWorkbook.Close
Stop
End Sub
第37行给了我错误。我尝试过sheet.activate
,sheet.select
都没有用。另外,我尝试了几种在网上找到的解决方案,但都无济于事。