PDA

View Full Version : Update word table based on another table input



mpdsal
10-19-2012, 12:54 PM
Greetings.

I am using Word 2010 and I want to perform the following action in a specific cell(s) within a Word table. My word doc contains 5 different tables. I will manually update the cells in table 5 but I want to automatically update the contents of table 3. For example:

Table 5, cell 3 (C3) contains the text "GREEN" with a green colored background. Table 5 has 12 rows and 3 columns. I want Table 3, cell 3 (C3) to be populated with the same text and format as Table 5 (C3).

In addition, I may add or delete a row from each table. Is there a way of using VBA to perform the update noted above AND count the rows on each pass so that it runs until it hits the last row whether the table contains 1 or n number of rows?

I hope I have stated this clearly enough. I can submit the document if that would help.

Thanks

macropod
10-22-2012, 07:15 PM
Cross-posted at: http://www.msofficeforums.com/word-vba/14819-update-word-table-based-another-table-input.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

fumei
10-22-2012, 07:47 PM
1. Please do not cross-post.

2. "In addition, I may add or delete a row from each table. Is there a way of using VBA to perform the update noted above AND count the rows on each pass so that it runs until it hits the last row whether the table contains 1 or n number of rows?"

Automatically? No, something will have to cause execution.

I am not following the need to count rows.

macropod
10-23-2012, 01:42 AM
One could hook into the Document.SelectionChange Event (see http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.selectionchange.aspx) but, as I noted in the cross-posted thread, the project:

would require some fairly involved vba programming, especially if Word's supposed to figure out what's supposed to happen when rows are added or deleted.
and

what if you add/delete a row above the existing one? How is Word going to keep track of what's going on? Sure, it might be able to for the current session, but as soon as you close the file, it'll forget all it knew about what had been going on. To over come that, you'd need to create & store the relevant content in a document variable or something such. Gets real complicated once you start moving rows around ...

fumei
10-23-2012, 09:07 PM
To meet the requirements mentioned by macropod (the adding/deleting before/after logic for table rows) will take precise thinking. Possible, but it is convoluted. Lots of testing.

Regarding Selection_Change, the is the ONLY (AFAIK) way with basic VBA to automaticly test for changes. And it would work when you made a change in C5. BUT....it would execute, wait fro it, for EVERY change. Anywhere. For any reason. Every character you type, anywhere. Any movement of the cursor (followed by a click, thus a change inSelection). So if that routine had a lot of code to run (due to the testing logic), that is a LOT of - pointless - calls to the routine. Less than 0.01% effective...just kidding. On large documents it would be way less than that.

macropod
10-23-2012, 09:13 PM
I think the very first test would have to be for .Information(wdWithInTable). If not, exit. Then you'd test which table. From there, it gets 'interesting'.