vba 如何创建一个 vbs 来检查未读电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21258176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:29:07  来源:igfitidea点击:

How to create a vbs to check for unread emails

vbavbscriptoutlook

提问by user3219064

I am have been looking for ways on how to create a visual basic script for outlook and most of the suggestions have been from this website. Unfortunately none of them meet my exact need.

我一直在寻找有关如何为 Outlook 创建可视化基本脚本的方法,并且大部分建议都来自该网站。不幸的是,它们都不能满足我的确切需求。

Reason for the script:

脚本的原因:

There is a shared mailbox which a few of my colleagues have access to and are meant to check however at times this can get ignored. I would like to create a script which checks for unread emails in this shared mailbox and emails specific contacts if the folder contains more than 10 emails.

有一个共享邮箱,我的一些同事可以访问并打算检查,但有时这可能会被忽略。我想创建一个脚本来检查此共享邮箱中的未读电子邮件,如果文件夹包含超过 10 封电子邮件,则向特定联系人发送电子邮件。

What I have tried so far:

到目前为止我尝试过的:

Option Explicit

 Private Sub Main()
  Dim olMAPI As Outlook.NameSpace
  Dim Folder As Outlook.MAPIFolder
  Const FOLDER_TO_OPEN = "Mailbox - John Doe"   'Modify as appropriate

  Set olMAPI = GetObject("", "Outlook.Application") _
                          .GetNamespa   ce("MAPI")
  Call PrintFolderNames(olMAPI.Folders(FOLDER_TO_OPEN), "->")
  Set olMAPI = Nothing
End Sub

 Sub PrintFolderNames(tempfolder As Outlook.MAPIFolder, a$)
  Dim i As Integer
  If tempfolder.Folders.Count Then
     Debug.Print a$ & " " & tempfolder.Name & "  ";
     Debug.Print tempfolder.UnReadItemCount
     For i = 1 To tempfolder.Folders.Count
       Call PrintFolderNames(tempfolder.Folders(i), a$ & "->")
     Next i
  Else
     Debug.Print a$ & " " & tempfolder.Name & "  ";
     Debug.Print tempfolder.UnReadItemCount
  End If
 End Sub

I have tried to create a script first which checks for unread emails in a specific folder but this usually fails with syntax/run time errors. I have tried:

我曾尝试首先创建一个脚本来检查特定文件夹中的未读电子邮件,但这通常会因语法/运行时错误而失败。我试过了:

But this came back with a compile error then a syntax error.

但这又带来了编译错误,然后是语法错误。

I have also tried:

我也试过:

Const olFolderInbox = 6

Set objOutlook = CreateObject("Outlook.Application")

Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon "Default Outlook Profile", , False, True    

Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

Set colItems = objFolder.Items
Wscript.Echo colItems.Count

objOutlook.Quit

回答by CommonGuy

You must have outlook installed to use this code:

您必须安装 Outlook 才能使用此代码:

Const olFolderInbox = 6
Const olMailItem = 0
dim objOutlook

call checkForUnreadMails


sub checkForUnreadMails()
    dim objFolder, objNamespace

    'get running outlook application or open outlook
    Set objOutlook = GetObject(, "Outlook.Application")
        If objOutlook Is Nothing Then
            Set objOutlook = CreateObject("Outlook.Application")
        End If

    Set objNamespace = objOutlook.GetNamespace("MAPI")  

    'get inbox folder
    Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)

    'send mail if more than 10 mails are unread
    if objFolder.UnReadItemCount > 10 then
        sendMail "[email protected]"
    end if

    'send mail if more folder contains more than 10 mails
    'if objFolder.Items.Count > 10 then
    '   sendMail "[email protected]"
    'end if
end sub

sub sendMail(address)
    dim oItem
    Set oItem = objOutlook.CreateItem(olMailItem)

    With oItem
         .To = address
         .Subject = "Unread mails!"
         .Body = "You have too many unread mails!"
         .send
    End With
end sub