从Access打开O​​utlook以扫描收件箱

时间:2016-11-08 01:16:15

标签: vba email outlook access-vba

我在Outlook中使用VBA查找电子邮件。它并不是一直都在工作。

我想知道我是否正式申报变量,或者没有正确打开Outlook。

此外,有没有办法打开Outlook,刷新收件箱文件夹,然后开始扫描?

错误是两件事之一,您尝试连接的远程服务器不可用"或"未声明的对象"好像Outlook应用程序从未打开过。

Dim ns                  As Outlook.NameSpace
Dim objOL               As Outlook.Application
Dim folder              As MAPIFolder
Dim item                As Object
Dim msg                 As MailItem
Dim objAttachments      As Outlook.Attachments

Set objOL = CreateObject("Outlook.Application")

'The line below will error out occasionally
'  with a remote serve not available...
Set ns = Session.Application.GetNamespace("MAPI")

Set folder = ns.GetDefaultFolder(olFolderInbox)

For Each item In folder.Items
    DoEvents
    If (item.Class = olMail) And (item.UnRead) Then
        ' This message has not been read.  Display it modally
        Set msg = item
        'Debug.Print (msg.Subject)
        'Debug.Print (msg.SenderEmailAddress)
        tempString = CStr(msg.Subject)
        If Mid(tempString, 1, 4) = "qqqq" Then
            'Debug.Print (tempString)
            'create new directory
            If updateFlag = 0 Then
                'this checks if the directory already exists,
                ' if it does just skip creating the directory since we do not
                ' want to override previous saved attached inventory files.
                If Dir(strFolderpath, vbDirectory) = "" Then
                    Createdir True, strFolderpath
                End If
            End If
            updateFlag = 1

            'Debug.Print msg.Subject
            'instantiate attachments object for each openned email
            Set objAttachments = msg.Attachments
            lngCount = objAttachments.Count
            'if you want to open all the emails uncomment next line
            'msg.Display True
            For i = lngCount To 1 Step -1
                ' Save attachment before deleting from item.
                ' Get the file name.
                strFile = objAttachments.item(i).FileName
                ' Combine with the path to the Temp folder.
                strFile = strFolderpath & "\" & strFile
                WriteLogFile "File saved from blabla@blbl inbox, file path: " & strFile
                ' Save the attachment as a file.
                objAttachments.item(i).SaveAsFile strFile
            Next i

            msg.UnRead = False
        End If
    End If
Next
'close the outlook application.
'objOL.Quit

我尝试打开Outlook,然后关闭它并重新打开它,但这并不起作用。有没有更合适的方式来打开Outlook?以及如何在打开后刷新收件箱,等待刷新完成,然后扫描收件箱?

0 个答案:

没有答案