【问题标题】:How can I set the order of columns in Outlook E-Mail Table view through VBA?如何通过 VBA 设置 Outlook 电子邮件表格视图中的列顺序?
【发布时间】:2020-11-24 16:33:19
【问题描述】:

我使用 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

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    Microsoft Documentation有如下备注:

    在表格视图中,ViewFields 中 ViewField 对象的顺序 集合与字段列的顺序不同 显示在表格视图中。获取列顺序的解决方法 就是解析View.XML属性返回的字符串。

    因此,您似乎无法仅通过排列 ViewFields 集合的方式来影响列顺序。看起来您需要操作 XML。

    您是否考虑过简单地创建您想要的视图,通过 VBA 锁定该视图(以防止意外更改它),然后将该视图应用到各个文件夹?

    很抱歉,这不是一个答案,更像是一个评论,但是太长了,无法发表评论。

    【讨论】:

    • 嗨,山姆,感谢您的回答。你是对的,我实际上正在使用你建议的功能很长时间并且仍然存在。它涵盖了我 70% 的要求,但有些文件夹需要自定义视图(例如:在“已发送”文件夹中,我想要“收件人”而不是“发件人”列),所以我正在寻找“一键式”解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 2019-02-26
    • 2019-02-05
    相关资源
    最近更新 更多