vb.net - Create, fill and resize WordTable before inserting it to instance -
i working on application gets data out of database , puts open word instance.
currently it's doing following steps:
- find open word instance (if multiple opened user can select)
dim odocs word.document = wordapplication.application.documents(filepath)
- create table on bookmark in word instance
try wordtable = odocs.tables.add(odocs.bookmarks.item("nameofbookmark").range, datatable.rows.count + 1, datatable.columns.count) catch ex exception wordtable = odocs.tables.add(odocs.application.selection.range, datatable.rows.count + 1, datatable.columns.count) end try
- fill table, when it's in word instance
- looping each row , cell -> in loop happening lot, it's working , doesn't matter question not put code inside here
i know speed can slow because of stuff happening in part fills table, not think it's much.
my problem speed of that. while working fine, takes years execute. can see every cell being filled in opened word-document. thoughts solution find way create wordtable in vb application , insert finished word-table word instance, can't find way so.
is there way that? if yes, please tell me how!
tl:dr can completly create , fill , resize wordtable in vb application before inserting opened wordinstance? if yes, how?
edit bibadia gave perfect answer!
i give full working code - creates table in word-instance. have format later in application.
dim odocs word.document = wordapplication.application.documents(filepath) dim strtable string = "" dim isfirst boolean = true dim intcolumns integer = datatable.columns.count dim introws integer = datatable.rows.count each column datacolumn in datatable.columns if not isfirst strtable &= ";" end if strtable &= column.columnname isfirst = false next each row datarow in datatable.rows each column datacolumn in datatable.columns strtable &= ";" & row.item(column) next next dim rng word.range rng = odocs.application.selection.range rng.text = strtable dim wordtable word.table = rng.converttotable(numrows:=introws + 1, numcolumns:=intcolumns, separator:=word.wdseparatortype.wdseparatorcolon)
three things try:
insert data plain text using delimiters not appear in data (e.g. vbtab , vbcr), use converttotable method of range object. need apply formatting after that.
build table using wordprocessingml , insert using insertxml method of range object. how formatting attempt describe using xml - personally, start inserting simplest possible table pre-filled data, apply formatting using object model if not slow.
use insertdatabase method of range. need able access database using method word can work (e.g. ole db or odbc), need .odc file (or dsn) make work, typically makes distribution of solution harder. may difficult prevent security information being stored in .docx or .odc/dsn.
there article here provides code method (1) , more information applying formatting.
Comments
Post a Comment