运行时错误91 Outlook保存附件 [英] Runtime error 91 Outlook Save Attchments

查看:90
本文介绍了运行时错误91 Outlook保存附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行时错误91-对象变量或未设置块变量

我遇到错误91
我试图在附件到达时保存它们,然后将其移动到子文件夹然后进行打印.

I'm getting Error 91
I am trying to save attachment as they arrive and then move it to sub-folder then print.

我正在ThisOutlookSession上使用代码

I am using the code on ThisOutlookSession

Private Sub SaveMovePrint(olMail As Outlook.MailItem)
    'On Error Resume Next
    Dim colAtts As Outlook.Attachments
    Dim olAtt As Outlook.Attachment
    Dim olFile As String
    Dim olDirectory As String
    Dim olFileType As String
    Dim olNameSpace As Outlook.NameSpace
    Dim olInbox As Outlook.Folder
    Dim olDestFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim olItem As Object

此行是错误来自Set colAtts = olAtt.Attachments

    Set colAtts = olAtt.Attachments
    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)
    Set olItems = olInbox.Items

    '// Save attachment then move
    If colAtts.Count Then

        '// Select Case save attch move
        Select Case olMail.SenderEmailAddress
            '// One
            Case "FaxOne@one.com"
                '// Save it to
                olDirectory = "C:\Users\Documents\FaxOne\"
                '// Move email to subfolder
                Set olDestFolder = olInbox.Folders("FaxOne")
                Set olItem = olItems.Find("[SenderName] = FaxOne@one.com'")
                While TypeName(olItem) <> "Nothing"
                    olItem.Move olDestFolder
                Set olItem = olItems.FindNext
                Wend

            '// Two
            Case "FaxTwo@two.com"
                '// Save attachments to
                olDirectory = "C:\Users\Documents\FaxTwo\" 
                Set olDestFolder = olInbox.Folders("FaxTwo")
                Set olItem = olItems.Find("[SenderName] = 'FaxTwo@two.com'")
                While TypeName(olItem) <> "Nothing"
                    olItem.Move olDestFolder
                Set olItem = olItems.FindNext
                Wend
            Case Else: Exit Sub
        End Select

        For Each olAtt In colAtts

            '// The code looks last 4 characters,
            '// including period and will work as long
            '// as you use 4 characters in each extension.
            olFileType = LCase$(Right$(olAtt.FileName, 4))

            '// Select Case File & Print
            Select Case olFileType

                '// Add additional file types below
                Case "docx", ".pdf", ".doc"

                olFile = olDirectory & olAtt.FileName
                olAtt.SaveAsFile olFile

                '// to print attachements
                ShellExecute 0, "print", olFile, vbNullString, vbNullString, 0
            End Select
        Next
    End If
End Sub

推荐答案

声明了olAtt对象,但未在代码中对其进行初始化.您需要在代码中使用olMail对象:

The olAtt object is declared, but not initialized in the code. You need to use the olMail object instead in the code:

Private Sub SaveMovePrint(olMail As Outlook.MailItem)
    'On Error Resume Next
    Dim colAtts As Outlook.Attachments
    Dim olFile As String
    Dim olDirectory As String
    Dim olFileType As String
    Dim olNameSpace As Outlook.NameSpace
    Dim olInbox As Outlook.Folder
    Dim olDestFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim olItem As Object

    Set colAtts = olMail.Attachments
    Set olNameSpace = Application.GetNamespace("MAPI")
    Set olInbox = olNameSpace.GetDefaultFolder(olFolderInbox)
    Set olItems = olInbox.Items

    '// Save attachment then move
    If colAtts.Count Then

        '// Select Case save attch move
        Select Case olMail.SenderEmailAddress
            '// One
            Case "FaxOne@one.com"
                '// Save it to
                olDirectory = "C:\Users\Documents\FaxOne\"
                '// Move email to subfolder
                Set olDestFolder = olInbox.Folders("FaxOne")
                Set olItem = olItems.Find("[SenderName] = FaxOne@one.com'")
                While TypeName(olItem) <> "Nothing"
                    olItem.Move olDestFolder
                Set olItem = olItems.FindNext
                Wend

            '// Two
            Case "FaxTwo@two.com"
                '// Save attachments to
                olDirectory = "C:\Users\Documents\FaxTwo\" 
                Set olDestFolder = olInbox.Folders("FaxTwo")
                Set olItem = olItems.Find("[SenderName] = 'FaxTwo@two.com'")
                While TypeName(olItem) <> "Nothing"
                    olItem.Move olDestFolder
                Set olItem = olItems.FindNext
                Wend
            Case Else: Exit Sub
        End Select

        For Each olAtt In colAtts

            '// The code looks last 4 characters,
            '// including period and will work as long
            '// as you use 4 characters in each extension.
            olFileType = LCase$(Right$(olAtt.FileName, 4))

            '// Select Case File & Print
            Select Case olFileType

                '// Add additional file types below
                Case "docx", ".pdf", ".doc"

                olFile = olDirectory & olAtt.FileName
                olAtt.SaveAsFile olFile

                '// to print attachements
                ShellExecute 0, "print", olFile, vbNullString, vbNullString, 0
            End Select
        Next
    End If
End Sub

这篇关于运行时错误91 Outlook保存附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆