2016-07-04 22 views
0

最初の投稿をループ電子メールで追加のロジックを実行することができます。 しかし、私は迷惑メールボックスやデフォルト以外のアカウントの受信トレイを参照する方法を理解することができません。私のコードは、アカウントチェックを行ってもデフォルトのアカウントを使い続けます。見通しVBAの切り替えは、私が受信トレイにデフォルト以外の見通しアカウントの迷惑メールフォルダ内のすべての電子メールをダンプしようとしています</p> <p>...ので、私と一緒に裸してくださいここでは、電子メールを介して

Public Sub New_Mail() 
Dim oAccount As Outlook.Account 
Dim objSourceFolder As Outlook.MAPIFolder 
Dim objDestFolder As Outlook.MAPIFolder 
dim lngCount as long 

lngcount = 0 

For Each oAccount In Application.Session.Accounts ' cycle through accounts till we find the one we want 
    If oAccount = "[email protected]" Then 

     Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderJunk) ' select junk folder of the account 
     Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox) ' select inbox of the account 

     For lngCount = objSourceFolder.Items.Count To 1 Step -1 ' Go through all items in inbox, if a mail object, move into inbox 
      Set objVariant = objSourceFolder.Items.Item(dblCount) 
      DoEvents 
       If objVariant.Class = olMail Then 

        Set objCurrentEmail = objVariant    ' the inbox item is an email, so change object type to olMail (email object) 

        objCurrentEmail.Categories = "red category" 

        objCurrentEmail.Move objDestFolder    ' Move the email to the required folder 

       End If 
     Next 
    End If 
Next 
End Sub 

EDIT: エリックの回答後、私は私の今取り組んでコードを共有したいと思います。

Private Sub clearJunk() 

    Dim objVariant As Variant     ' Variant object to handle and inbox item 
    Dim objCurrentEmail As Outlook.MailItem  ' Temporary email object for logic 
    Dim dblCount As Double      ' Double used to count email items in the inbox 
    Dim objStore As Outlook.Store    ' Store Object to cycle through email accounts 
    Dim objRoot As Outlook.Folder    ' Folder object to define Inbox of desired account 
    Dim folders As Outlook.folders    ' FolderS object to holder folders...lol 
    Dim Folder As Outlook.Folder    ' Temporary Folder object 
    Dim foldercount As Integer     ' integer to count folders in account 
    Dim objInboxFolder As Outlook.MAPIFolder ' MAPI folder object to move emails to or from 
    Dim objJunkFolder As Outlook.MAPIFolder  ' MAPI folder object to move emails to or from 
    Dim objRandomFolder As Outlook.MAPIFolder ' MAPI folder object to move emails to or from 

    '-------------------------------------------------------------------- 
    ' Cycle through each account in outlook client and find desired account 
    For Each objStore In Application.Session.Stores   
     If objStore = "[email protected]" Then  ' If we find the account 

      Set objRoot = objStore.GetRootFolder  ' Store int objRoot Object 

      On Error Resume Next 

       Set folders = objRoot.folders   ' Check if it has folders 
       foldercount = folders.Count 

       If foldercount Then       ' if folders exist 

        For Each Folder In folders    ' Go through each folder AND .... 

         ' Look for Junk Email folder, Inbox Folder, and some random customer folder. 
         ' Store in individual objects for future referencing 
         If Folder.FolderPath = "\\[email protected]\Junk Email" Then 
          Set objJunkFolder = Folder 
         End If 
         If Folder.FolderPath = "\\[email protected]\Inbox" Then 
          Set objInboxFolder = Folder 
         End If 
         If Folder.FolderPath = "\\[email protected]\Random Custom Folder" Then 
          Set objRandomFolder = Folder 
         End If 

        Next 

       End If 

      ' Now we have everything identified lets move emails! 

      For dblCount = objJunkFolder.Items.Count To 1 Step -1 
       Set objVariant = objJunkFolder.Items.Item(dblCount) 
       DoEvents 
       If objVariant.Class = olMail Then 

        Set objCurrentEmail = objVariant     

        objCurrentEmail.Categories = "Red Category" 
        objCurrentEmail.Move objInboxFolder 

       End If 
      Next 
     End If 
    Next 

End Sub 

答えて

0

あなたは、デフォルト以外のアカウントのStore.GetDefaultFolder(olFolderInbox)を呼び出す必要があります。 Account.DeliveryStoreプロパティからストアオブジェクトを取得します。ほとんどの場合、正しいストアになります。たとえば、別のアカウントのストア(おそらくデフォルトアカウントのストア)にメッセージが配信されているPSTアカウントでない限りです。

+0

恐ろしく! 確かに正しい方向へ私を導いてくれる! 元の投稿を編集して、どのように動かすかを誰にでも見せるようにします。これを数回、100%成功させました。 ありがとうエリック! – zicon117

関連する問題