2017-09-22 167 views
0

因此,我已經掌握了一些基本知識,但我無法弄清楚如何執行創建任務或移動電子郵件等操作。我創建了一個腳本來手動更新主題,但這並不能幫助我實現最終目標。Outlook VBA在收到電子郵件時更改主題,移動電子郵件和創建任務

Prob不是最好的方式來做到這一點,但它的工作原理。基本上,我需要在收到新電子郵件時關閉此功能,編輯如下所示的主題,將其移至子文件夾....讓我們將其稱爲1,然後創建一個2天的任務來完成。

Sub ChangeSubjects() 
Dim Item As Outlook.MailItem 
Dim oInspector As Inspector 
Dim oExplorer As Explorer 
Dim oCt As Integer 
Dim cCt As Integer 
Dim iSubject As String 
Set oInspector = Application.ActiveInspector 
Set oExplorer = Application.ActiveExplorer 
cCt = 1 
oCt = oExplorer.Selection.Count 

For Each Item In Application.ActiveExplorer.Selection 
    If oInspector Is Nothing Then 
     Set Item = Application.ActiveExplorer.Selection.Item(cCt) 
     Item.Display 
     Set oInspector = Application.ActiveInspector 'Reassign oInpsector and Item again 
     Set Item = oInspector.currentItem 
    Else 
     Set Item = oInspector.currentItem 
    End If 

    If Left(Item.Subject, 27) = "Look for this string" Then Item.Subject = Mid(Item.Subject, 29) 
     Item.Close (2) 
    Set oInspector = Application.ActiveInspector 
    cCt = cCt + 1 
Next 
End Sub 
+1

該網站已建立,因此你會問四個獨立的問題,如果他們沒有被回答過一篇文章的鏈接。零件是分開的,所以未來的搜索者,就像你在這個評論之後,可以找到答案並根據需要將它們放在一起。這樣,如果您可以單獨解決每個部分的問題,如果您選擇這樣做,請接受四個答案,這是您在此廣泛文章中無法做到的。 – niton

回答

2

下面是如何移動電子郵件:

Sub MoveItems() 
Dim myNameSpace As Outlook.NameSpace 
Dim myInbox As Outlook.Folder 
Dim myDestFolder As Outlook.Folder 
Dim myItems As Outlook.Items 
Dim myItem As Object 

Set myNameSpace = Application.GetNamespace("MAPI") 
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) 
Set myItems = myInbox.Items 
Set myDestFolder = myInbox.Folders("Test") 
Set myItem = myItems.Find("[Subject] = '[Subject Name]'") 
While TypeName(myItem) <> "Nothing" 
    myItem.Move myDestFolder 
    Set myItem = myItems.FindNext 
Wend 
End Sub 

您也可以Set myItem = myItems.Find("[Subject] = '[Subject Name]'")等於Set myItem = myItems.Find("[SenderName] = '[Sender Name]'"),但警告說,它會找到所有與發件人或主題名稱的電子郵件。以下代碼將更改電子郵件的主題名稱。

Sub ChangeSubject() 
Dim obj As Object 
Dim msg As Outlook.MailItem 
Dim subject As String 
Set obj = ActiveExplorer.Selection 
subject = "Picture" 
If Not obj Is Nothing Then 
    For i = 1 To obj.Count 
    Set msg = obj.Item(i) 
    With msg 
     .subject = subject 
    End With 
    Next i 
ElseIf Not msg Is Nothing Then 
    msg.subject = subject 
End If 
End Sub 

下面是關於創建任務https://www.devhut.net/2010/09/03/vba-create-an-outlook-task/