Consulting

Results 1 to 9 of 9

Thread: Itterate through image and table captions

  1. #1
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    4
    Location

    Itterate through image and table captions

    Hi,

    Where can I find the captions collection? Are they shapes? I'm trying to fill a listbox with the captions.

    Other question: Easy way to select an image or table after adding one in vba?

    And another one: How do you group multiple selected objects in code?

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. There is no Captions collection. Table captions are simply text. There is NO direct connection. If you make a Caption, and then put a bunch of other text between the Caption and the table...Word has no problem doing that. The Caption is text of a Caption style.

    There is however, an AutoCaptions collections, but...(from Help)
    AutoCaption objects cannot be programmatically added to or deleted from the AutoCaptions collection.
    2. Use objects. E.g.[vba]
    Dim oTable As Table
    Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, _
    NumRows:=2, _
    NumColumns:=3)
    oTable.Cell(2, 3).Range.Text = "Second row, third column."
    [/vba]oTable can be actioned/altered with any method or property of a Table. It IS that table. The above creates a table and puts the text "Second row, third column." into, well the second row, third column cell.

    3. You can't. Well, maybe, but you will have to be very explicit with what objects you are talking about. You can make non-contiguous selections in the GUI (holding Ctrl and selecting another chunk). You can NOT do this in VBA. VBA uses Ranges, and ranges can not be non-contiguous. In fact, if you DO have non-contiguous selections and try and use VBA on the Selection, only the last chunk will be actioned. VBA will ignore the other chunks.

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "I'm trying to fill a listbox with the captions."

    You can do this. Use a search for the Caption style, and grab the text.

  4. #4
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    4
    Location
    Hey Fumei! Thanks for your answers!

    I'm making an standard report for use in my study, with a custom toolbar to add styles and images, so others who know almost nothing about word, can make a report without spending much time on styles.

    It has buttons to insert headings, that start automatically on new pages and appendixes, images with numberd captions and tables with numbered captions, and a refresh all fields button.

    I can add a picture by an openfile dialog and then selecting it and add a caption to it, but the selection mechanism isn't working for tables. I will puzzle some more, with your info.

    The hardest part is making an custom form to replace the crosslink dialog. I am even doubting if it is going to work if I can only search for caption style...

    anywho, atm my word is freezing when updating fields and even at some other events, so I have to investigate that first.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Reprise #2: You can easily get any specific (existing) table if you bookmark the table, thus having a name. Bookmarks have names.

    Say you bookmark the third table in the document, and say call the bookmark Table3.[vba]Dim oTable As Table
    Set oTable = ActiveDocument.Bookmarks("Table3") _
    .Range.Tables(1)

    [/vba]Voila. The object oTable IS that table. It does not matter if you put in 6 other tables before it - thus turning the Index of the table from:

    ActiveDocument.Tables(3)

    to

    ActiveDocument.Tables(9)

    The index number Tables collection is always in the order the table is in the document. If Table(1) is moved after Table(3), then:

    Table(2) becomes Table(1)
    Table(3) becomes Table(2)
    Table(1) becomes Table(3)

    By using bookmarking tables you can make a table object of any given table by name.

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Sounds good. Just remember that captions are not fields. Nor are they really connected to the table they are the caption of.

  7. #7
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    4
    Location
    Thanks man.

    Can you think of a solution for this:

    Grouping last two shapes (added image + added caption, works!):
    [vba]
    ActiveDocument.Shapes.Range(Array(ActiveDocument.Shapes(ActiveDocument.Shap es.Count).Name, _
    ActiveDocument.Shapes.Item(ActiveDocument.Shapes.Count - 1).Name)).Select
    Selection.ShapeRange.Group.Select
    [/vba]

    But now, this won't work for a table + caption:
    [vba]Dim oTable As Table
    Set oTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, NumColumns:=3)
    oTable.Rows(1).Select
    Selection.Style = ActiveDocument.Styles("Tabelkop - gecentreerd")
    oTable.Rows(2).Select
    Selection.Style = ActiveDocument.Styles("Tabelcel - gecentreerd")
    oTable.Select

    Selection.InsertCaption Label:="Tabel", TitleAutoText:= _
    "InvoegenBijschrift1", Title:="", Position:=wdCaptionPositionBelow, _
    ExcludeLabel:=0
    Selection.TypeText Text:=": "
    'ActiveDocument.Shapes.Range(Array(ActiveDocument.Shapes(ActiveDocument.Sha pes.Count).Name, _
    ' ActiveDocument.Tables(ActiveDocument.Tables.Count).)).Select
    ActiveDocument.Range(Array(ActiveDocument.Shapes(ActiveDocument.Shapes.Coun t).Name, _
    ActiveDocument.Tables(ActiveDocument.Tables.Count))).Select
    Selection.ShapeRange.Group.Select[/vba]
    There are all kinds of things going wrong in this last part, I know, but I don't know how to fix it...

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. I will repeat. Captions are just text in a specific style. There is NO accessible direct relationship to the table for which it is a Caption. Further, as it IS only text, it is NOT a Shape.

    For example, below represents a table, with a caption.

    Table 1 (caption)
    Actual Table

    OK?

    Now, let's change it to....

    Table 1 (caption)

    bunch of text now between "caption" and the table.
    yadda yadda yadda
    MORE text!

    space

    Hey...a Different Table!!!

    more text
    Actual Table

    Tell me, what is the relation between the "caption" and the table NOW? None. There is a lot of stuff (including another table!!!) between them. There never was a relationship.

    There is no Tables(x).Caption property.

    And the "caption" is NOT a Shape (or InlineShape), it is plain text - formatted with the Caption style.

    2. Try to not use Selection. You also have not grasp my point/suggestion about using objects. The whole point of making a table object, is to USE it.
    [vba]
    Dim oTable As Table
    Set oTable = ActiveDocument.Tables.Add _
    (Range:=Selection.Range, NumRows:=2, _
    NumColumns:=3)
    oTable.Rows(1).Select
    Selection.Style = ActiveDocument.Styles("Tabelkop - gecentreerd")
    oTable.Rows(2).Select
    Selection.Style = ActiveDocument.Styles("Tabelcel - gecentreerd")[/vba]should be:

    [vba]
    Dim oTable As Table
    Set oTable = ActiveDocument.Tables.Add _
    (Range:=Selection.Range, NumRows:=2, _
    NumColumns:=3)
    oTable.Rows(1).Range.Style = _
    ActiveDocument.Styles("Tabelkop - gecentreerd")
    oTable.Rows(2).Range.Style = _
    ActiveDocument.Styles("Tabelcel - gecentreerd")

    [/vba]There is NO need to Select anything. You can action - as I mentioned - all properties and methods of the table object directly.

    Same with:[vba]
    oTable.InsertCaption Label:="Tabel", TitleAutoText:= _
    "InvoegenBijschrift1", Title:="", Position:=wdCaptionPositionBelow, _
    ExcludeLabel:=0 [/vba]The oTable object, not Selection.

    Please explain what:[vba]
    ActiveDocument.Range _
    (Array(ActiveDocument.Shapes(ActiveDocument.Shapes.Count).Name

    [/vba] is supposed to be.

    What Shape?? The table is not a Shape, and neither is the Caption.

    PS. Please note that I am using the underscore character ( _ ) to break up the code lines.

  9. #9
    VBAX Newbie
    Joined
    Oct 2008
    Posts
    4
    Location
    Relax, I got the point!
    I am just improvizing, trying to do the same trick as with the image. And that is not possible, ok. I just thought it would be, because you can select the table like an image (clicking the little box topleft when you hoover over it), but I found out captions added to images are in a textframe and to tables they are not.

    Making a textframe and ctrl-selecting it with a table is also not possible, so I don't see a way to make a foolproof caption that stays with the table.

    Thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •