table added to Word document is empty

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
 
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top