Steps to link a figure number to a reference in Microsoft Word:
- Insert the figure and add a caption with the figure number by selecting the figure, going to the "References" tab, clicking "Insert Caption", and adding the caption text.
- Place the cursor where you want to insert the cross-reference in the text.
- Go to the "References" tab and click "Cross-reference".
- In the "Reference type" dropdown, select "Figure".
- In the "Insert reference to" dropdown, select "Only label and number".
- In the "For which caption" dropdown, select the caption that corresponds to the figure you want to reference.
- Click "Insert" to add the cross-reference to your document.
To update the cross-reference if the figure number changes, simply right-click on the cross-reference and select "Update Field".
PHP Code:
Sub Example()
'This example inserts a caption and a cross-reference to a figure in a Word document.
Dim objFigure As Word.Shape
Dim objCaption As Word.CaptionLabel
Dim objRange As Word.Range
Dim objRef As Word.Field
'Insert the figure and add a caption
Set objFigure = ActiveDocument.Shapes.AddPicture("C:\Users\Public\Pictures\Sample Pictures\Desert.jpg", False, True, 0, 0, -1, -1)
Set objCaption = ActiveDocument.CaptionLabels.Add(Name:="Figure")
objFigure.Select
Selection.InsertCaption Label:=objCaption.Name, TitleAutoText:="", Title:="", Position:=wdCaptionPositionBelow, ExcludeLabel:=0
'Insert the cross-reference
Set objRange = ActiveDocument.Content
objRange.Collapse Direction:=wdCollapseEnd
objRange.InsertParagraphAfter
objRange.Collapse Direction:=wdCollapseEnd
Set objRef = objRange.Fields.Add(Range:=objRange, Type:=wdFieldRef, Text:="Figure \ref Figure1", PreserveFormatting:=False)
End Sub