简体   繁体   English

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

[英]Automatically connect to ms excel and check for unread emails using excel vba

I am doing a project based on excel-vba. 我正在做一个基于excel-vba的项目。

There are 2 users. 有2位用户。 User A and User B 用户A和用户B

User A has an excel workbook with a table. 用户A具有带有表的excel工作簿。 User B will be emailing a document regularly,to user A's outlook account. 用户B将定期通过电子邮件将文档发送到用户A的Outlook帐户。

My requirement is by using a macro to give 2 options to user A. 我的要求是使用宏为用户A提供2个选项。

option 1: manually check if there is unread email from a particular user. 选项1:手动检查是否有来自特定用户的未读电子邮件。

This is done by: 这是通过以下方式完成的:

Opening outlook from within the excel workbook and then User A can manualy search through the outlook account. 从excel工作簿中打开Outlook,然后用户A可以手动搜索Outlook帐户。

I have successfully done this using the following code. 我已经使用以下代码成功完成了此操作。

    Sub Open_Outlook()

    ' This Macro Opens Microsoft Outlook
    ' Runs an executable program

    Shell ("OUTLOOK")

    End Sub

option 2: Automatically check if there is unread email from a particular user. 选项2:自动检查是否有来自特定用户的未读电子邮件。

This is done by: 这是通过以下方式完成的:

  1. Create a Connection to Outlook. 创建到Outlook的连接。

  2. Checking if there is any unread email. 检查是否有未读的电子邮件。

Sub ExtractFirstUnreadEmailDetails() 子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

End Sub 结束子

I am getting an error when i run the code for the second option. 运行第二个选项的代码时出现错误。

Run time error 429: Active X component cant create object. 运行时错误429:Active X组件无法创建对象。

What does this mean? 这是什么意思?

How do i change the code to get rid of the error and to run it succesfully? 如何更改代码以消除错误并成功运行?

olFolderInbox is a Outlook only constant Either define it as a constant in VBA like this: olFolderInbox是Outlook唯一的常量可以将其定义为VBA中的常量,如下所示:

Const olFolderInbox = 6

Or simply replace it with 6 in the Set oOlInb line 或者直接在Set oOlInb行中将其替换为6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM