簡體   English   中英

如何通過 VBA 設置 Outlook 電子郵件表視圖中的列順序?

[英]How can I set the order of columns in Outlook E-Mail Table view through VBA?

Outlook 電子郵件表列

我使用 Outlook 2019 管理十 (10) 個電子郵件帳戶
我更喜歡單行表格視圖

目標:在所有郵件文件夾中均等排列的列
在所有收件箱和子文件夾上以相同的寬度和順序顯示列重要性、圖標、狀態、附件、發件人、主題、日期、大小(期待我想要“發送”而不是“發件人”的“已發送”文件夾)。

現有的部分解決方案:

  • 我有一個 VBA 腳本(見下文)來讀取“參考文件夾”(我手動安排內容的地方)的格式(在Outlook.Folder.CurrentView.ViewFields ),然后將其應用於當前選定的文件夾
  • 它適用於列類型、寬度和名稱

一個問題仍然存在:

  • 這不會影響排序
  • 一切都已設置,但順序與配置中的順序不同

運行我的腳本后,列正確但順序錯誤

問題:如何通過 VBA 影響Outlook.Folder.CurrentView.ViewFields的列順序?

Sub ApplyReferenceColumnFormatToSelectedFolder()
    
    Dim myNamespace As Outlook.NameSpace
    Set myNamespace = Application.GetNamespace("MAPI")
    
    ' read ViewFields of reference folder into dictionary
    
    Dim refFolder As Outlook.Folder
    Set refFolder = myNamespace.Folders.Item("myaccount@myprovider.someTLD").Folders.Item("Posteingang")
    
    Dim refFields As Dictionary
    Set refFields = New Dictionary
    
    Dim thisField As Dictionary
    
    ' iterate over all fields and read the relevant config data
    ' result is in two-dimensional dictionary
    I = 1
    For Each refField In refFolder.CurrentView.ViewFields
        Set thisField = New Dictionary
        thisField("Label") = refField.ColumnFormat.Label
        thisField("ViewXMLSchemaName") = refField.ViewXMLSchemaName
        thisField("Width") = refField.ColumnFormat.Width
        thisField("FieldFormat") = refField.ColumnFormat.FieldFormat
        Set refFields(I) = thisField
        I = I + 1
    Next
    
    ' now "copy" this config to currently selected folder
    
    Set curFolder = Application.ActiveExplorer.CurrentFolder
    Set curView = curFolder.CurrentView
    
    ' remove all but one ViewFields
    ' (ideally, would remove all but there needs to be at least one remaining)
    oC = curView.ViewFields.Count
    If (oC > 1) Then
        For I = oC To 2 Step -1
            curView.ViewFields.Remove (I)
        Next
    End If
    curView.Apply
     
     
    ' now, set the desired configuration
    
    With curView
        ' set single-line table view without preview
        .AutoPreview = olAutoPreviewNone
        .MultiLine = olAlwaysSingleLine
        .ShowFullConversations = True
        '.Apply
     
        ' iterate over the columns form reference folder
        I = 1
        For Each refField In refFields
            Set thisField = refFields(refField)
            
            ' add field
            ' note: can fail, if field of same type already exists
            ' then, we can just "resume next" without adding :-)
            On Error Resume Next
            .ViewFields.Add (thisField("ViewXMLSchemaName"))
            .Apply
            
            ' finally, set the relevant properties
            .ViewFields.Item(I).ColumnFormat.Label = thisField("Label")
            .ViewFields.Item(I).ColumnFormat.Width = thisField("Width")
            .ViewFields.Item(I).ColumnFormat.FieldFormat = thisField("FieldFormat")
            .Apply
            
            I = I + 1
        Next
    End With

    curView.Apply
    
End Sub

Microsoft 文檔有以下注釋:

在表視圖中,ViewFields 集合中的 ViewField 對象的順序與字段列在表視圖中的顯示順序不同。 獲取列順序的解決方法是解析 View.XML 屬性返回的字符串。

因此,您似乎無法簡單地通過排列 ViewFields 集合的方式來影響列順序。 看起來您需要操作 XML。

您是否考慮過簡單地創建所需的視圖,通過 VBA 鎖定該視圖(以防止意外更改它),然后將該視圖應用於各種文件夾?

對不起,這不是一個答案,更多的是評論,但發表評論太長了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM