Reputation: 433
I'm wanting to update various tables in word documents using this method... http://msdn.microsoft.com/en-us/library/2dw39e8e.aspx
I'm basically going to add rows based on an SQL view. The problem I'm having is trying to find the table objects programmatically in the Word document so I can add the rows. There are a number of tables and they can be located in different places thoughout the document. I can't find a "TableID" field or something similar I can edit in the Word document so I can locate it programmatically.
I'm using Word 2010 and Visual Studio 2010.
Thanks for any help, Aaron
Upvotes: 2
Views: 3109
Reputation: 15863
You could put the table in a content control. A content control can have an ID (and a tag, and a title).
A different approach for adding the rows would be to wrap the relevant row in a content control, and repeat it. On this model, you put your SQL data in a custom XML part. The built-in data binding doesn't support repeating content, but you can do this via VSTO and/or the Open XML SDK. I've suggested a convention for how you might set this up, at dev.plutext.org/svn/docx4j/trunk/docx4j/sample-docs/databinding/conventions.html
Upvotes: 1
Reputation: 20602
The Table object, does have an ID property that you can get/set, though it is designed for saving as a web page only and as such any IDs that are set are not saved when in .doc(x|m) format.
There is a description field (.Descr) that was added in Word 2010 that you could use, but even Word 2007 does not support it, I do not know if that would be a problem if you are using your work elsewhere than the environment you state.
Dim cTbl, targetTbl As Table
For Each cTbl In ActiveDocument.Tables
If cTbl.Descr = "target-id" Then
Set targetTbl = cTbl
Exit For
End If
Next
Your best bet if not, is to ensure there is a known cell with the ID in it for the table and cycle through the tables until you find the one you want. Either that or have knowledge in advance of the table order (which seems like it is not an option for your scenario). Conceivably you could use custom styles for each table, that do nothing, but the names of which can be use for identification - similar to using css classes for identification in HTML DOMs.
Upvotes: 1