如何将样式应用于文档中的所有交叉引用?

huangapple go评论51阅读模式
英文:

How do I apply a style to all cross-references in a document?

问题

我创建了一个超链接样式,将文本变为蓝色和粗体。

我想将这个样式应用到文档中的超链接上。

当我运行宏时,输出显示“未找到超链接”。

英文:

I created a hyperlink style that turns text blue and bold.

I would like to apply the style to the hyperlinks in the document.

When I run the macro the output is "no hyperlinks found."

    Dim objDoc As Document
    Dim objFld As Field

    Set objDoc = ActiveDocument 
    If Selection.Fields.Count >= 1 Then
        For Each objFld In objDoc.Fields 
            If objFld.Type = wdFieldRef Then 
                objFld.ShowCodes = True
                objFld.Select
                Selection.Collapse wdCollapseStart
                Selection.MoveStartUntil "R"
                Selection.Fields(1).Code.Text = Selection.Fields(1).Code.Text & "\*CharFormat"
                Selection.Style = ActiveDocument.Styles("HyperlinkStyle")
                objFld.Update 
                objFld.ShowCodes = True
            End If
        Next objFld
    Else
        MsgBox "No hyperlinks found.", vbInformation, "Select OK"
    End If
End Sub

答案1

得分: 1

以下是您要翻译的内容:

尽管您的交叉引用链接到文档的其他部分,但它们与实际超链接不同。如果它们是默认的超链接和已访问的超链接样式将会应用。

您看到的这个消息,其措辞具有误导性,并不是因为文档中没有超链接,而是因为当前选定的文本中没有字段。您可以通过查看整个文档而不仅仅是选定的文本来避免这种情况。

个人而言,我会将“HyperlinkStyle”重命名为“Cross Reference Text”,以明确样式的目的,除非您打算以这种方式格式化所有超链接,那么只需修改超链接样式并使用它。

您还可以通过直接使用 objFld 来避免完全更改选择:

子格式化交叉引用()
Dim objDoc As Document
Dim objFld As Field

Set objDoc = ActiveDocument
If objDoc.Fields.Count >= 1 Then
    For Each objFld In objDoc.Fields
        With objFld
            If .Type = wdFieldRef Then
                If Not Right(.Code.Text, 13) = "\*CharFormat " Then .Code.Text = .Code.Text & "\*CharFormat "
                .Code.Characters.First.Style = objDoc.Styles("Cross Reference Text")
                .Result.Style = objDoc.Styles("Cross Reference Text")
            End If
        End With
    Next objFld
Else
    MsgBox "No cross references found.", vbInformation, "Select OK"
End If

End Sub

英文:

Although your cross references link to other parts of the document they are not treated the same as actual hyperlinks. If they were the default Hyperlink and FollowedHyperlink styles would be applied.

You are seeing that message, the wording of which is misleading, not because there are no hyperlinks in the document, but because there are no fields in the currently selected text. You can avoid this by looking at the document as a whole rather than just the selected text.

Personally, I would rename "HyperlinkStyle" to "Cross Reference Text" to make the purpose of the style clear, unless you intend all hyperlinks to be formatted that way in which case just modify the Hyperlink style and use that.

You can also avoid changing the selection at all by working directly with objFld:

Sub FormatCrossReferences()
Dim objDoc As Document
Dim objFld As Field

Set objDoc = ActiveDocument
If objDoc.Fields.Count >= 1 Then
    For Each objFld In objDoc.Fields
        With objFld
            If .Type = wdFieldRef Then
                If Not Right(.Code.Text, 13) = "\*CharFormat " Then .Code.Text = .Code.Text & "\*CharFormat "
                .Code.Characters.First.Style = objDoc.Styles("Cross Reference Text")
                .Result.Style = objDoc.Styles("Cross Reference Text")
            End If
        End With
    Next objFld
Else
    MsgBox "No cross references found.", vbInformation, "Select OK"
End If

End Sub

huangapple
  • 本文由 发表于 2023年5月26日 00:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定