PDA

View Full Version : Add Styles,Numbering and TOC in MS word 2003 using VBA



balasvpr
09-26-2008, 03:51 PM
Hi all,

I am creating a Macro in MS Word 2003 using VBA to import the data from CSV(Comma separated values) files in table format. I have created the tables in MS word and successfully imported the data from CSV file. Now i want to add headings to each and every tables in MS word using VBA and add section numbers to the sections and the same section number and section name to be display in Table of content. Please help to me add Section number and create a Table of content in MS Word 2003 using VBA.

fumei
09-27-2008, 09:40 AM
1. Sections do not have names. The number of a Section is simply the order - Section 1, Section 2 etc. Have you created new Sections for each table?

2. ToC are generated by whatever Style is defined to use for a ToC. The defaults are Heading 1, Heading 2 etc., but you can use any style you want.

3. Are the tables already existing, or are you making them?

In any case, simply make a paragraph for each table with the Section number (as text), and give the paragraph the style that is going to be used for the ToC.

balasvpr
09-27-2008, 01:11 PM
I have imported the data from CSV files and created a number of table in MS word 2003. Then added a text above the table (for table headings) and added style to the text.
If i go to the MSword and enable the Table of content manually i can able to see the headings added in TOC. Now i want to enable the same condition through VBA. Also i want to add Numbering to the text i added a style for table heading..

Please let me know if have any class to do this .. or if u have a sample program update here

macropod
09-28-2008, 12:25 AM
Hi balasvpr,

If your document already has a TOC and the Heading level you're applying to the table heading is one that's captured by the TOC, then all you need to do is to update the TOC. If it's a heading that isn't already used by the TOC, all you need to do is to add that heading to the TOC's heading scope (potentially just a simple change to the upper or lower level index #).

The code is trivial. For example:
Sub RefreshTOCs()
Dim i As Integer
With ActiveDocument
For i = 1 To .TablesOfContents.Count
With .TablesOfContents(i)
.LowerHeadingLevel = 4
.Update
End With
Next i
End With
End SubSets the lower limit to Heading 4 and updates the TOC.

balasvpr
10-01-2008, 03:11 AM
Is there anyway to check the MS word contains the ToC(Table of contents).?

I want to
1. Add the ToC in MSWord if document doesnt contains the Toc
2. Update the Doc if documents have the ToC

macropod
10-01-2008, 05:40 AM
Hi balasvpr,

Building on the previous example, you could use something along the lines of:
Sub ManageToCs()
Dim i As Integer
With ActiveDocument
For i = 1 To .TablesOfContents.Count
With .TablesOfContents(i)
.LowerHeadingLevel = 4
.Update
End With
Next i
If .TablesOfContents.Count = 0 Then .TablesOfContents.Add _
Range:=.Range(0, 0), UseFields:=False, UseHeadingStyles:=True, _
UpperHeadingLevel:=1, LowerHeadingLevel:=4, _
IncludePageNumbers:=True, RightAlignPageNumbers:=True
End With
End Sub