2012-10-04 200 views
0

Outlook程序我在VS2010寫了這個小程序在Outlook 2007中以附件移動到子文件夾

它的工作原理,通過收件箱中的閱讀標準運行,但我不能讓它正確指向其他文件夾,我得到一個「COMException被用戶代碼未處理」錯誤,說「操作失敗,找不到對象」。 ...

我已經包括了我的Outlook結構的截圖,如果它可以幫助...

Imports Microsoft.Office.Interop 

Public Class ThisAddIn 
Private Sub ThisAddIn_Startup() Handles Me.Startup 
End Sub 
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
End Sub 
Private Sub Application_Startup() Handles Application.Startup 

    Dim MyApp As Outlook.Application = New Outlook.Application 
    Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI") 
    Dim MyInbox As Outlook.MAPIFolder = MyNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 
    Dim MyEmails As Integer = MyInbox.Items.Count 
    Dim MyEMail As Outlook.MailItem 
    Dim MyCount As Integer 
    Dim MySubFolder As Outlook.MAPIFolder = MyNS.Folders("Kickabout") **<<< Error occurs here** 

    For MyCount = MyEmails To 1 Step -1 
     MyEMail = MyInbox.Items(MyCount) 
     If MyEMail.SenderEmailAddress = "[email protected]" Then 
      If MyEMail.Attachments.Count > 0 Then 
       MySubFolder = MyNS.Folders("Kickabout\Attachments") 
      End If 
      MyEMail.Move(MySubFolder) 
     End If 
    Next 
End Sub 

End Class 

My Outlook Structure

+0

您是否嘗試過使用調試器和SETT在'Dim MySubFolder ...'行添加一個斷點並展開'MyNs.Folders'樹來查看裏面有什麼?在Exchange Web服務中,我不認爲它通過名稱來鍵入它們,而是通過ID來鍵入它們,並且您必須使用單獨的函數來按名稱查找它們。 – Origin

+0

是的,我正在使用調試器,並且無處可尋!然而,我在Sue Mosher的舊網站www.outlookcode.com找到了一個解決方案,您是對的,我需要一個功能,我現在將代碼放入... –

回答

0

好吧,我有這個解決自己...如果有人有興趣的未來,你必須要在建立路徑&需要一個函數來做到這一點,這裏是代碼比較明確...

Imports Microsoft.Office.Interop 

Public Class ThisAddIn 

Dim strFolderPath As String 

Private Sub ThisAddIn_Startup() Handles Me.Startup 
End Sub 
Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown 
End Sub 
Private Sub Application_Startup() Handles Application.Startup 

    Dim MyApp As Outlook.Application = New Outlook.Application 
    Dim MyNS As Outlook.NameSpace = MyApp.GetNamespace("MAPI") 
    Dim MyInbox As Outlook.MAPIFolder = MyNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) 
    Dim MyEmails As Integer = MyInbox.Items.Count 
    Dim MyEMail As Outlook.MailItem 
    Dim MyCount As Integer 
    Dim MySubFolder As Outlook.Folder = GetMyFolder("Outlook (Gary)\Kickabout") 
    Stop 
    For MyCount = MyEmails To 1 Step -1 
     MyEMail = MyInbox.Items(MyCount) 
     If MyEMail.SenderEmailAddress = "[email protected]" Then 
      If MyEMail.Attachments.Count &gt; 0 Then 
       MySubFolder = GetMyFolder("Outlook (Gary)\Kickabout\Attachments") 
      End If 
      MyEMail.Move(MySubFolder) 
     End If 
    Next 
End Sub 

Function GetMyFolder(FolderPath) 
    ' folder path needs to be something like 
    ' "Public Folders\All Public Folders\Company\Sales" 
    Dim aFolders 
    Dim fldr 
    Dim i 
    Dim objNS 

    On Error Resume Next 
    strFolderPath = Replace(FolderPath, "/", "\") 
    aFolders = Split(FolderPath, "\") 

    'get the Outlook objects 
    ' use intrinsic Application object in form script 
    objNS = Application.GetNamespace("MAPI") 

    'set the root folder 
    fldr = objNS.Folders(aFolders(0)) 

    'loop through the array to get the subfolder 
    'loop is skipped when there is only one element in the array 
    For i = 1 To UBound(aFolders) 
     fldr = fldr.Folders(aFolders(i)) 
     'check for errors 
     'If Err() &lt;&gt; 0 Then Exit Function 
    Next 
    GetMyFolder = fldr 

    ' dereference objects 
    objNS = Nothing 
End Function 
End Class 
相關問題