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


32

无需添加任何内容,我想立即更改Word 2007文档中所有交叉引用的样式。但是我不知道该怎么做。如何才能做到这一点?


好问题,我找不到任何快速而肮脏的东西。但我认为这与整个文档的样式有关
Ivo Flipse'2009年

1
@Ivo,是的,我也认为这与文档的样式有关,但是我是一位可怜的软件开发人员,对Word经验不多:)
Drake

@marco:请参阅我的最新答案
已暂停,直至另行通知。

是的,非常好的答案非常感谢,非常非常有趣
Drake

Answers:


38

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

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

  • 选择文字
  • 选择功能区中的“主页”选项卡
  • 使用功能区“样式”组中的上下按钮或下拉按钮,选择“强烈参考”样式(或根据需要选择其他样式)

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

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

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

  • 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

实际上,大多数引用(如果不是全部)都是默认情况下使用普通文本创建的。谢谢您的回答,但是在这种情况下,我必须手动设置每个样式,然后才能一次更新所有样式。我在寻找一种可以使过程的第一步自动化的方法
Drake

6
哇,我不知道查找/替换的这种高级用法,非常有帮助
Drake

6
您的回答是浓缩知识的瑰宝。值得拥有自己的Wiki!
绝望的鬼脸

5
耶稣基督...这很好用,但问题是:为什么很难将样式应用于交叉引用!?;)
Leniel Maccaferri 2014年

在Mac上的MS Word 16.9.1上,此方法有效(未尝试使用宏),但是查找/替换不在主功能区上(我认为呢?),而是在“编辑”->“查找”->“高级”下的菜单中查找并替换...
迈克尔

5
  • 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

这是我第一次使用宏来加快工作速度。感谢半机械人提供的有用的宏。


0

快速有效的方法:

  1. 选择一些您想要用于参考的格式的文本。
  2. 选择功能区中的“ 主页”选项卡。
  3. 右键单击Normal样式,然后选择Update Normal以匹配selection
  4. 与更新引用Ctrl+ AF9

0

此宏将打开“交叉引用”对话框,以在当前光标位置插入一个交叉引用。

当您在插入引用后关闭“外部参照”对话框时,宏会继续将插入的交叉引用格式化为上标。

Sub XrefSuper()
'
' This opens the Cross Reference dialogue box to insert a cross reference at the current cursor position.
'   When the dialogue box is closed after inserting the reference the macro resumes to format the inserted cross reference to superscript.
'
'
Dim wc As Integer
    wc = ActiveDocument.Characters.Count

Dim dlg As Dialog
    Set dlg = Dialogs(wdDialogInsertCrossReference)
        dlg.Show 'Open dialogue and perform the insertion from the dialog box) 
                 'Macro continues after closing CrossRef dialogue box

    If wc = ActiveDocument.Characters.Count Then Exit Sub   'If we failed to insert something then exit

    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Font.Superscript = wdToggle
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Font.Superscript = wdToggle
End Sub

感谢ExpertsExchange的Graham Skan提供了如何打开外部参照对话框的信息。


0

将上面的答案与另一个功能结合起来,循环浏览文档“故事”,以将样式应用于文档正文,页眉,页脚和形状上的文本。

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

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.