VBA 在第二个收件箱中(共享帐户)
VBA in second inbox (shared account)
我完全是一个VBA新手。所以,这可能是一个非常基本的问题,但我根本无法让它与所有其他非常有用的线程一起工作。
我使用以下 VBA 代码将电子邮件移动到收件箱的特定子文件夹,只要它被标记为“发票”。
Private WithEvents objInboxFolder As Outlook.Folder
Private WithEvents objInboxItems As Outlook.Items
'Process inbox mails
Private Sub Application_Startup()
Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
Set objInboxItems = objInboxFolder.Items
End Sub
'Occurs when changing item
Private Sub objInboxItems_ItemChange(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim objTargetFolder As Outlook.Folder
If TypeOf Item Is MailItem Then
Set objMail = Item
'Move mails based on color category
If InStr(objMail.Categories, "Invoice") > 0 Then
Set objTargetFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Invoices").Folders("Uploaded")
objMail.Move objTargetFolder
End If
End If
End Sub
这很好用。唯一的问题是我在 Outlook 中有 2 个不同的 mailboxes/accounts:我的个人电子邮件地址以及 Accounting@company.com(多人使用)。我如何才能更改代码以处理会计收件箱而不是我的个人标准收件箱?
非常感谢!
您可以使用 Store.GetDefaultFolder method which returns a Folder
object that represents the default folder in the store and that is of the type specified by the FolderType
argument. This method is similar to the GetDefaultFolder method of the NameSpace 对象。不同之处在于此方法获取与帐户关联的交付商店上的默认文件夹,而 NameSpace.GetDefaultFolder returns 当前配置文件的默认商店上的默认文件夹。
因此,您可以在 Outlook 配置文件中枚举商店并找到所需的商店。例如,以下代码显示了如何递归地遍历 Outlook 中的所有存储和文件夹:
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
您可以查看 Store.DisplayName 属性 在配置文件中找到所需的商店,然后在 Outlook 中获取特定商店所需的收件箱文件夹。
我完全是一个VBA新手。所以,这可能是一个非常基本的问题,但我根本无法让它与所有其他非常有用的线程一起工作。
我使用以下 VBA 代码将电子邮件移动到收件箱的特定子文件夹,只要它被标记为“发票”。
Private WithEvents objInboxFolder As Outlook.Folder
Private WithEvents objInboxItems As Outlook.Items
'Process inbox mails
Private Sub Application_Startup()
Set objInboxFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox)
Set objInboxItems = objInboxFolder.Items
End Sub
'Occurs when changing item
Private Sub objInboxItems_ItemChange(ByVal Item As Object)
Dim objMail As Outlook.MailItem
Dim objTargetFolder As Outlook.Folder
If TypeOf Item Is MailItem Then
Set objMail = Item
'Move mails based on color category
If InStr(objMail.Categories, "Invoice") > 0 Then
Set objTargetFolder = Application.Session.GetDefaultFolder(olFolderInbox).Folders("Invoices").Folders("Uploaded")
objMail.Move objTargetFolder
End If
End If
End Sub
这很好用。唯一的问题是我在 Outlook 中有 2 个不同的 mailboxes/accounts:我的个人电子邮件地址以及 Accounting@company.com(多人使用)。我如何才能更改代码以处理会计收件箱而不是我的个人标准收件箱?
非常感谢!
您可以使用 Store.GetDefaultFolder method which returns a Folder
object that represents the default folder in the store and that is of the type specified by the FolderType
argument. This method is similar to the GetDefaultFolder method of the NameSpace 对象。不同之处在于此方法获取与帐户关联的交付商店上的默认文件夹,而 NameSpace.GetDefaultFolder returns 当前配置文件的默认商店上的默认文件夹。
因此,您可以在 Outlook 配置文件中枚举商店并找到所需的商店。例如,以下代码显示了如何递归地遍历 Outlook 中的所有存储和文件夹:
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
您可以查看 Store.DisplayName 属性 在配置文件中找到所需的商店,然后在 Outlook 中获取特定商店所需的收件箱文件夹。