ThisOutlookSession モジュールから複数の Outlook フォルダーにマクロを適用するにはどうすればいいですか?

ThisOutlookSession モジュールから複数の Outlook フォルダーにマクロを適用するにはどうすればいいですか?

このサイトの情報を使用して、メッセージを親フォルダーに移動するときに、メッセージを「送信者名」サブフォルダーに分類するマクロを作成できました。例:

  1. 受信トレイにメッセージが届きます。

  2. メッセージを「フォローアップ」フォルダに移動します

  3. 送信者名という名前のサブフォルダがない場合、作成されます

    3a. メッセージはすぐにフォローアップ/送信者名に移動されます

以下のコードはこれらの手順を完璧に実行します。次に必要なのは、コードを他のフォルダーに適用することです。現時点では、自動的に動作するようにしたいため、コードは「ThisOutlookSession」モジュール内にあります。

私の質問は、受信トレイの複数のサブフォルダーにマクロを適用するにはどうすればよいかということです。

受信トレイ - ここでは適用されません

  follow-up  - applied here
  team       - applied here
  vendors    - applied here

これまでのコードは次のとおりです。

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace

  ' set object reference to default Inbox
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Folders("Follow-up").Items
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)
' fires when new item added to default Inbox
' (per Application_Startup)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim targetFolder As Outlook.MAPIFolder
  Dim senderName As String

  ' don't do anything for non-Mailitems
  If TypeName(item) <> "MailItem" Then GoTo ProgramExit

  Set Msg = item

  ' move received email to target folder based on sender name
  senderName = Msg.senderName

  If CheckForFolder(senderName) = False Then  ' Folder doesn't exist
    Set targetFolder = CreateSubFolder(senderName)
  Else
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("follow-up").Folders(senderName)
  End If

  Msg.Move targetFolder

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

Function CheckForFolder(strFolder As String) As Boolean
' looks for subfolder of specified folder, returns TRUE if folder exists.
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox).Folders("follow-up")

' try to set an object reference to specified folder
On Error Resume Next
Set FolderToCheck = olInbox.Folders(strFolder)
On Error GoTo 0

If Not FolderToCheck Is Nothing Then
  CheckForFolder = True
End If

ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function

Function CreateSubFolder(strFolder As String) As Outlook.MAPIFolder
' assumes folder doesn't exist, so only call if calling sub knows that
' the folder doesn't exist; returns a folder object to calling sub
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olInbox = olNS.GetDefaultFolder(olFolderInbox).Folders("Follow-up")

Set CreateSubFolder = olInbox.Folders.Add(strFolder)

ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function

答え1

誰かがこれを見つけて不思議に思うかもしれないので、私は自分で解決することができました。私はまだこの分野に不慣れで、「アイテム」タグを変更して自分が見ているものを定義してから、新しい変数をそれぞれ独自のフォルダーを指すように設定できることに気づいていませんでした。それを実行すると、フォルダーごとにサブを追加して、作業を開始できました。

Private WithEvents Corporate As Outlook.Items
Private WithEvents Subsidiary As Outlook.Items
Private WithEvents ServDsk As Outlook.Items
Private WithEvents Vendors As Outlook.Items


Public Sub Application_Startup()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace

  ' set object reference to default Inbox
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  Set DefFol = objNS.GetDefaultFolder(olFolderInbox)
  Set Corporate = DefFol.Folders("Corporate Teammates").Items
  Set Subsidiary = DefFol.Folders("Subsidiary").Items
  Set ServDsk = DefFol.Folders("Service Desk").Items
  Set Vendors = DefFol.Folders("Vendors").Items

End Sub


Private Sub Corporate_ItemAdd(ByVal up As Object)


' fires when new item added to default Inbox
' (per Application_Startup)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim targetFolder As Outlook.MAPIFolder
  Dim senderName As String
  Dim dirstr As String
  Dim strDomain As String


  ' don't do anything for non-Mailitems
  If TypeName(up) <> "MailItem" Then GoTo ProgramExit

  Set Msg = up

  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")

  ' move received email to target folder based on sender name
  senderName = Msg.senderName
  dirstr = "Corporate Teammates"



  If CheckForFolder(senderName, dirstr) = False Then ' Folder doesn't exist
    Set targetFolder = CreateSubFolder(senderName, dirstr)
  Else
   ' Set olApp = Outlook.Application
   ' Set objNS = olApp.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Folders(dirstr).Folders(senderName)
  End If

  Msg.UnRead = False
  Msg.Move targetFolder

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & dirstr
  Resume ProgramExit
End Sub

Private Sub Subsidiary_ItemAdd(ByVal up As Object)


' fires when new item added to default Inbox
' (per Application_Startup)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim targetFolder As Outlook.MAPIFolder
  Dim senderName As String
  Dim dirstr As String


  ' don't do anything for non-Mailitems
  If TypeName(up) <> "MailItem" Then GoTo ProgramExit

  Set Msg = up

  ' move received email to target folder based on sender name
  senderName = Msg.senderName
  dirstr = "Subsidiary"

  If CheckForFolder(senderName, dirstr) = False Then ' Folder doesn't exist
    Set targetFolder = CreateSubFolder(senderName, dirstr)
  Else
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Folders(dirstr).Folders(senderName)
  End If

  Msg.UnRead = False
  Msg.Move targetFolder

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & dirstr
  Resume ProgramExit
End Sub

Private Sub ServDsk_ItemAdd(ByVal up As Object)


' fires when new item added to default Inbox
' (per Application_Startup)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim targetFolder As Outlook.MAPIFolder
  Dim folderName As String
  Dim dirstr As String


  ' don't do anything for non-Mailitems
  If TypeName(up) <> "MailItem" Then GoTo ProgramExit


  ' move received email to target folder based on sender name
  dirstr = "Service Desk"

' checks the subject to decide how to handle the sorting
Select Case True
   Case InStr(Msg.Subject, "Demand") > 0
    folderName = "Demands"
   Case InStr(Msg.Subject, "Demand") = 1
    folderName = "Demands"
   Case InStr(Msg.Subject, "Incident") > 0
    folderName = "Tickets"
   Case InStr(Msg.Subject, "Problem") > 0
    folderName = "Tickets"
   Case InStr(Msg.Subject, "Opened") > 0
    folderName = "Tickets"
   Case InStr(Msg.Subject, "task") > 0
    folderName = "Tasks"
   Case InStr(Msg.Subject, "Status") > 0
    folderName = "Tickets"
   Case InStr(Msg.Subject, "TASK") > 0
    folderName = "Tasks"
   Case InStr(Msg.Subject, "Approval") > 0
    folderName = "OCH Approval requests"
   Case InStr(Msg.Subject, "Request") > 0
    folderName = "Requests"
   Case InStr(Msg.Subject, "Maintenance") > 0
    folderName = "Maintenance"
   Case InStr(Msg.Subject, "Alert") > 0
    folderName = "Alerts"
   Case InStr(Msg.Subject, "Notice") > 0
    folderName = "Alerts"
   Case InStr(Msg.Subject, "Reminder") > 0
    folderName = "Alerts"
End Select


  If CheckForFolder(folderName, dirstr) = False Then ' Folder doesn't exist
    Set targetFolder = CreateSubFolder(folderName, dirstr)
  Else
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Folders(dirstr).Folders(folderName)
  End If

  Msg.UnRead = False
  Msg.Move targetFolder

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & dirstr
  Resume ProgramExit
End Sub

Private Sub Vendors_ItemAdd(ByVal up As Object)


' fires when new item added to default Inbox
' (per Application_Startup)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Dim targetFolder As Outlook.MAPIFolder
  Dim senderName As String
  Dim dirstr As String
  Dim strDomain As String


  ' don't do anything for non-Mailitems
  If TypeName(up) <> "MailItem" Then GoTo ProgramExit

  Set Msg = up

' strips the domain name out of the sender address
  If InStr(1, Msg.SenderEmailAddress, "@") > 0 Then
         strDomain = Right(Msg.SenderEmailAddress, Len(Msg.SenderEmailAddress) - InStr(Msg.SenderEmailAddress, "@"))
  End If


If CheckForFolder(senderName, strDomain) = False Then ' Folder doesn't exist
    Set targetFolder = CreateSubFolder(senderName, strDomain)
  Else
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set targetFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Vendors").Folders(strDomain)
End if

  Msg.UnRead = False
  Msg.Move targetFolder

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description & dirstr
  Resume ProgramExit
End Sub





Function CheckForFolder(strFolder As String, dirstr As String) As Boolean
' looks for subfolder of specified folder, returns TRUE if folder exists.
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder
Dim FolderToCheck As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")


Set olInbox = olNS.GetDefaultFolder(olFolderInbox).Folders(dirstr)


' try to set an object reference to specified folder
On Error Resume Next
Set FolderToCheck = olInbox.Folders(strFolder)
On Error GoTo 0

If Not FolderToCheck Is Nothing Then
  CheckForFolder = True
End If

ExitProc:
Set FolderToCheck = Nothing
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing

End Function




Function CreateSubFolder(strFolder As String, dirstr As String) As Outlook.MAPIFolder
' assumes folder doesn't exist, so only call if calling sub knows that
' the folder doesn't exist; returns a folder object to calling sub
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olInbox As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")

Set olInbox = olNS.GetDefaultFolder(olFolderInbox).Folders(dirstr)
Set CreateSubFolder = olInbox.Folders.Add(strFolder)


ExitProc:
Set olInbox = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Function

関連情報