首页 > 解决方案 > VBA Outlook如何取消选中删除纯文本中的多余换行符

问题描述

我不是真正的 VBA 开发人员,我说我正在尝试解决 Outlook 2013 宏的一些问题。我遇到的最后一个问题是关于默认的“我们从这条消息中删除了额外的换行符”。我想出了在选项>邮件>消息格式中取消选中此选项的位置,但我找不到有关如何以编程方式执行此操作的任何信息。

是否可以?

换行符

标签: vbaoutlook

解决方案


托尼·达利摩尔是对的。

Outlook 在启动时读取注册表更改。所以改变不会是立竿见影的。下面带有辅助函数的示例代码。

Outlook在 Windows 注册表中存储诸如删除纯文本消息中多余的换行符之类的选项。(对于我机器上的 Outlook 2016,关键是(注意版本16.0):

HKCU\Software\Microsoft\Office\16.0\Outlook\Options\Mail\AutoFormatPlainText

VBA 读取和写入 Windows 注册表的有限区域,HKEY_CURRENT_USER\Software\VB and VBA Program Settings\. 您可以使用Windows 脚本宿主对象模型库来读取和编辑注册表

VBA 附注:早期绑定和添加对 Windows 脚本宿主对象模型的引用有助于代码提示。(Visual Basic > 工具 > 参考...)

Option Explicit

Function isRemoveExtraLineBreaksChecked() As Boolean
    ' Outlook >
    '  File > Options >
    '   Mail > Message format >
    '    Remove extra line breaks in plain text messages
    ' Tested on Outlook 2016 Professional Plus running on Windows 7 Professional
    Dim wsh As New WshShell
    Dim appVer As String
    Dim key As String
    Dim val As Integer

    appVer = partialVersionNumberAsString(Application.version)
    key = "HKCU\Software\Microsoft\Office\" + appVer + "\Outlook\Options\Mail\AutoFormatPlainText"
    val = wsh.RegRead(key)

    'Tidy Up
    Set wsh = Nothing

    isRemoveExtraLineBreaksChecked = val = 1
End Function

Sub setRemoveExtraLineBreaksCheck(ByVal checked As Boolean)
    ' Outlook >
    '  File > Options >
    '   Mail > Message format >
    '    Remove extra line breaks in plain text messages
    ' Tested on Outlook 2016 Professional Plus running on Windows 7 Professional
    '
    ' Must restart Outlook so it can read new Registry value
    Dim wsh As New WshShell
    Dim appVer As String
    Dim key As String
    Dim val As Integer

    If checked Then
        val = 1
    Else
        val = 0
    End If

    appVer = partialVersionNumberAsString(Application.version)
    key = "HKCU\Software\Microsoft\Office\" + appVer + "\Outlook\Options\Mail\AutoFormatPlainText"
    wsh.RegWrite key, val, "REG_DWORD"

    'Tidy Up
    Set wsh = Nothing
End Sub

Function partialVersionNumberAsString(ByVal version As String, _
    Optional ByVal numberOfGroups As Integer = 2, _
    Optional ByVal inputSeparator As String = ".", _
    Optional ByVal outputSeparator As String = "." _
) As String
    ' Given a version number like 16.0.0.9226
    '  Return 16.0
    Debug.Assert numberOfGroups >= 0
    Debug.Assert Len(inputSeparator) = 1
    Debug.Assert Len(outputSeparator) = 1

    Dim versionExpanded() As String
    Dim versionToOutput() As String

    versionExpanded = Split(Application.version, inputSeparator)

    Dim actualNumberOfGroups As Integer
    Dim maxGroups As Integer
    actualNumberOfGroups = arrayLen(versionExpanded)
    If actualNumberOfGroups < numberOfGroups Then
        maxGroups = actualNumberOfGroups - 1
    Else
        maxGroups = numberOfGroups - 1
    End If

    ReDim versionToOutput(maxGroups)
    Dim i As Integer
    For i = 0 To maxGroups
        versionToOutput(i) = versionExpanded(i)
    Next i

    partialVersionNumberAsString = Join(versionToOutput, outputSeparator)
End Function

Function arrayLen(anyArray As Variant) As Integer
    arrayLen = UBound(anyArray) - LBound(anyArray) + 1
End Function

推荐阅读