使用VBA

时间:2016-08-12 16:01:46

标签: vba email outlook outlook-vba

我试图创建一个宏来更改符合用户搜索条件的电子邮件主题。

例如,用户搜索苹果'在他们的收件箱中,然后使用此宏来更改与苹果相关的所有电子邮件,使其在主题中具有 [APPLES]

问题是我现在的代码只更改了用户点击的单个电子邮件的主题,而不是所有选定的电子邮件。我对VBA很陌生,但我很确定它与我使用的ActiveExplorer和选择功能有关。

以下是代码:

Sub AddString()
Dim myolApp As Outlook.Application
Dim aItem As Object

Set myolApp = CreateObject("Outlook.Application")
Set mail = myolApp.ActiveExplorer.CurrentFolder
Dim iItemsUpdated As Integer
Dim strTemp As String
Dim strString As String
Dim myOlExp As Outlook.Explorer
Dim myOlSel As Object


' User input
strString = InputBox("Enter the project code")
iItemsUpdated = 0

' Empty value or cancel button
If strString = "" Then Exit Sub

' Writes string to e-mail subject
Set myOlExp = myolApp.ActiveExplorer
Set myOlSel = myOlExp.Selection
For x = 1 To myOlSel.Count
    strTemp = "[" & strString & "] " & myOlSel.Item(x).Subject
    myOlSel.Item(x).Subject = strTemp
    myOlSel.Item(x).Save
    iItemsUpdated = iItemsUpdated + 1
Next x

' Tells user how many items have been updated
MsgBox iItemsUpdated & " of " & mail.Items.Count & " Messages Updated"
Set myolApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

只需在代码中添加myOlExp.SelectAllItems,例如

' Writes string to e-mail subject
Set myOlExp = myolApp.ActiveExplorer
myOlExp.SelectAllItems
Set myOlSel = myOlExp.Selection