Automate adding bookmarks to tables and then create an index - vba

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

Related

Access to Word Template

I am having issues figuring out the best way to do this:
I have a word template for an interview pre-night. What I need to do is fill out the word template with the interviewer and the people who are interviewing them. There will always be 1 interviewee but up to 12 interviewers. The part giving me issue is that there will not always be 12 interviewers so the area that the data is moved to needs to be dynamic. Should I create a table or bookmarks in Word and use VBA to move the data, or design the report in Access? Thanks for your help!
I am going to assume that what you are trying to do is complicated enough that "mail merge" won't work for you.
It really depends on whether you need the end result to be a Word document or an Access report. Both easily convert to PDF for document archiving. If you prefer to work with Word, add the key fields into your document with all the formatting necessary and then use VBA to do a search replace.
Two ways you could go about dealing with the 1-12 interviewers issue:
Use VBA to create one long segment containing all interviewers as a
single "field".
Add 12 sets of key fields and use search/replace
(through VBA of course not manually) to fill in the exiting
interviewer info and delete the key fields for the non-existing ones.

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.

Any way to make Word table first row repeat when inside other table?

I'm automating a word document in vb.net. My problem is I need a table within another table to repeat the first row. Is there any way to do this?
The table's textwrap is set to none, and the first row is the only one that has the repeat has header property set.
I CAN'T take the table out of it's containing table. This solution is not an option.
This has nothing to do with the fact that the document is automated too.
Using word 2010.
I just did a quick test with Word 2010.
I created a table, and checked "repeat as header row at the top of each page".
Sure enough, that worked.
Then I created another table, and cut/pasted the first one into it.
The header was not repeated in the Word UI, although the property remained set.
I had a quick look around for properties which might affect the behaviour of nested tables, but couldn't see any.
Then I googled "word nested table repeat header row", which returns quite a few relevant results.
Conclusion: you can't make a header row in a nested table repeat on subsequent pages. Tricks like putting it inside a content control didn't seem to work either.

Word VBA - Matching large selecting of text based keys with data. Embedded resource/text?

I have a pretty complex VBA plugin for Word written that automatically creates a report for me, using XML input, cycling through the X objects within the report to create the output. It is currently embedded into a Word Template file .DOCM.
I need to insert into the report a static list of text, based on the name of the item within the XML. For example, within my XML I have entries with a name BLAH1, BLAH2, BLAH3. Every time I see BLAH1, I need to match it with the static INSERT1, and BLAH2 match it with INSERT2, etc.
This seems simple enough, but her lies the problem...
It appears there are no Hashmap's in VBA without requiring external libraries, which I can't really rely on, since I can't install items on the machines where this will be running. As a result I can't store this reference data in a Hashmap as far as I can tell.
I can't seem to concatenate more than about 20 lines of strings together without hitting a max within VBA, and just parsing the chunk of text for what I need since there are about 1500 "lines" in my reference data, which greatly exceeds 20.
I also haven't found a way to embed a text, or any other type of file to hold this information within the file, and then parse the data.
I really would like to have everything within the single template file, without requiring additional text or other files to be bundled with the document. If there is no other option, I will go that route, but I wanted to see what create ideas people at Stackoverflow might have first ;-)
Have you considered using Word's Document Variables? They are name/value pairs stored invisibly within the document. (ActiveDocument.Variables("BLAH1").Value = "INSERT1" to create one, debug.print ActiveDocument.Variables("BLAH1").Value to retrieve a value (you have to use an error handler to detect non-existent indices if you go that route). Word can store (at least) hundreds of thousands of these things).

Populate table in Word Template with 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.