add a cross ref to a caption in VBA

User avatar
stuck
Panoramic Lounger
Posts: 8173
Joined: 25 Jan 2010, 09:09
Location: retirement

add a cross ref to a caption in VBA

Post by stuck »

As a minimum, adding a caption using VBA requires:

Code: Select all

Selection.InsertCaption Label:="Table"
so far so simple. To extend that to include a title is also simple:

Code: Select all

Selection.InsertCaption Label:="Table", Title:=":   my Title Here"
But how do I include a cross ref field (that points to a bookmark) within the title?

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: add a cross ref to a caption in VBA

Post by macropod »

For example:

Code: Select all

With Selection
  .InsertCaption Label:="Table"
  .Collapse wdCollapseEnd
  .InsertBefore " "
  .Collapse wdCollapseEnd
  .InsertCrossReference ReferenceType:="Heading", ReferenceKind:=wdContentText, ReferenceItem:="1", _
    InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
End With
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8173
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: add a cross ref to a caption in VBA

Post by stuck »

Ah, yes, 'With Selection. etc.', of course :stupidme:

Next question, closely related. Is it possible to ad the \* Charformat switch to the cross ref field, i.e. end up with a cross ref that looks like:

Code: Select all

{ REF myBookmarkName \h  \* Charformat }
Thanks again,

Ken

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: add a cross ref to a caption in VBA

Post by macropod »

For that, given that you seem to know the bookmark's name, you might replace:

Code: Select all

.InsertCrossReference ReferenceType:="Heading", ReferenceKind:=wdContentText, ReferenceItem:="1", _
    InsertAsHyperlink:=True, IncludePosition:=False, SeparateNumbers:=False, SeparatorString:=" "
with:

Code: Select all

.Fields.Add .Range, wdFieldEmpty, "REF myBookmarkName \h  \* Charformat", False
Unless you're changing the formatting to something other than that of the Caption Style, you don't need the \* Charformat switch but, if you do, you'll then want to format the 'R' of 'REF'. For example:

Code: Select all

  .Start = .Start - 1
  .Fields(1).Code.Characters.First.Next.Font.Italic = True
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8173
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: add a cross ref to a caption in VBA

Post by stuck »

Thank you again, I'm nearly there. The construct that works for me is:

Code: Select all

    With Selection
        .InsertCaption Label:="Table", Title:=":" & vbTab & "my text here"
        .Fields.Add _
            Range:=Selection.Range, _
            Type:=wdFieldRef, _
            Text:="myBookMarkNane \h  \* Charformat", _
            PreserveFormatting:=False
   End With
EXCEPT I cannot get a space to appear between the end of the title text and the inserted cross ref field. I'd expect it to be as simple as having a trailing space on the end of title text:

Code: Select all

... & "my text here "
but such a space gets trimmed. I've also tried adding a space thus:

Code: Select all

... & "my text here" & " "
and that's ignored as well.

Any tips on how to get my cross ref field to be preceded by a space?

Ken

User avatar
HansV
Administrator
Posts: 78449
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: add a cross ref to a caption in VBA

Post by HansV »

Code: Select all

    With Selection
        .InsertCaption Label:="Table", Title:=":" & vbTab & "my text here"
        .Collapse wdCollapseEnd
        .TypeText " "
        .Collapse wdCollapseEnd
        .Fields.Add _
            Range:=Selection.Range, _
            Type:=wdFieldRef, _
            Text:="myBookMarkNane \h  \* Charformat", _
            PreserveFormatting:=False
   End With
Best wishes,
Hans

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: add a cross ref to a caption in VBA

Post by macropod »

stuck wrote:Thank you again, I'm nearly there.

EXCEPT I cannot get a space to appear between the end of the title text and the inserted cross ref field. I'd expect it to be as simple as having a trailing space on the end of title text:
...
Any tips on how to get my cross ref field to be preceded by a space?
I already showed you how to do that ...
Paul Edstein
[Fmr MS MVP - Word]

User avatar
stuck
Panoramic Lounger
Posts: 8173
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: add a cross ref to a caption in VBA

Post by stuck »

Thank you Hans, .TypeText is more elegant / simpler that what I've just come up with:

Code: Select all

        .InsertAfter (" ")
        .Collapse wdCollapseEnd
Paul, I'm sorry but I obviously miss understood something because I couldn't get your code as given to work. However, it was enough to get me to something that, apart from the missing space, did work so I'm very grateful for your help.

Thank you, I've learnt a little bit more from you both (again).

Ken