我已经正确定义和分配了所有变量,但仍然通过下面的附件行在循环中获取所需的对象(错误 424):
Sub apscan()
Dim objItems As Outlook.Items
Dim objItem As Object
Dim objMail As Outlook.MailItem
Dim strMonth As String
Dim dReceivedTime As Date
Dim strReceivedDate As String
Dim i, n As Long
Dim strMsg As String
Dim nPrompt As Integer
Dim ns As Namespace
Dim folder As MAPIFolder
Dim Loc As Range
Dim atmt As Outlook.Attachment
Dim atmts As Outlook.Attachments
ThisWorkbook.Activate
Set olApp = CreateObject "Outlook.Application")
Set ns = olApp. GetNamespace ("МАРІ")
Set folder = ns.Folders("AP").Folders("Inbox")
Set objItems = folder.Items
objItems.SetColumns("ReceivedTime")
objItems.Sort "[ReceivedTime]", True
For Each objItem In objItems
For Each atmt In objItem.Attachments
Debug.Print atmt.Filename
Next atmt
Next objItem
End Sub
Sub apscan()
Dim olApp As Object
Dim ns As Object
Dim folder As Object
Dim objItems As Object
Dim objItem As Object
Dim atmt As Object
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set ns = olApp.GetNamespace("MAPI")
Set folder = ns.GetDefaultFolder(6) ' 6 = olFolderInbox
Set objItems = folder.Items
objItems.Sort "[ReceivedTime]", True
For Each objItem In objItems
If TypeName(objItem) = "MailItem" Then
For Each atmt In objItem.Attachments
Debug.Print atmt.Filename
Next atmt
End If
Next objItem
Set atmt = Nothing
Set objItem = Nothing
Set objItems = Nothing
Set folder = Nothing
Set ns = Nothing
Set olApp = Nothing
End Sub