是否可以为 Word 2007 中的所有交叉引用分配特定样式?

是否可以为 Word 2007 中的所有交叉引用分配特定样式?

没什么可补充的,我想改变所有的风格交叉引用我有一个 Word 2007 文档。但我不知道该怎么做。如何做到这一点?

答案1

一些交叉引用类型会自动格式化为“强烈引用”样式,但大多数会被格式化为“普通”文本。

要将“强烈引用”样式应用于交叉引用的文本:

  • 选择文本
  • 选择功能区中的“主页”选项卡
  • 使用功能区“样式”组中的上下按钮或下拉按钮,选择“强烈引用”样式(或其他您喜欢的样式)

要更改给定样式的所有文本的外观:

  • 选择功能区中的“主页”选项卡
  • 使用功能区“样式”组中的下拉按钮,选择“应用样式...”
  • 在“应用样式”对话框中的“样式名称”下,选择要更改的样式的名称(例如“强烈引用”)
  • 点击“修改…”按钮
  • 更改格式以适合您,然后单击“确定”

要一次性将样式应用于所有交叉引用:

  • Alt+F9显示字段代码
  • 选择功能区中的“主页”选项卡
  • 点击“编辑”组中的“替换”
  • 在“查找内容”字段中,输入^19 REF
    • (这是插入符号-一-九-空格-REF)
  • 点击“替换为”字段,但不要输入任何内容
  • 点击“更多”按钮
  • 对话框底部的标题应为“替换”(后面有一条水平线)
  • 单击“格式”按钮并选择“样式...”
  • 选择一种风格(例如“强烈引用”)并单击“确定”
  • 现在应该显示您在“替换为”字段下选择的样式
  • 如果您有足够的勇气,请单击“全部替换”,或者使用“查找下一个”和“替换”逐一替换或跳过每个参考字段代码的样式
  • Alt+F9隐藏字段代码

这一页有关“查找和替换”中的特殊代码的更多信息。

这是一个宏,它将向每个字段添加开关\* mergeformat。如果您进行字段更新,此开关是必需的,以防止格式丢失。您可以将宏分配给按键,每次按下按键时,它都会逐个遍历字段。您还可以编辑宏以循环遍历整个文档以自动化该过程。

Sub mf()
'
' mf Macro
' Find cross references and add \* mergeformat
'
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "^19 REF"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="\* mergeformat "
    Selection.Find.Execute
End Sub

答案2

  • Alt+F9显示字段代码
  • 使用以下宏将 CHARFORMAT 添加到所有交叉引用。此宏仅在字符串尚未存在时才将其添加到字段。

    Sub SetCHARFORMAT()
    '
    ' Set CHARFORMAT switch to all {REF} fields. Replace MERGEFORMAT.
    '
    '
        Dim oField As Field
        Dim oRng As Range
        For Each oField In ActiveDocument.Fields
        'For Each oField In Selection.Fields
            If InStr(1, oField.Code, "REF ") = 2 Then
                If InStr(1, oField.Code, "MERGEFORMAT") <> 0 Then
                    oField.Code.Text = Replace(oField.Code.Text, "MERGEFORMAT", "CHARFORMAT")
                End If
                If InStr(1, oField.Code, "CHARFORMAT") = 0 Then
                    oField.Code.Text = oField.Code.Text + "\* CHARFORMAT "
                End If
            End If
        Next oField
    
    
    End Sub
    
  • 使用此宏将所有交叉引用格式化为“细微引用”样式(确保您具有这样的样式,并且显示字段代码):

    Sub SetCrossRefStyle()
    '
    ' Macro to set styole of all cross references to "Subtle Reference"
    '
    '
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Style = ActiveDocument.Styles( _
            "Subtle Reference")
        With Selection.Find
            .Text = "^19 REF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    
  • Alt+F9隐藏字段代码

答案3

通过编辑机器人上传的宏,我们可以轻松地自动显示和隐藏字段代码。这样,每次我们想要更新时,就不必使用切换字段代码。我使用以下代码来添加字段代码切换。

ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

完整的宏如下:

Sub SetCrossRefStyle()
'
' Macro to set styole of all cross references to "Subtle Reference"
'
'
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Style = ActiveDocument.Styles( _
    "Subtle Reference")
With Selection.Find
    .Text = "^19 REF"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

这是我第一次使用宏来加快我在 Word 中的工作速度。感谢 cyborg 提供如此有用的宏。

答案4

将上述答案与另一个函数结合起来,循环遍历文档“故事”,对文档正文、页眉、页脚和形状上的文本应用样式。

只需调用下面的 SetCrossRefStyle() 宏即可将“强烈引用”样式应用于所有交叉引用。

Sub m_SetCHARFORMAT(textRanges As Collection)
' https://superuser.com/questions/13531/is-it-possible-to-assign-a-specific-style-to-all-cross-references-in-word-2007
'
' Set CHARFORMAT switch to all {REF} fields. Replace MERGEFORMAT.
' Requires ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
'
    Dim oField As Field
    Dim oRng As Range
    For Each oRng In textRanges
        For Each oField In oRng.Fields
            If InStr(1, oField.Code, "REF ") = 2 Then
                If InStr(1, oField.Code, "MERGEFORMAT") <> 0 Then
                    oField.Code.Text = Replace(oField.Code.Text, "MERGEFORMAT", "CHARFORMAT")
                End If
                If InStr(1, oField.Code, "CHARFORMAT") = 0 Then
                    oField.Code.Text = oField.Code.Text + "\* CHARFORMAT "
                End If
            End If
        Next oField
    Next oRng
End Sub


Sub m_AddCrossRefStyle(textRanges As Collection)
' https://superuser.com/questions/13531/is-it-possible-to-assign-a-specific-style-to-all-cross-references-in-word-2007
'
' Macro to set style of all cross references to "Intense Reference"
' Requires ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
'
    For Each oRng In textRanges
        oRng.Find.ClearFormatting
        oRng.Find.Replacement.ClearFormatting
        oRng.Find.Replacement.Style = ActiveDocument.Styles("Intense Reference")
        With oRng.Find
            .Text = "^19 REF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        oRng.Find.Execute Replace:=wdReplaceAll
    Next oRng
End Sub


Function m_GetAllTextRanges() As Collection
' https://wordmvp.com/FAQs/Customization/ReplaceAnywhere.htm
' https://www.mrexcel.com/forum/excel-questions/443052-returning-collection-function.html
'
' Get text ranges in all document parts.
'
    Set m_GetAllTextRanges = New Collection
    For Each rngStory In ActiveDocument.StoryRanges
        'Iterate through all linked stories
        Do
            m_GetAllTextRanges.Add rngStory
            On Error Resume Next
            Select Case rngStory.StoryType
                Case 6, 7, 8, 9, 10, 11
                If rngStory.ShapeRange.Count > 0 Then
                    For Each oShp In rngStory.ShapeRange
                        If oShp.TextFrame.HasText Then
                            m_GetAllTextRanges.Add oShp.TextFrame.TextRange
                        End If
                    Next
                End If
                Case Else
                    'Do Nothing
            End Select
            On Error GoTo 0
            'Get next linked story (if any)
            Set rngStory = rngStory.NextStoryRange
        Loop Until rngStory Is Nothing
    Next
End Function

Sub SetCrossRefStyle()
'
' 1. Get all text ranges since Selection.Find only works on document body, but not on headers/footers
' 2. Add CHARFORMAT to make styling persistent
' 3. Add styling to all references
'
    Dim textRanges As Collection
    Set textRanges = m_GetAllTextRanges
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
    m_SetCHARFORMAT textRanges
    m_AddCrossRefStyle textRanges
    ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

相关内容