将文档中 table 中的格式化条目添加到自动更正库

Add formatted entries from a table in document to the autocorrect library

我正在尝试将 MSWord 2016 文档中的 table 中的格式化条目添加到自动更正库(它像往常一样存储在 normal.dotx 中用于格式化条目)。

在文档中,我有一个包含两列的 table,左列是短文本,右列是自动更正条目的格式化长文本。

我有一个工作宏,用于使用 AutoCorrect.Entries.Add Name:=ShortText, Value:=LongText.
行存储未格式化的文本 我正在尝试修改它以使用 AutoCorrect.Entries.AddRichText ShortText, longtext 函数,该函数然后应该在 table.

中获取字体和斜体属性

我试了两种方法

第一个 - testAddRichText1

这是代码(删除了一些化妆品)

Sub testAddRichText1()
    Set oDoc = ActiveDocument
    For i = 1 To oDoc.Tables(2).Rows.Count
        If oDoc.Tables(2).Rows(i).Cells(1).Range.Characters.Count > 1 Then
            ShortText = oDoc.Tables(2).Cell(Row:=i, Column:=1)
            ShortText = Left(ShortText, Len(ShortText) - 2) 'remove the trailing CR and LF
            longtext = oDoc.Tables(2).Cell(Row:=i, Column:=2)
            StatusBar = "Adding " & ShortText & " = " & longtext.Text
            AutoCorrect.Entries.AddRichText ShortText, longtext
        End If
    Next i
    MsgBox "done"
End Sub

使用此代码,从单元格中提取的文本末尾有许多 unprintable 字符,主要是 Chr(13) 的字符。我尝试 运行 对字符串进行清洁以删除所有非原则 table 字符,但有些东西不会消失,并在更正后的文本末尾出现黑框使用自动更正。我假设它是 table 单元格中的某种秘密代码。试图打印它的 ASC 值 returns 13,但删除它没有任何效果(只是删除了黑框符号之前的字符)。

第二次测试AddRichText2

我尝试在我的工作模型中将斜体添加到我的文本字符串中,然后将其与 AddRichText 方法一起使用。 AddRichText 需要一个范围,但我无法将文本字符串转换为一个范围。

这是代码

Sub testAddRichText2()
    Set oDoc = ActiveDocument
    Dim LongTextrng As Range
    For i = 1 To oDoc.Tables(2).Rows.Count
        If oDoc.Tables(2).Rows(i).Cells(1).Range.Characters.Count > 1 Then
            ShortText = oDoc.Tables(2).Cell(Row:=i, Column:=1)
            ShortText = Left(ShortText, Len(ShortText) - 2)
            longtext = oDoc.Tables(2).Cell(Row:=i, Column:=2).Range
            longtext = Left(longtext, Len(longtext) - 2)
            LongTextrng.Text = longtext 'Fails
            LongTextrng.Italic = True
            StatusBar = "Adding " & ShortText & " = " & longtextrng.Text
                AutoCorrect.Entries.Add Name:=ShortText, Value:=LongTextrng
        End If
    Next i
    MsgBox "done"
End Sub

您的第一个示例 testAddRichText1 几乎是正确的。它失败了,因为尽管您已经认识到需要从 ShortText 中删除尾随字符,但您还没有对 longText 执行相同的操作。

要缩短范围,您可以使用 MoveEnd 方法移动范围的末尾。在这种情况下,您需要将范围的末尾向后移动一个字符以删除单元格末尾标记。

在您的第二个示例 testAddRichText2 中,代码失败是因为您没有正确地将范围分配给变量 LongTextrng。为对象变量赋值时,需要使用 Set 命令,如下所示:

Set objVar = object

这在您的第一次尝试中并没有失败,因为 LongText 尚未声明,因此被假定为变体。

以下代码适合您:

Sub AddRichTextAutoCorrectEntries()
    Dim LongText                    As Range
    Dim oRow                        As Row
    Dim ShortText                   As String

    For Each oRow In ActiveDocument.Tables(2).Rows
        If oRow.Cells(1).Range.Characters.Count > 1 Then
            ShortText = oRow.Cells(1).Range.Text
            ShortText = Left(ShortText, Len(ShortText) - 2)
            'assign the range to the variable
            Set LongText = oRow.Cells(2).Range
            'move the end of the range back by 1 character
            LongText.MoveEnd wdCharacter, -1
            StatusBar = "Adding " & ShortText & " = " & LongText.Text
            AutoCorrect.Entries.AddRichText Name:=ShortText, Range:=LongText
        End If
    Next oRow
End Sub