Populate table in Word Template with VBA? - vba

I'm filling in a Word template with data that's been collected from user input. In particular a (variable) number of documents is chosen, and information about each document fills a row of a table.
I've bookmarked several items in the template and successfully filled information in the header from my macro, but the table I'm not so sure with. I bookmarked the first cell and tried tabbing (with Chr(9)) through, and also tried passing an array. (In the template the table has only a first row. Usually tabbing past the last column creates an additional row.)
I can retieve cell contents with
Word.Application.ActiveDocument.Tables(1).Cell(3, 1).Range.Text
but can't write to the any cell except the first, where I placed a bookmark.
Can anyone offer a possible solution to populate the table?

To populate table, use this code
ActiveDocument.Tables(1).Cell(1, 1).Range.Text = "Blah Blah"
This will write to the first cell in the first table. Use a loop to fill the rest of the cells.
I would also recommend see this link.
Topic: Automating Word Tables for Data Insertion and Extraction
Link: http://msdn.microsoft.com/en-us/library/aa537149%28v=office.11%29.aspx#Y1254
Extract:
Summary: Learn how to automate the creation and formatting of tables in Word. Get information about optimizing performance, populating a table with data, formatting table structure, linking table data, and extracting data from a table. (25 printed pages)

I've actually ran into a similar problem using Access to automate filling out a Word table template. I found that if I opened the template in Word prior to running the VBA script, then the Word document is successfully filled out with the table information. My code looks similar to yours as far as adding to it by row. Because the number of fields to be transferred to the form is dynamic it didn't seem like bookmarks for each section would work. If you have any update, I'd be happy to hear of a different way to resolve this.

Related

How to create a macro in word to import multiple tables from excel?

Frequently in my job I need to generate reports with lots of tables of inputs and results. Especially for the result tables, one change in analysis may require editing a dozen spreadsheets. I'd like to create a macro in word that pulls in data from a spreadsheet, with each table on it's own tab, so that if I update any of those tables in excel the word document tables will also update. Given the number of tables/data points, I don't want to have to tell the macro to pull each single data point. The aim would be to reduce time and errors from manual entry.
I'm thinking this would involve the following steps, but not sure how to go about them:
1) Define the name/size for each table in word with matching name/size in excel
2) Tell the macro to pull the data into a table format
I'm not sure if this is possible as so far I've only seen how to insert a caption or a text box, not insert or update entire tables. Any help would be greatly appreciated!
Depending on what you're doing, you may not even need any VBA code.
If you copy a range from Excel and paste it into Word using Paste Special with the 'paste link' option, any subsequent changes in the Excel range will automatically be reflected in the document when the workbook is saved. And, if you name the range in Excel before copying/pasting, the Word content will expend/contract to reflect changes in the named range's scope in Excel. A variety of paste formats is supported.
Alternatively, you might use a DATABASE field in Word.

Is there a (creative) way to hide a text field in Indesign if there is no information in the data merge field?

I am creating a data-merge document in InDesign.
There are various tables that I've created which only show as many rows as there is actual data in the field, through some creative table and cell styles.
Now I've been asked to only have an entirely separate table only show if there is information in any of those fields.
I'm at a total loss. With the way the current structure is set up, I can cause it to not display any text, but it still shows empty header cells and one line of empty row cells.
Pre-DataMerge, with the data fields
Post-Datamerge, with the resulting empty cells
Any creative ideas to hide that table? I was thinking there might be a way to hide the entire text field, if not the table. Maybe a script? I tried one that deletes blank tables, but that didn't seem to work after the data-merge was run.
I am not sure you can get that level of processing with InDesign datamerge. You could think of a script to post remove those tables or use a dedicated plugin such as Easycatalog that can take care of such empty items natively.

Automate adding bookmarks to tables and then create an index

I have a program which outputs a collection of tables in a word document which I eventually want to post as an html file with bookmarks and an index. The tables are grouped by "Name:" where there is a 3 row table that contains detailed header information for a section of data, then there is a second table which can span multiple pages which contains the data for that section. There is then a page break so that the next sections header table is on a new page. This can occur for a variable number of sections numbers in the hundreds. I need to write a script that
searches my document for "Name:", which is unique and would not
appear anywhere but the header table,
grabs the text that follows "Name:" within that table cell (for example "Name: Line 1234)
replaces all the blanks in that text string with an underscore to
make it a suitable bookmark name,
creates a bookmark with the name,
goes back and creates an index at the front of the document
Saves the file as an html
I have a passing familiarity with VB for word, I have used it a bit in excel, but am by no means an expert. I would appreciate any advice on functions and objects that I should be using for this script.
Hey MikeV from what I can gather, your problem seems more conceptual, less specific. What I mean is, have you started yet? Or looking at a blank script page?
I'm relatively new to coding, so I get that myself. What I do is make a list of what I need to do (what you have). Then think of the code or psuedo-code that would go with each step. Then you can start to build your script. You don't have to start with step one (as step 2/3 is often the more interesting bit), but let's do that.
Now, you need to search for a text string containing "Name:". I am proficient with VBA in excel, but haven't done anything for word. So I'd look it up. Googling "VBA find word in word document" will bring you to this page, which shows you how to approach step one. So steal their code, alter it to fit your needs and move on to step 2. Repeat the process, and that's how you build your algorithm! :)
Just a FYI, typically StackOverflow is for specific questions with an answer that can be confirmed, whereas you asked for help building an algorithm. I'd reserve those questions for your programming professor or friend who can help.
cheers

adding a new row to a table when adding another row in a different sheet

I have an Excel workbook in which I have several tables. On 3 of my sheets, there is some kind of "main" table which differ a bit from one another.
These tables are 2 lines by default (1 header and 1 data row) and extend when I add a new line below the one already existing. In some of the fields are formulas which are using values from the other tables. However, if for example, I insert a new value in a field below the final row, thus adding another row to the table, I want the tables in the other sheets to also add another row (and execute the formulas that are in the new row)
This is the sheet in which I have "Soorten Vezels" with their corresponding "max demping" and "soorten connectoren" with their corresponding "max demping":
This is the "maximale demping" file:
This is the measurement form, in this form, the user should not have to enter anything, except for the 2 dropdown menu's "type kabel" and "type connector":
What I would like is that when the user for example, inputs the length ("lengte") in the "maximale demping" file, a new row gets added (the fields "kabelnr", "van" and "naar" will be filled in automatically from the "gegevens" file)
But I would also like the workbook to add a new line in the measurement form, automatically fills in the "kabelnr", "van" and "naar" fields. So that I only have to choose the "type kabel" and "type connector" from the dropdown menu.
How do I do this (with VBA)?
Personally I like the MSDN when I am looking for answers to VBA and vb.net. I have found it to be thorough and comprehensive. The more I learn, the more I understand what they are teaching on this site.
This is a good tutorial to get started. Follow the links from this page.
Getting Started with VBA in Excel 2010
http://msdn.microsoft.com/en-us/library/office/ee814737(v=office.14).aspx
Also remember to search Stack Overflow as you progress, there are an amazing number of questions covering specifics you may require when you start, or ask more questions.
If you require further assistance you can look at the links below.
This link is helpful. But I would navigate through the links below to get a broader view and to find an explanation that works for you.
Insert Row in Tables Multiple Sheets Excel 2010
http://chandoo.org/forums/topic/insert-row-in-tables-multiple-sheets-excel-2010
A forum- this link leads to a question about inserting rows.
MrExcel.com
http://www.mrexcel.com/forum/excel-questions/58685-insert-rows-between-different-data-2-a.html
If you persevere, down the page, there is good discussion, with code examples and trouble shooting (you will get a pop up when you go to this link asking to sign up for the newsletter, you can disregard this without a problem)
Insert Row in Tables Multiple Sheets Excel 2010
http://chandoo.org/forums/topic/insert-row-in-tables-multiple-sheets-excel-2010
A useful Q&A from Microsoft forum.
Insert blank rows in excel table with vba
http://answers.microsoft.com/en-us/office/forum/office_2007-customize/insert-blank-rows-in-excel-table-with-vba/69e369a8-e656-4f68-adcd-c57e37253f12
Please let me know if this helps and ask new questions freely as you go.

How to extract specific tables from a MS-Word document using VBA?

VBA is a programming language so I'll assume this question is ok on SO.
What API calls and other techniques can I use, to extract specific tables from an MS-Word document?
I need to write a program which will open several Word documents, and look inside for tables which have a certain text in Row 1 Column 1, and output those tables to another file, preferrably as cells in an Excel spreadsheet.
Is this possible? How would you tackle this? Where to start?
Thanks
The Document.Tables collection contains all tables in a document.
Each Table in this collection has certain properties, such as .Rows, .Columns, or .Cell, which give access to a given row, a given column or a given cell.
The Table.Range.Copy method copies the given table to the clipboard.
The Worksheet.Paste method pastes the copied table into an Excel sheet, using the currently active cell as the insertion point.