Nada mais a acrescentar, gostaria de alterar o estilo de todos referências cruzadas que tenho em um documento do Word 2007 de uma só vez. Mas não tenho ideia de como fazê-lo. Como isso pode ser feito?
Alguns tipos de referência cruzada são formatados automaticamente com o estilo "referência intensa", mas a maioria é formatada como texto "normal".
Para aplicar o estilo "referência intensa" ao texto da referência cruzada:
Para alterar a aparência de todo o texto de um determinado estilo:
Para aplicar um estilo a todas as referências cruzadas de uma vez:
^19 REF
Consulte esta página para obter mais informações sobre códigos especiais em Localizar e substituir.
Aqui está uma macro que adicionará a opção \* mergeformat
a cada um dos campos. Essa opção é necessária para evitar que a formatação seja perdida se você fizer uma atualização de campo. Você pode atribuir a macro a um pressionamento de tecla e ela percorrerá os campos um de cada vez para cada vez que pressionar o pressionamento de tecla. Você também pode editar a macro para percorrer todo o documento para automatizar o processo.
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
Use a macro a seguir para adicionar CHARFORMAT a todas as referências cruzadas. Essa macro adiciona a sequência ao campo apenas se ainda não estiver lá.
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
Use esta macro para formatar todas as referências cruzadas com o estilo "Referência sutil" (verifique se você possui esse estilo e se os códigos de campo são mostrados):
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
Pressione Alt+F9 ocultar códigos de campo
ao editar a macro carregada pelo cyborg, podemos automatizar facilmente a exibição e ocultação dos códigos de campo. para que, toda vez que desejamos atualizar, não tenhamos que alternar códigos de campo. Eu usei o seguinte código para adicionar alternância de código de campo.
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
A macro completa é a seguinte:
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
É a primeira vez que uso macros para acelerar meu trabalho no Word. Obrigado cyborg por essa macro útil.
Combinando as respostas acima com outra função para percorrer as 'histórias' do documento, aplicar estilo no corpo do documento, cabeçalhos, rodapés e texto nas formas.
Simplesmente chame a macro SetCrossRefStyle () abaixo para aplicar o estilo "Intense Reference" a todas as referências cruzadas.
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
Maneira rápida e eficaz:
Essa macro abre a caixa de diálogo Referência cruzada para inserir uma referência cruzada na posição atual do cursor.
Quando você fecha a caixa de diálogo Refex após inserir a referência, a macro continua para formatar a referência cruzada inserida em sobrescrito.
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
Agradecemos a Graham Skan no ExpertsExchange por como abrir o diálogo Xref.