2011-11-23 32 views
1

第一眼看到Luke123的'追加標籤到Outlook'請求非常相似的要求。 在這裏,我需要在Outlook Exchange(共享)郵箱的主題行中追加一個自動編號任務ID(僅限於此要求)。自動附加一個自動編號的TaskID到Outlook Exchange(共享)郵箱

因此需要a)順序自動編號和b)在電子郵件登陸時自動運行。

針對共享郵箱的漂亮的特定規則運行在服務器端並被業務鎖定。

感謝所有的想法/幫助。

回答

0

這段代碼很可能需要調整,但應該做你想做的事情。您可能需要拿起現有的收件箱項目並給他們一個任務ID以使球滾動。查看我的意見,瞭解代碼的解釋以及需要編輯的地方。

Private WithEvents Items As Outlook.Items 

Private Sub Application_Startup() 
    Dim olApp As Outlook.Application 
    Set olApp = Outlook.Application 
    ' edit this line to reflect the actual mailbox name as displayed in Outlook 
    Set Items = Session.Folders("Mailbox - My Shared Mailbox").Folders("Inbox") 
End Sub 

Private Sub Items_ItemAdd(ByVal item As Object) 

    On Error GoTo ErrorHandler 

    Dim msg As Outlook.mailItem 
    Dim firstObj As Object 
    Dim i As Long 
    Dim firstMsg As Outlook.mailItem 
    Dim currentTaskID As String 
    Dim nextTaskID As Long 

    If TypeName(item) = "MailItem" Then ' it's an email 
    Set msg = item 

    ' get first email from Inbox to determine next task ID 
    Do Until TypeName(firstObj) = "MailItem" Or i = Session.Folders("Mailbox - My Shared Mailbox").Folders("Inbox").Items.Count 
     i = i + 1 
     ' might have to start at item #2? 
     Set firstObj = Session.GetDefaultFolder(olFolderInbox).Items(i) 
    Loop 

    ' typecast the object to mailitem for Intellisense 
    If TypeName(firstObj) = "MailItem" Then 
     Set firstMsg = firstObj 
    Else 
     ' display messagebox? 
     Goto ProgramExit 
    End If 

    ' get task id and calculate next value, let's assume it's the last three chars of subject 
    ' Ex: Subject: Incoming Email - TaskId: 001 
    currentTaskID = Right$(firstMsg.Subject, 3) 
    nextTaskID = CLng(currentTaskID) + 1 

    ' put next task ID number into new email's subject 
    With msg 
     .Subject = msg.Subject & " - TaskId: " & nextTaskID 
     .Save 
    End With 

    End If 

ProgramExit: 
    Exit Sub 
ErrorHandler: 
    MsgBox Err.Number & " - " & Err.Description 
    Resume ProgramExit 
End Sub 
+0

感謝JP和我的apols沒有得到更快。我被拖到另一個工作項目上,只是剛剛回到這個問題。 真的很感謝幫助。 – PocketGuy

+0

@PocketGuy沒問題,謝謝你反饋! – JimmyPena