首页 文章

自动连接到ms excel并使用excel vba检查未读电子邮件

提问于
浏览
1

我正在做一个基于excel-vba的项目 .

有2个用户 . 用户A和用户B.

用户A有一个带有表格的Excel工作簿 . 用户B将定期向用户A的Outlook帐户发送电子邮件 .

我的要求是使用宏为用户A提供2个选项 .

选项1:手动检查是否有来自特定用户的未读电子邮件 .

这是通过:

从excel工作簿中打开outlook,然后用户A可以手动搜索outlook帐户 .

我使用以下代码成功完成了此操作 .

Sub Open_Outlook()

    ' This Macro Opens Microsoft Outlook
    ' Runs an executable program

    Shell ("OUTLOOK")

    End Sub

选项2:自动检查是否有来自特定用户的未读电子邮件 .

这是通过:

  • 创建与Outlook的连接 .

  • 检查是否有任何未读电子邮件 .

Sub ExtractFirstUnreadEmailDetails()

Dim oOutlook As Object
Dim oOlns As Object
Dim oOlInb As Object


'~~> Get Outlook instance
Set oOutlook = GetObject(, "Outlook.Application")
Set oOlns = oOutlook.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox)

'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
    MsgBox "NO Unread Email In Inbox"
    Exit Sub
End If

结束子

I am getting an error when i run the code for the second option.

Run time error 429: Active X component cant create object.

What does this mean?

How do i change the code to get rid of the error and to run it succesfully?

1 回答

  • 0

    olFolderInbox是一个仅Outlook的常量在VBA中将它定义为常量,如下所示:

    Const olFolderInbox = 6
    

    或者简单地用Set oOlInb行中的6替换它

相关问题