【问题标题】:MS Word - link to header of current section / relative referencingMS Word - 链接到当前部分的标题/相对引用
【发布时间】:2021-04-21 17:10:58
【问题描述】:

我正在处理一个包含多级标题的大型文档,用于一系列测试程序。在每个程序结束时,都有一个签收框。我想在每个签核框中添加部分编号和名称。

我研究了(当然是在这里!)并编写了一个宏来插入对当前部分标题的引用,它似乎工作正常:

Sub InsertCrossRefToSectionHeading()

    Dim RefList As Variant
    Dim LookUp As String
    Dim Ref As String
    Dim i As Integer

    
    LookUp = ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListString

    With ActiveDocument
        RefList = .GetCrossReferenceItems(wdRefTypeHeading)
        For i = UBound(RefList) To 1 Step -1
            Ref = Trim(RefList(i))
            If Left(Ref, Len(LookUp)) = LookUp Then Exit For
        Next i

        If i Then
            Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading, _
                                           ReferenceKind:=wdNumberFullContext, _
                                           ReferenceItem:=CStr(i), _
                                           InsertAsHyperlink:=True, _
                                           IncludePosition:=False, _
                                           SeparateNumbers:=False, _
                                           SeparatorString:=" "
                                           
            Selection.TypeText Text:=vbTab
            
            Selection.InsertCrossReference ReferenceType:=wdRefTypeHeading, _
                                           ReferenceKind:=wdContentText, _
                                           ReferenceItem:=CStr(i), _
                                           InsertAsHyperlink:=True, _
                                           IncludePosition:=False, _
                                           SeparateNumbers:=False, _
                                           SeparatorString:=" "
                                           
            'Copy the formatting from the previous cell in the template table and apply to refrence text.
            Selection.MoveLeft Unit:=wdCell
            Selection.CopyFormat
            Selection.MoveRight Unit:=wdCell
            Selection.PasteFormat
                                           
        Else
            MsgBox "A cross reference to """ & LookUp & """ couldn't be set" & vbCr & _
                   "because a paragraph with that number couldn't" & vbCr & _
                   "be found in the document.", _
                   vbInformation, "Invalid cross reference"
        End If
    End With
End Sub

但是,它是对在运行宏时作为本节标题的标题的硬链接引用。如果我添加一个部分,则该部分之后的签字框中的所有引用都指向上一个部分,这违背了它的目的。

是的,我的宏将使它更快地修复,但每次我在这个不断发展的文档中添加一个部分时都会有很多返工。是的,我什至可以以编程方式在文档中搜索第一行带有“测试记录”的表格,然后删除第 2 行单元格 2 并插入引用......但这是很多额外的编程!......我已经在 Excel 中完成了大量的 VBA,但在 Word 中是新手。我想我可以将参考文献留到本次编辑的最后......但是如果文档在未来发展(这很可能),我将不得不再次这样做。

有没有办法引用当前部分的标题?

感谢您的帮助!

【问题讨论】:

  • 请注意,“header”和“heading”指的是两个完全不同的东西。这些术语不可互换。 “部分”在 Word 中也有特殊含义 - 您是否在问题的正确上下文中使用它?鉴于我们没有文档,正确使用术语有助于我们理解您的问题。
  • 我认为如果您通过 ActiveDocument.Fields.Update 更新交叉引用,您会发现引用实际上仍然指向正确的标题。
  • 前两个 cmets 是有根据的,macropod 可能会回答您的问题。如果没有,请考虑改用 StyleRef 字段。这是我在该领域的文章的链接:addbalance.com/usersguide/fields.htm#STYLEREF
  • @TimothyRylatt - 是的,我对术语有点懒惰。我指的是标题,即 1 标题级别 1 ... 1.1 标题级别 2 ..... 问题已更新,谢谢。
  • 谢谢@CharlesKenyon,看起来可行!我正在使用 { STYLEREF "Heading 5" \n \w * MERGEFORMAT } { STYLEREF "Heading 4" * MERGEFORMAT } 来获取标题编号和文本。我必须根据文档的级别来更新级别,但这很容易。现在我要再次查看宏并让它为我读取关卡..

标签: vba ms-word reference heading


【解决方案1】:

感谢@CharlesKenyon,这是解决方案(也比我的第一次尝试简单得多!)。

Sub InsertCrossRefToSectionHeading()
' Adds ref to the heading of the current section.
'https://stackoverflow.com/questions/67200486/ms-word-link-to-header-of-current-section-relative-referencing
'http://www.addbalance.com/usersguide/fields.htm#STYLEREF

    CurrentHeadingLevel = ActiveDocument.Bookmarks("\HeadingLevel").Range.Paragraphs(1).Range.ListFormat.ListLevelNumber

    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "STYLEREF  ""Heading " & CurrentHeadingLevel & """ \w ", PreserveFormatting:=True
    Selection.TypeText Text:=" "
    Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
        "STYLEREF  ""Heading " & CurrentHeadingLevel & """ ", PreserveFormatting:=True
    
    'Copy the formatting from the previous cell in the template table and apply to refrence text.
    Selection.MoveLeft Unit:=wdCell
    Selection.CopyFormat
    Selection.MoveRight Unit:=wdCell
    Selection.PasteFormat
End Sub

【讨论】:

猜你喜欢
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-28
  • 1970-01-01
  • 2021-09-07
  • 2010-12-12
  • 1970-01-01
相关资源
最近更新 更多