table added to Word document is empty

  • Thread starter Thread starter bill
  • Start date Start date
B

bill

All,

I have a Visual Studio Tools for Office (VSTO) project that ceates a
COM addin for MS Word. I want to add a table anywhere the cursor is in
an open Word document. The code below works great - except for the
cases when it doesn't. If I try inserting the table in a
"sample" .docx file that I create by just typing in some random stuff
all is well. It seems, however, that if I try inserting a table in a
"highly formatted" word doc that it gets inserted but is invisible and
empty, unless I insert it at the very top of the document. It is
almost as though the code below that comes after "Fill in the table"
does not execute. Any thoughts?

Dim rng As Word.Range =
Globals.ThisAddIn.Application.Selection.Range
Dim doc As Word.Document

doc = Globals.ThisAddIn.Application.ActiveDocument
' Create the table
'
doc.Tables.Add(Range:=rng, NumRows:=10, NumColumns:=4)
' Fill in the table
'
For i = 0 To 9
doc.Tables(1).Cell(i, .... = blah blah
Next
' Decorate the table
'
doc.Tables(1).Borders.OutsideLineStyle =
Word.WdLineStyle.wdLineStyleDouble
doc.Tables(1).Borders.InsideLineStyle =
Word.WdLineStyle.wdLineStyleSingle
doc.Tables(1).Range.Paragraphs.SpaceAfter = 0


TIA,

Bill
 
All,

I have a Visual Studio Tools for Office (VSTO) project that ceates a
COM addin for MS Word. I want to add a table anywhere the cursor is in
an open Word document. The code below works great - except for the
cases when it doesn't. If I try inserting the table in a
"sample" .docx file that I create by just typing in some random stuff
all is well. It seems, however, that if I try inserting a table in a
"highly formatted" word doc that it gets inserted but is invisible and
empty, unless I insert it at the very top of the document. It is
almost as though the code below that comes after "Fill in the table"
does not execute. Any thoughts?

    Dim rng As Word.Range =
Globals.ThisAddIn.Application.Selection.Range
    Dim doc As Word.Document

    doc = Globals.ThisAddIn.Application.ActiveDocument
    ' Create the table
    '
    doc.Tables.Add(Range:=rng, NumRows:=10, NumColumns:=4)
    ' Fill in the table
    '
    For i = 0 To 9
      doc.Tables(1).Cell(i, ....   =  blah blah
    Next
    ' Decorate the table
    '
    doc.Tables(1).Borders.OutsideLineStyle =
Word.WdLineStyle.wdLineStyleDouble
    doc.Tables(1).Borders.InsideLineStyle =
Word.WdLineStyle.wdLineStyleSingle
    doc.Tables(1).Range.Paragraphs.SpaceAfter = 0

TIA,

Bill

oops, it was a coding error - the code needed to be the following:

tbl = doc.Tables.Add(Range:=rng, NumRows:=10, NumColumns:=4)
' Fill in the table
'
For i = 0 To 9
tbl.Cell(i, .... = blah blah
Next


It did not work if there was already a table in the doc. because
doc.Tables(1) was an absolutre reference. Sorry about that.

Bill
 
Back
Top