无需添加任何内容,我想立即更改Word 2007文档中所有交叉引用的样式。但是我不知道该怎么做。如何才能做到这一点?
无需添加任何内容,我想立即更改Word 2007文档中所有交叉引用的样式。但是我不知道该怎么做。如何才能做到这一点?
Answers:
一些交叉引用类型会自动以“强烈引用”样式设置格式,但大多数会被设置为“普通”文本格式。
要将“强引用”样式应用于交叉引用的文本:
更改给定样式的所有文本的外观:
一次将样式应用于所有交叉引用:
^19 REF
有关查找和替换中特殊代码的更多信息,请参见此页面。
这是一个将开关添加\* 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
使用以下宏将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隐藏域代码
编辑由电子人上传的宏,我们可以轻松地自动显示和隐藏域代码。这样,每次我们希望更新时,我们都不必使用切换字段代码。我使用以下代码添加了域代码切换。
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
这是我第一次使用宏来加快工作速度。感谢半机械人提供的有用的宏。
此宏将打开“交叉引用”对话框,以在当前光标位置插入一个交叉引用。
当您在插入引用后关闭“外部参照”对话框时,宏会继续将插入的交叉引用格式化为上标。
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提供了如何打开外部参照对话框的信息。
将上面的答案与另一个功能结合起来,循环浏览文档“故事”,以将样式应用于文档正文,页眉,页脚和形状上的文本。
只需调用下面的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