【问题标题】:Add formatted entries from a table in document to the autocorrect library将文档中表格中的格式化条目添加到自动更正库
【发布时间】:2016-09-30 08:15:08
【问题描述】:

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

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

我有一个工作宏,用于使用 AutoCorrect.Entries.Add Name:=ShortText, Value:=LongText 行存储未格式化的文本。
我正在尝试修改它以使用 AutoCorrect.Entries.AddRichText ShortText, longtext 函数,然后该函数应该会在表格中获取字体和斜体属性。

我尝试了两种方法。

FIRST - 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

使用此代码,从单元格中提取的文本末尾有许多不可打印的字符,主要是 Chr(13) 的。我尝试在字符串上运行清洁器以删除所有不可打印的字符,但是当使用自动更正时,那里有些东西不会消失,并在更正文本的末尾导致一个黑框。我认为这是表格单元格中的某种密码。尝试打印它的 ASC 值返回 13,但删除它没有任何效果(只是删除黑盒符号之前的字符)。

第二次 testAddRichText2

我尝试在我的工作模型中将斜体添加到我的文本字符串中,然后将它与 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

【问题讨论】:

  • 您正在重新发明轮子,因为有两个免费的 MVP 产品可以做到这一点。 Jay Freedman 的 AutoCorrect2007.zip jay-freedman.info 和 Greg Maxey 的 AutoCorrect Backup Utility Manager。 gregmaxey.com/word_tip_pages/autocorrect_utility_manager.html ---- 除非有理由不使用其中之一,否则我会使用它们而不是尝试编写自己的代码。杰的代码没有隐藏。
  • 这些实用程序已在数千个系统上使用。
  • 谢谢查尔斯。 2016 年,当我努力让它发挥作用时,本可以做到这一点:-)
  • 不过,看看 Jay 实用程序中的代码。它可能会有所帮助。他已经在这方面工作了超过 15 年,并从 Microsoft 代码开始。我相信他有成千上万的用户,并且已经对其进行了多次调整以确保它在所有系统上都能正常工作。

标签: vba ms-word autocorrect


【解决方案1】:

您的第一个示例 testAddRichText1 几乎是正确的。它失败了,因为虽然您已经认识到需要从 ShortText 中删除尾随字符,但您没有为 longText 做同样的事情。

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

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

Set objVar = object

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

下面的代码对你有用:

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

【讨论】:

  • 谢谢蒂姆。这太棒了,而且效果很好!感谢您的解释和更优雅的编码!干杯...史蒂夫
猜你喜欢
  • 2017-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多