2009年10月11日 星期日

改造 Taglocity 3.0 Tag Bar (2) ---- 反映標籤狀態

繼上一篇, 改造 Taglocity 3.0 Tag Bar (1) ---- 讓 按鈕文字 變清爽 之後。接下來, 要來看看如何讓『Tag Bar 反映標籤狀態』。

底下的 VBA 程式, 不論是 操作範圍 或 執行時機, 都比要前一篇來得複雜, 所以建議先熟悉 Outlook 的巨集運作, 再來實作。

 

(1) 按鈕文字變清爽

(2) 反映標籤狀態

操作範圍

輸入 : Tag Bar 的文字
輸出 : Tag Bar 的文字
輸入 : 當前的郵件標籤
輸出 : Tag Bar 的狀態

執行時機

只需要執行一次 需要重複執行

.

讓 Taglocity 3.0 Tag Bar 反映標籤狀態

為了彈性和容易除錯, 把程式碼拆成幾個部份

  • 第一部份 ---- updateTagBar()
    1. 查出目前 Outlook 的視窗狀況, 只對郵件列表 (Explorer) 作處理, 因為此時才有 Tag Bar。將來如果需要, 可以擴充到單篇郵件 (Inspector)。
    2. 將多封郵件的標籤連結在一個字串裡
    3. 呼叫第二部份
  • 第二部份 ---- syncTagBar("標籤字串")
    1. 確認輸入參數為字串, 以避免意外錯誤
    2. 如果 Tag Bar 可見 (.Visible=True) 才繼續往下
    3. 取出 .DescriptionText (如果不是空字串), 否則取出 .Caption
      ** 這是上一篇的伏筆, 因為按鈕文字 (.Caption) 經過處理之後, 可能會和標籤兜不起來, 所以上一篇先將資訊存在平常用不到內部說明欄位 (.DescriptionText)
    4.  移除 按鈕文字開頭的 &2. 或是 12. 等數字, 這樣不管 Tag Bar 有沒有經過『清爽化』的處理, 都可以反映標籤狀態
    5. 根據標籤, 將按鈕的 .State 欄位設為 msoButtonDown 或 msoButtonUp

2009-10-10_170220 實作出來就像這樣, 橘色底色反映標籤狀態

光是以上的程式, 只能做到『一次性』的反映標籤狀態。

雖然有幾種不同的方式可以實現『定時重複執行』或是『觸發執行』, 但各有優缺點, 就留待下一篇再來詳細說明囉。

.

  1. Public Sub updateTagBar()  
  2.     Dim oSel, oItem As Object  
  3.   
  4.     mailTags = ""  
  5.     Select Case TypeName(Application.ActiveWindow)  
  6.     Case "Explorer"  
  7.         Set oSel = Application.ActiveExplorer.Selection  
  8.         For i = 1 To oSel.Count  
  9.             Set oItem = oSel.Item(i)  
  10.             mailTags = mailTags + ", " + oItem.Categories  
  11.         Next  
  12.     Case Else  
  13.         Exit Sub  
  14.     End Select  
  15.           
  16.     syncTagBar (mailTags)  
  17. End Sub  
  18.   
  19. Sub syncTagBar(ByRef oInput As Variant)  
  20.       
  21.     On Error GoTo ErrorHandler  
  22.       
  23.     Select Case TypeName(oInput)  
  24.     Case "String"  
  25.         mailTags = oInput  
  26.     Case Else  
  27.         mailTags = ""  
  28.     End Select  
  29.   
  30.     Set myTagBar = Application.ActiveExplorer.CommandBars.Item("Taglocity 3.0 Tag Bar")  
  31.     If Not myTagBar.Visible Then Exit Sub  
  32.     For i = 1 To myTagBar.Controls.Count  
  33.         With myTagBar.Controls.Item(i)  
  34.             If Len(.DescriptionText) > 0 Then  
  35.                 btnTag = .DescriptionText  
  36.             Else  
  37.                 btnTag = .Caption  
  38.             End If  
  39. ' /-- Begin of optional section  
  40.             c = Left(btnTag, 1)  
  41.             If InStr(1, "&123456789", c) > 0 Then  
  42.                 p = InStr(1, btnTag, ". ")  
  43.                 If (p > 0) And (p <= 3) Then btnTag = Mid(btnTag, p + 2, 100)  
  44.             End If  
  45.             If Left(btnTag, 1) = " " Then btnTag = Mid(btnTag, 2, 100)  
  46. ' \-- End of optional section  
  47.             If (Len(mailTags) > 0) And (InStr(1, mailTags, btnTag) > 0) Then  
  48.                 .State = msoButtonDown  
  49.             Else  
  50.                 .State = msoButtonUp  
  51.             End If  
  52.         End With  
  53.     Next  
  54. ErrorHandler:  
  55. End Sub  


.


0 意見:

發表您的回應

張貼留言