A faster way to create tables in MS Word in Office 2k3

D

Dave

Dear Sirs,

I have a bit of code that generates a table with 40 cells (10 rows, 4
cols). This runs incredibly slow and you can actually watch the cells
being populated with data.

Is there a faster way to populate a table where it would seem instant
to the user?

Thank you,
Dave


// Assume that this._word equals the Word.Application of the word doc

object missing = Type.Missing;
Word.Table tbl = this._word.ActiveDocument.Tables.Add(this._word.ActiveDocument.Range(ref
missing, ref missing), 10, 4, ref missing, ref missing);

int index = 0;
for(int rA = 1; rA <= 10; rA++)
{
for(int cA = 1; cA <= 4; cA++)
{
tbl.Cell(rA, cA).Range.Text = index.ToString();
index++;
}
}
 
S

Scott M.

Why not just use the native Word objects? This worked very quickly for me:

Dim myWord As New Word.ApplicationClass()
Dim myDoc As Word.DocumentClass = myWord.Documents.Add()
myDoc.Activate()
Dim myTable As Word.Table =
myWord.ActiveDocument.Tables.Add(Range:=myWord.Selection.Range, NumRows:=40,
NumColumns:=3)
With myTable
..ApplyStyleHeadingRows = True
..ApplyStyleLastRow = True
..ApplyStyleFirstColumn = True
..ApplyStyleLastColumn = True
End With
myWord.Visible = True
 
D

Dave

Scott, Thank you for your response. I am confused. The code that I
listed does use the native MS Word objects. Just that my code is meant
to be used on a document that is already open and potentially in use,
rather than first generating a document, then displaying it.

I have found a cheesy work around. Where I generate the table in html
and paste it in. This way the table gets placed in immediatly. I am
very much open to anyone who has a better way to *quickly* generate a
table in word. =)

Thanks,
Dave
 
S

Scott M.

The difference Dave, is that you are using C# code to generate Word Cell
objects repeatedly. My code just asks Word to build the correctly sized
table directly.

Did you try my code? Works very fast for me.
 
Top