Solved

Include Section number in captions

Posted on 2004-10-26
350 Views
Last Modified: 2013-11-18
I have 5 sections in my document, all created by inserting section breaks

I would like each table in each section to have the section number as part of its caption numbering

Eg,
A table in section 1 will be Table 1.1, Table 1.2.....Table 1.xx
A table in section 3 will be Table 3.1, Table 3.2......Table 3.xx

I follow the steps
Insert > Reference > Caption > Numbering
Then, check Include Chapter Number...which WONT work, as i dont use Heading 1 to Heading X in my document. I do have a numbered style called Main Header at the top of each section, which is numbered exactly as the section numbers

How do i use the numbering from the Main Header style, or include the section numbers in the captions ?

EDIT: Should have mentioned this, sorry..Im using Word 2003 SP1 (11.6359.6360)
Hope thats not impossible, or at least a way to get the numbering right
0
Question by:etdwh
    8 Comments
     
    LVL 5

    Expert Comment

    by:AntonioP
    You could insert it into your caption afterwards.  Create the caption without the reference, then click in the caption at the correct location and insert a cross-reference to the numbered list.  It seems to survive updating.  You might wait to see if anyone has a better solution.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    The following will add the table number, as text, after the table.  I could not get it to add a caption (the .InsertCaption function did not work for me), but it should give you some idea as to how to get the table number, at least:

        Sub InsertTableNumber()
       
            Dim oSec As Word.Section
            Dim oTab As Word.Table
            Dim oPar As Word.Paragraph
           
            For Each oSec In ActiveDocument.Sections
           
                iTab = 0
               
                For Each oTab In oSec.Range.Tables
               
                    iTab = iTab + 1
                    sTab = CStr("Table " & oSec.Index & "." & iTab)
                   
                    oTab.Range.Next.InsertBefore sTab & vbCrLf & vbCrLf
                   
                Next oTab
               
            Next oSec
       
        End Sub

    HTH

    J.
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    Add the following line:

                          oTab.Range.Next.Style = "Caption"

    after

                          oTab.Range.Next.InsertBefore sTab & vbCrLf & vbCrLf

    to give the paragraph a caption style...

    For some reason, when I try to use

                          oTab.Range.InsertCaption ...

    it errors with 'Command Failed'.  I think it may be a bug but I'm not sure...

    HTH

    J.
    0
     

    Author Comment

    by:etdwh
    Thanks for the reply. I shall try this out.

    From what i understand from the code, it numbers every table in the document.
    Is there a way to limit this ?

    There were some parts in the document (section 3) i used tables as placeholders and to partition the page into several parts.

    Or is there a way to apply this macro only to a selection of tables ?
    0
     
    LVL 16

    Accepted Solution

    by:
    As it stands, the macro I wrote above will do it to all tables, no exceptions.  You _could_ limit it, but it would require something common about all the 'placeholder' tables.  Then you could limit so it does not touch those tables.  The following change will make the macro only work for tables that have an outer border:

        Sub InsertTableNumber()
       
            Dim oSec As Word.Section
            Dim oTab As Word.Table
            Dim oPar As Word.Paragraph
           
            For Each oSec In ActiveDocument.Sections
           
                iTab = 0
               
                For Each oTab In oSec.Range.Tables
               
                    If oTab.Borders.OutsideLineStyle <> wdLineStyleNone Then     '<-- IF TABLE'S OUTER BORDER IS 'NONE' DO THE COUNT
                   
                        iTab = iTab + 1
                        sTab = CStr("Table " & oSec.Index & "." & iTab)
                       
                        oTab.Range.Next.InsertBefore sTab & vbCrLf & vbCrLf
                        oTab.Range.Next.Style = "Caption"
                   
                    End If
               
                Next oTab
           
            Next oSec
       
        End Sub


    HTH

    J.
    0
     
    LVL 3

    Expert Comment

    by:MBarber1957
    You could use Insert->Field->Categories: Numbering->Section to put the Section number into your Table. The Table number could be an Autonum field. One downside to this is that, if you change the position or quantity of tables in the document, you will need to remember to update the field references; even here though you could turn the Update Fields option on in Tools->Options->Print->Update Fields which will automatically renumber all fields prior to printing. This way you do not need the Caption facility at all.
    0
     

    Author Comment

    by:etdwh
    I shall accept jimbobmcgee's answer. Not only does this work perfectly, i could add more conditions later, such as skip sections, even caption other objects. If what works in VB works here too, i could add input boxes for this...i guess

    MBarber1957, thanks for the suggestion, but your idea wont work, because if its not a caption, i cant create a List of Figures and Tables later. Futhermore, captions can also have a specific style for it. But just for the sake of numbering the tables, your method does work.

    But i prefer jimbobmcgee macro because its automatic. Drawback, cant update the fields if you add more tables. Only option, select all captions starting with Table, delete it, and run macro again

    Thanks for the solution
    0
     
    LVL 16

    Expert Comment

    by:jimbobmcgee
    Thanks for the points.  It should be worth noting that my code doesn't actually create captions, it justs makes a bit of text after the table and makes it look the same as all other captions.  I don't know if this will work when you come to create your contents page.

    If anyone can give the correct implementation of the InsertCaption method (and why it just says Command Failed for me), for use in the code I have posted above, I will award them 200 points, myself...

    J.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    934 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now