'Word VBA: How to create cross reference to figure in text box?
I'm creating a Word VBA macro to insert a text frame containing an image and its caption, and then create a cross reference to it in the main document text.
But Word doesn't find the caption if it's in a text frame.
Here's illustrative code:
Sub ShowMe()
Dim items() As String
items = ActiveDocument.GetCrossReferenceItems("Figure")
MsgBox ("Found " & UBound(items))
End Sub
If the caption is of a thing (say, a picture, table, or just a para) that's inline in the main document text it finds it. But if the caption is in a text frame or a text box, whether it's a caption para that I create within the text frame, or if I simply click the text frame and do 'Insert caption' (which actually creates a text box below the frame), I get zero.
I've tried creating the cross reference just by building the REF field manually, but it doesn't work reliably. I suspect there's some magic about the cross reference process creating a new hidden bookmark when required.
Any suggestions, please?
Solution 1:[1]
The solution I found was to move the caption paragraph out of the frame to the end of the main document using cut and paste, create the cross reference to it, then move it back again.
The code to do it is rather long, mainly since Word tends to be a bit inconsistent in cutting paragraphs. You can find it at https://github.com/charlesweir/WordImagesAndTables in AnchoredFrame.cls, function InsertCrossReferenceBesideAnchor
- Charles
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | oberlies |