使用 VBA 获取 Word 中的所有交叉引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24951526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:58:27  来源:igfitidea点击:

Get all cross references in word with VBA

vbams-wordword-vba

提问by kafman

I have quite a large word document (> 400 pages) with lots of cross references to headings. So far, I have always referred to the title of the heading, but now I would like to change that and refer to the page the heading resides on.

我有一个相当大的 Word 文档(> 400 页),其中包含大量对标题的交叉引用。到目前为止,我一直引用标题的标题,但现在我想改变它并引用标题所在的页面。

I didn't find a solution to this via the GUI (except manual treatment, of course), so I was looking into writing some VBA. Unfortunately, I have only found a way to list all targets that can be cross referenced (via GetCrossReferenceItems), but I need a way to access the actual cross reference field.

我没有通过 GUI 找到解决方案(当然,手动处理除外),所以我正在考虑编写一些 VBA。不幸的是,我只找到了一种方法来列出可以交叉引用的所有目标(通过GetCrossReferenceItems),但我需要一种方法来访问实际的交叉引用字段。

Can you help me with that? Is a cross reference field the same as a hyperlink?

你能帮我解决这个问题吗?交叉引用字段是否与超链接相同?

回答by Christina

Cross-references are fields in a Word document, and can be accessed via the Fields collection (ActiveDocument.Fields). You can loop through them like any other collection and check their types to see if it's one you want to work on. It looks like cross-references to text are type 3 (wdFieldRef) and cross-references to page numbers are type 37 (wdFieldPageRef). Changing fields can be a little tricky; the following should get you started:

交叉引用是 Word 文档中的字段,可以通过 Fields 集合 (ActiveDocument.Fields) 访问。您可以像任何其他集合一样循环遍历它们并检查它们的类型以查看它是否是您想要处理的类型。看起来对文本的交叉引用是类型 3 (wdFieldRef),对页码的交叉引用是类型 37 (wdFieldPageRef)。更改字段可能有点棘手;以下应该让你开始:

Sub ChangeFields()
    Dim objDoc As Document
    Dim objFld As Field
    Dim sFldStr As String
    Dim i As Long, lFldStart As Long

    Set objDoc = ActiveDocument
    ' Loop through fields in the ActiveDocument
    For Each objFld In objDoc.Fields
        ' If the field is a cross-ref, do something to it.
        If objFld.Type = wdFieldRef Then
            'Make sure the code of the field is visible. You could also just toggle this manually before running the macro.
            objFld.ShowCodes = True
            'I hate using Selection here, but it's probably the most straightforward way to do this. Select the field, find its start, and then move the cursor over so that it sits right before the 'R' in REF.
            objFld.Select
            Selection.Collapse wdCollapseStart
            Selection.MoveStartUntil "R"
            'Type 'PAGE' to turn 'REF' into 'PAGEREF'. This turns a text reference into a page number reference.
            Selection.TypeText "PAGE"
            'Update the field so the change is reflected in the document.
            objFld.Update
            objFld.ShowCodes = True
        End If
    Next objFld   
End Sub