outlook 2013收到的邮件

时间:2014-04-23 11:32:14

标签: vba email outlook

我已经建立了一个VBA项目,用于检查特殊电子邮件的收件箱 提取附件并将​​附件保存在网络上 这一切都发生在用户点击按钮时。

我现在的问题是我想自动化这个。
因此我试图重写VBA项目但是 当电子邮件到达时,我总是收到错误消息
'Unzulässiger或者nicht ausreichend defnierter Verweis'

(tr。不正确或没有足够的定义参考)

我无法弄清楚该做什么,因此我正在尝试 在这里得到答案。

附上你会找到放在'ThisOutlookSession'

中的代码
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()

Dim objNs As Outlook.NameSpace
Dim X As Integer

Set objNs = GetNamespace("MAPI")
Set Items = objNs.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)

Dim objNs As Outlook.NameSpace
Dim strPath, strAuditPath, strSavPath, strFolderName As String
Dim oAttachment As Outlook.Attachment
Dim objTrash As Outlook.Folder
Dim intAnlagen, intTotal, i As Integer

Set objNs = GetNamespace("MAPI")

On Error GoTo check_error

If TypeOf Item Is Outlook.MailItem Then
    Dim Msg As Outlook.MailItem
    Set Msg = Item

    If Msg.SenderEmailAddress = "notify@someone.com" Then 
        If Left(Msg.Subject, 8) = "QHST-Log" Then 

        strSavPath = "D:\Users\AS400_QHST_Logs\"
        strPath = "T:\DOKUMENTE\AS400\QHST-Logs\"
        strAuditPath = "D:\Dropbox\QHST-Log\"

        strFolderName = Right(Msg.Subject, 4)
            If Dir(strPath & strFolderName, vbDirectory) = vbNullString Then 'Prüfen ob Subfolder der Form JJJJ angelegt ist.
                MkDir strPath & strFolderName
                MkDir strAuditPath & strFolderName
                MkDir strSavPath & strFolderName
            End If
            strPath = strPath & strFolderName & "\"
            strAuditPath = strAuditPath & strFolderName & "\"
            strSavPath = strSavPath & strFolderName & "\"
            strFolderName = Mid(.Subject, 14, 2)

            If Dir(strPath & strFolderName, vbDirectory) = vbNullString Then 
                MkDir strPath & strFolderName
                MkDir strAuditPath & strFolderName
                MkDir strSavPath & strFolderName
            End If
            strPath = strPath & strFolderName & "\"
            strAuditPath = strAuditPath & strFolderName & "\"
            strSavPath = strSavPath & strFolderName & "\"

            intAnlagen = Msg.Attachments.Count
            intTotal = intTotal + intAnlagen
            'Debug.Print objNewMail & ": "; intanlagen
            If intAnlagen > 0 Then
                For i = 1 To intAnlagen
                    Set oAttachment = Msg.Attachments.Item(i)
                    oAttachment.SaveAsFile strPath & oAttachment.FileName
                    oAttachment.SaveAsFile strAuditPath & oAttachment.FileName
                Next i
            End If
            Msg.UnRead = False
            Msg.Delete
        End If
    End If
End If

check_error:
Debug.Print Err.Number; Err.Description
If Err.Number = 75 Then 
   Err.Clear
   GoTo Back1:
Else
   Err.Raise Err.Number, Err.Description
End If

Err.Clear
Resume Next

End Sub

1 个答案:

答案 0 :(得分:0)

尝试

Set objNs = Application.GetNamespace("MAPI")

编辑:引用应用程序对象

http://msdn.microsoft.com/en-us/library/office/ff865800%28v=office.15%29.aspx

语法

表达式.GetNamespace(Type)

expression表示Application对象的变量。