2016-08-12 44 views
3

我在嘗試製作一個宏,用於更改與用戶搜索的條件相匹配的電子郵件主題。使用VBA更改Outlook中選定項目的主題

因此,例如,用戶在搜索他們的收件箱「蘋果」,然後使用這個宏來更改所有與蘋果有在主題[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

只需添加myOlExp.SelectAllItems到你的代碼,就像

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