Outlook VBA保存附件代码失败

时间:2013-05-23 20:29:59

标签: vba outlook save directory attachment

我在网上找到了我想用来保存电子邮件文件夹中附件的代码,但它不起作用。我不是一个程序员,这似乎适合我的需求。如果有人可以解决此问题而不是建议其他代码,将非常感激。我正在使用outlook 2007并且outlook文件夹的名称是正确的。谢谢!

http://www.vbaexpress.com/kb/getarticle.php?kb_id=522#instr

代码失败于:

Set TargetFolderItems = ns.Folders.Item( _
"Personal Folders").Folders.Item("Temp").Items 

并给出错误消息:

  

'运行时错误'-2147221233(8004010f)':   操作失败。无法找到对象'

任何帮助都会非常感激。


'###############################################################################
 '### Module level Declarations
 'expose the items in the target folder to events
Option Explicit 
Dim WithEvents TargetFolderItems As Items 
 'set the string constant for the path to save attachments
Const FILE_PATH As String = "C:\********\" 

 '###############################################################################
 '### this is the Application_Startup event code in the ThisOutlookSession module
Private Sub Application_Startup() 
     'some startup code to set our "event-sensitive" items collection
    Dim ns As Outlook.NameSpace 
     '
    Set ns = Application.GetNamespace("MAPI") 
    Set TargetFolderItems = ns.Folders.Item( _ 
    "Inbox").Folders.Item("BS CDGL").Items 

End Sub 

 '###############################################################################
 '### this is the ItemAdd event code
Sub TargetFolderItems_ItemAdd(ByVal Item As Object) 
     'when a new item is added to our "watched folder" we can process it
    Dim olAtt As Attachment 
    Dim i As Integer 

    If Item.Attachments.Count > 0 Then 
        For i = 1 To Item.Attachments.Count 
            Set olAtt = Item.Attachments(i) 
             'save the attachment
            olAtt.SaveAsFile FILE_PATH & olAtt.FileName 

             'if its an Excel file, pass the filepath to the print routine
            If UCase(Right(olAtt.FileName, 3)) = "XLS" Then 
                PrintAtt (FILE_PATH & olAtt.FileName) 
            End If 
        Next 
    End If 

    Set olAtt = Nothing 

End Sub 

 '###############################################################################
 '### this is the Application_Quit event code in the ThisOutlookSession module
Private Sub Application_Quit() 

    Dim ns As Outlook.NameSpace 
    Set TargetFolderItems = Nothing 
    Set ns = Nothing 

End Sub 

 '###############################################################################
 '### print routine
Sub PrintAtt(fFullPath As String) 

    Dim xlApp As Excel.Application 
    Dim wb As Excel.Workbook 

     'in the background, create an instance of xl then open, print, quit
    Set xlApp = New Excel.Application 
    Set wb = xlApp.Workbooks.Open(fFullPath) 
    wb.PrintOut 
    xlApp.Quit 

     'tidy up
    Set wb = Nothing 
    Set xlApp = Nothing 

End Sub 

1 个答案:

答案 0 :(得分:0)

您已跳过文件夹。

更改

Set TargetFolderItems = ns.Folders.Item( _ 
"Inbox").Folders.Item("BS CDGL").Items 

Set TargetFolderItems = ns.Folders.Item("Other Mailbox Name").Folders.Item( _ 
"Inbox").Folders.Item("BS CDGL").Items