0

Outlook 2007 で実行するために、VS2010 でこの小さなプログラムを作成しました。

受信トレイの標準的な読み取りには機能しますが、他のフォルダーを正しく指すことができません。「操作に失敗しました。オブジェクトが見つかりませんでした。 " ...

役立つ場合は、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 = "MrX@abc.com" Then
            If MyEMail.Attachments.Count > 0 Then
                MySubFolder = MyNS.Folders("Kickabout\Attachments")
            End If
            MyEMail.Move(MySubFolder)
        End If
    Next
End Sub

End Class

私の見通しの構造

4

1 に答える 1

0

OK、私はこれを自分で解決しました...誰かが将来に興味がある場合は、パスを設定する際に非常に明示的にする必要があり、そのためには関数が必要です。コードは次のとおりです...

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 = "MrX@abc.com" 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
于 2012-10-05T12:27:14.847 に答える