Excel 表示未连接,并在“myInbox”设置为 Outlook 收件箱的行上显示错误。
我的研究引导我找到了这些参考资料和 Outlook 命令。然后网上有一段代码使用“for next”来查找所有新电子邮件,他们必须递增,但他们无法递增对象,因此他们在嵌套的“for next”中使用
i
但随后他们尝试增加外部“for next”中的文件夹。当“for next”被修改时,Unk
也没有缓解。当“olInbox”用于“myInbox”设置为 olFolderInbox 的位置时,仍然出现相同的错误,因此不需要 MAPI。
我正在尝试获取一个等于 Outlook 电子邮件主题的变量。很确定我们不需要嵌套的“下一个”,但它们已包含(注释掉),因此不会丢失任何工作。
返回的错误是“您未连接”和“运行时”
我期望:
收件箱中的未读电子邮件变为已读。
所有附件都保存在一个文件夹中。
通过主题行确定的重要电子邮件应该得到回复。
此如何使用 Excel VBA 从 Outlook 检索电子邮件? 使用对象来定义其“myInbox”变量,然后将其设置为等于 Outlook 文件夹。我试图模仿它,但同样的错误并且没有看到差异
Dim olInbox As Outlook.MAPIFolder
Dim myInbox As Outlook.Folder 'does not change error if we switch this to object
Dim unRead, m As Object
Dim att As Object
Dim emailSubject As String
Dim newEmailItem As Outlook.MailItem
Dim x As Date
Dim ws As Worksheet
Dim i As Long
Dim row As Long
Dim unk As Integer
x = Date
'~~> Get Outlook instance
Set EmailApp = New Outlook.Application
Set myNameSpace = Outlook.GetNamespace("MAPI")
'For unk = 1 To 2 Step 1
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folder 'HERE IS WHERE MY ERROR IS
'using "olInbox" in place of "myInbox" does not solve it either so its not expecting MAPI
'Set olInbox = myNameSpace.GetDefaultFolder(olFolderInbox).Folders(myInbox.Name)
'For i = olInbox.Items.Count To 1 Step -1
'If TypeOf olInbox.Items(i) Is MailItem Then
Set newEmailItem = olInbox.Items(i)
' If newEmailItem(newEmailItem.Subject, "transactions") > 0 _
' And newEmailItem(newEmailItem.ReceivedTime, x) > 0 Then
' With ws
' row = .Range("A" & .Rows.Count).End(xlUp).row
' .Range("A" & row).Offset(1, 0).Value = newEmailItem.Subject
' .Range("A" & row).Offset(1, 1).Value = newEmailItem.ReceivedTime
' .Range("A" & row).Offset(1, 2).Value = newEmailItem.SenderName
' End With
' End If
'End If
'Next i
'Set olInbox = Nothing
'Next unk or myInbox?
'set unread equal to the count of unread e mails in the inbox
Set unRead = myInbox.Items.Restrict("[UnRead] = True")
File_Path = "D:\Documents\Email attachments\" 'where we save the attachments
If unRead.Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Else
For Each m In unRead
emailSubject = newEmailItem.Subject
Select Case emailSubject
Case emailSubject Like "MOI"
Set newEmailItem = EmailApp.CreateItem(olMailItem) 'creates a new e mail to be sent
newEmailItem.To = "[email protected]" 'who your sending it to, we will need to make dynamic
newEmailItem.Subject = "MOI" 'enters MOI into new e mail subject line"
'below is the body of the e mail
newEmailItem.HTMLBody = "Hi," & vbNewLine & "Branagan Ins here just wanted to let you know there is a new MOI" & vbNewLine & "have a great week" & vbNewLine & "Branagan Ins Services" & vbNewLine & "707-255-2500" & vbNewLine & "Marilyn Branagan" & vbNewLine & "1631 Lincoln ave, Napa CA"
If m.Attachments.Count > 0 Then
For Each att In m.Attachments
att.SaveAsFile File_Path & "att.Filename" 'might need to make dynamic
m.unRead = False 'mark email as read
DoEvents
m.Save
EmailApp.Attachments.Add File_Path & "att.Filename" 'attach att to new e mail out
Next att
End If
newEmailItem.Send
Case emailSubject Like "Renewal"
Set newEmailItem = EmailApp.CreateItem(olMailItem) 'creates a new e mail to be sent
newEmailItem.To = "[email protected]" 'who your sending it to, we will need to make dynamic
newEmailItem.Subject = "Renewal" 'enters MOI into new e mail subject line"
'below is the body of the e mail
newEmailItem.HTMLBody = "Hi," & vbNewLine & "Branagan Ins here just wanted to let you know your policy is renewing" & vbNewLine & "have a great week" & vbNewLine & "Branagan Ins Services" & vbNewLine & "707-255-2500" & vbNewLine & "Marilyn Branagan" & vbNewLine & "1631 Lincoln ave, Napa CA"
If m.Attachments.Count > 0 Then
For Each att In m.Attachments
MsgBox "you saved your attachements"
att.SaveAsFile File_Path & "att.Filename" 'might need to make dynamic
m.unRead = False
DoEvents
m.Save
EmailApp.Attachments.Add File_Path & "att.Filename" 'attach att to new e mail out
Next att
End If
newEmailItem.Send
Case Else
m.unRead = False 'marks all messages as read
End Select
Next m
End If
End Sub
添加对
Namespace.Logon
的调用并删除 .Folder
部分。
Set EmailApp = New Outlook.Application
Set myNameSpace = Outlook.GetNamespace("MAPI")
myNameSpace.Logon
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)