Question

• I'm using VB.Net 2010 top create a Word 2010 doc file (creating the word doc from scratch)

I have found a couple of different examples, but they were very confusing and, ultimately, didn't work.

TIA

Monday, June 27, 2011 5:12 PM

• Ok, I've finally found an example that works (mostly).

```    With oDoc
RightAlignPageNumbers:=True, _
IncludePageNumbers:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=True)
.TablesOfContents(1).Range.Font.Name = "Arial Narrow"
.TablesOfContents(1).Range.Font.Size = 11
.TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
End With
```

Just a couple of issues that, hopefully, y'all can help me with...

First, the TOC works on a simple level.  All the headings that I need on the TOC are included, all I had to do was define and add a style (styleSection) to each heading I want included.  However, I need to add an indentation level to a couple of sub-headings.  I presume I need to define another style and apply that to the sub-headings.  But how does the TOC know which what to indent?  How do I add the sub-heading style to the TOC structure above?

Second, even though I tried I could not set the font on the TOC to what I wanted (Arial Narrow/11pt).  It seems to want to set itself to it's default (Calibri/10).  How do I set the TOC font the way I want?

Again, TIA

• Marked as answer by Tuesday, June 28, 2011 5:20 PM
Monday, June 27, 2011 8:49 PM

All replies

• Ok, I've finally found an example that works (mostly).

```    With oDoc
RightAlignPageNumbers:=True, _
IncludePageNumbers:=True, _
HidePageNumbersInWeb:=True, _
UseOutlineLevels:=True)
.TablesOfContents(1).Range.Font.Name = "Arial Narrow"
.TablesOfContents(1).Range.Font.Size = 11
.TablesOfContents.Format = Word.WdTocFormat.wdTOCSimple
End With
```

Just a couple of issues that, hopefully, y'all can help me with...

First, the TOC works on a simple level.  All the headings that I need on the TOC are included, all I had to do was define and add a style (styleSection) to each heading I want included.  However, I need to add an indentation level to a couple of sub-headings.  I presume I need to define another style and apply that to the sub-headings.  But how does the TOC know which what to indent?  How do I add the sub-heading style to the TOC structure above?

Second, even though I tried I could not set the font on the TOC to what I wanted (Arial Narrow/11pt).  It seems to want to set itself to it's default (Calibri/10).  How do I set the TOC font the way I want?

Again, TIA

• Marked as answer by Tuesday, June 28, 2011 5:20 PM
Monday, June 27, 2011 8:49 PM
• Hi Clif001,

Below code sample implements inserting a 3 x 5 table and filling it with specific data.

oTable.Rows.Item(1).Range.Font.Bold = CInt(True) to set each row's format

oTable.Rows.Item(1).Cells(1).Range.Font.Underline = CType(True, WdUnderline) to set each cell's format

Please have a try and let us know the situation on your side.

```Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Create Word Application
Dim oWord As Word.Application = CType(CreateObject("Word.Application"), Application)
' Create new word document
Dim oDoc As Word.Document = oWord.Documents.Add()
oWord.Visible = True

'Insert a 3 x 5 table and fill it with specific data
Dim r As Integer, c As Integer
Dim oTable As Word.Table = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 5)
oTable.Range.ParagraphFormat.SpaceAfter = 6
For r = 1 To 3
For c = 1 To 5
oTable.Cell(r, c).Range.Text = "Row" & r & "Col" & c
Next
Next
'make the first row bold and italic
oTable.Rows.Item(1).Range.Font.Bold = CInt(True)
oTable.Rows.Item(1).Range.Font.Italic = CInt(True)

'set the second row's font as Arial Narrow/11pt
oTable.Rows.Item(2).Range.Font.Name = "Arial Narrow"
oTable.Rows.Item(2).Range.Font.Size = 11

' Save this word document
oDoc.SaveAs("C:\myfile.doc", True)
oDoc.Close()
oWord.Application.Quit()

End Sub
```

Have a nice day.

Best regards

Liliane Teng [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Tuesday, June 28, 2011 8:04 AM
• Hi Liliane,

I finally figured out how to create the TOC, but needed help in indenting some sub sections and setting the font (read my second post).