PDA

View Full Version : [SOLVED:] Reference formatting with a Macro



sharens1
09-23-2017, 10:03 AM
I have am trying to automate the reference insertion. I have a macro that does that, but I basically want the figure insertion to be the same style as where it is being inserted, and I would like it to be bold...Also, I only want the those characters to be bold, so if I start typing again, it will revert back to the font that was used previously.

What do I need to add to make that a reality?

Sub InsertFigureReference()
With Application.Dialogs(wdDialogInsertCrossReference)
.ReferenceType = "Figure"
.ReferenceKind = wdOnlyLabelAndNumber
.InsertAsHyperlink = True
.Show
End With
End Sub


Many thanks in advance.

macropod
09-23-2017, 11:34 PM
Try:

Sub InsertFigureReference()
Application.ScreenUpdating = False
ActiveWindow.View.ShowFieldCodes = True
Dim Rng As Range: Set Rng = Selection.Range
With Application.Dialogs(wdDialogInsertCrossReference)
.ReferenceType = "Figure"
.ReferenceKind = wdOnlyLabelAndNumber
.InsertAsHyperlink = True
.Show
End With
With Rng
.MoveEnd wdWord, 1
If .Fields.Count = 0 Then
Undo
GoTo ErrExit
End If
With .Fields(1)
With .Code
.Text = Trim(Split(.Text, "\h")(0) & "\* Charformat \h")
.Characters.First.Style = "Strong"
End With
.Update
End With
.Collapse wdCollapseEnd
End With
ErrExit:
Set Rng = Nothing
ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub

sharens1
09-28-2017, 05:31 PM
Thank you very much...

sharens1
09-28-2017, 06:00 PM
I was unable to get the macro to work with the following statement:
If .Fields.Count = 0 Then
Undo
GoTo ErrExit
End If

However, if you remove the bolded "undo", it works fine..

Thanks again