【问题标题】:Cannot use InsertCrossReference (results in a Runtime Error 4198)无法使用 InsertCrossReference(导致运行时错误 4198)
【发布时间】:2019-10-24 08:40:42
【问题描述】:

我正在遍历一组字符串(看起来像“图 1”),在 Word 文档中查找这些字符串的每次出现,并尝试在每个字符串上插入对标题的交叉引用(我的标题如下这种模式“图 1 [00:01:20]”)。由于某种原因,它在包含“InsertCrossReference”方法的行上完全失败(抛出运行时错误 4198 并显示“命令失败”消息)。到目前为止,这是我的代码的一部分:

For Each Match In AllMatches 'for each word in my AllMatches collection
    Set rngFind = ActiveDocument.Content
    With rngFind.Find
        .Forward = True
        .ClearFormatting
        .Text = Match.value
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .Wrap = wdFindStop
        Do While .Execute
            rngFind.InsertCrossReference ReferenceType:="Figure", _
                ReferenceKind:=wdOnlyLabelAndNumber, _
                ReferenceItem:=Match.value, _ 'Match.value looks like "Figure 1 [00:01:20]" (like in the picture below)
                InsertAsHyperlink:=True, _
                IncludePosition:=False, _
                SeparateNumbers:=False, _
                SeparatorString:=" "
        Loop
    End With
Next Match

我尝试调查并发现“UBound(ActiveDocument.GetCrossReferenceItems(ReferenceType:="Figure"))”返回“0”(当我使用“wdCaptionFigure”作为 ReferenceType 时也是如此)。

当我手动尝试插入交叉引用时,一切正常(见下图)...

当我记录手动插入并尝试运行word生成的代码时,它失败了!

问题可能是由于之前使用 VBA 在形状而不是 inlineshapes 上插入了字幕(使用循环和“.InsertCaption Label:=wdCaptionFigure”等)这一事实引起的吗?

---- 编辑----

以下是触发错误的一些步骤,并且似乎确认在形状上插入标题而不是在 inlinshapes 上会导致稍后出现交叉引用错误。

  1. 创建一个空白word文档(我使用的是Word 2016)
  2. 插入图片
  3. 将图片格式环绕文本选项从“与文本对齐”(=inlineshape) 更改为其他任何内容(例如“通过”)
  4. 右击图片并插入标题(参考类型:“图”,其中标题:“图1”)
  5. 将图片格式改回“符合文字”
  6. 在您的文档中插入一些文本(例如“我是一个测试”)
  7. 选择“I am a test”并运行以下宏:

    子宏1() Selection.InsertCrossReference ReferenceType:="Figure", ReferenceKind:=wdEntireCaption, ReferenceItem:="1", InsertAsHyperlink:=True, IncludePosition:=False,separateNumbers:=False, SeparatorString:="" 结束子

这会触发 4198 错误。如果省略第 3 步和第 5 步,则不会出错。我完全坚持这个。如果你们中的任何一位能帮助我,我将非常高兴。

【问题讨论】:

  • 什么是错误信息 - 没有人记住数字,这条信息会有所帮助。您能否请edit 包含信息以复制代码正在运行的文档?足以进行测试(minimal reproducible example)。一些文档内容的屏幕截图也可以提供帮助。你是说Find.Execute 工作正常,对吗?
  • @CindyMeister 我更新了我的帖子。

标签: vba macos ms-word


【解决方案1】:

我遇到了类似的问题。我通过创建一个数组来解决它,将范围保存到 .Execute 循环中的数组中,然后在 find 完成后循环遍历数组以插入交叉引用。这是一个例子:

Dim docRange As Range
Set docRange = ActiveDocument.Content
Dim terms() As Range, X As Long
X = 0
With docRange.Find
...
    While .Execute
        docRange.Select
            If .found Then
                ReDim Preserve terms(X)
                Set terms(X) = Selection.Range
                X = X + 1
            End If
        Selection.Collapse
    Wend
End With
Dim i As Integer
For i = 0 To UBound(terms)
    terms(i).InsertCrossReference _
                ReferenceType:=wdRefTypeBookmark, _
                ReferenceKind:=wdContentText, _
                InsertAsHyperlink:=True, _
                ReferenceItem:=mark

Next i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2019-02-06
    • 2019-05-31
    相关资源
    最近更新 更多