如何使用VBA更改MS Outlook 2013中的电子邮件类别?

时间:2015-09-18 11:54:49

标签: vba outlook outlook-vba

我尝试制作在VBA中完成某些操作后自动更改电子邮件类别的宏。

我创建了一些看起来不错的代码,但是它的问题并不总是有效 - 有时类别不会改变,看起来就像是错误:

Dim myolApp As Outlook.Application
Dim olFolder As Outlook.MAPIFolder
Dim ns As Outlook.NameSpace
Set ns = Outlook.GetNamespace("MAPI")
Set myolApp = CreateObject("Outlook.Application")
Set olFolder = ns.GetDefaultFolder(olFolderInbox)
Set olFolder = myolApp.ActiveExplorer.CurrentFolder
Dim itm As Object
Dim mailcattable As Variant
Dim kategoria As String

For Each itm In olFolder.Items

For b = 0 To UBound(mailcattable)
    If mailcattable(b) = "ABCD" Then
       mailcattable(b) = "After ABCD"
       kategoria = Join(mailcattable, "; ")
       itm.Categories = kategoria
    End If
Next
Next

你能告诉我如何解决它吗?

1 个答案:

答案 0 :(得分:0)

您永远不会保存该项目。重置每个项目的kategoria变量也是一个好主意。

    Dim bModified As Boolean
    For Each itm In olFolder.Items
      kategoria = ""
      bModified = false
      For b = 0 To UBound(mailcattable)
        If mailcattable(b) = "ABCD" Then
           mailcattable(b) = "After ABCD"
           kategoria = Join(mailcattable, "; ")
           itm.Categories = kategoria
           bModified = true
        End If
      Next
      if bModified Then 
        itm.Save
      End If 
    Next