How to have variable sized word reports

bentheimmigrant

Lost & confused
Local time
Today, 17:26
Joined
Aug 21, 2015
Messages
60
So, I have been tasked with generating reports in Word if possible. There are multiple reasons for this, but primarily they are the need to a) have equations and various nice looking things in the report, b) be built by non-access people for me to then set up the database connections with (because I simply don't have the time to build these reports from scratch), and c) be editable post-creation by non-access people.

I've figured out how to do this generally, and have in the past used content controls (CC in the code below), and the generic code:
Code:
For Each CC In wDoc.ContentControls
     If CC.Title = TargetName Then CC.Range.Text = TargetText
Next CC

However, what I will have to do is effectively generate various types of subreports, some of which will be called, some of which won't, with some being called multiple times.

So a main report may be populated with report type A, B, C, and D, but it will look more like A1, B1, B2, B3, C1, B4, B5, B6, D1, B7. Each instance of the sub-reports will be populated by a different record (with a field determining which type to use).

How would you approach this challenge? Unfortunately most of my VBA knowledge is Excel and Access, so I'm not really up to speed on how much I can bend Word to my will (may have to force their hand and push for Excel reports, as then I can just copy template sheets and fill certain cells, but I'd rather do what I'm asked).

I suspect the only really viable option is to open a new instance for each subreport, and then copy contents into a main report. That or programmatically create a master document (yikes!). But I would love to hear otherwise.
 
HTML is pretty easy to learn.
HTML is very flexible.
You can open HTML in Word and save it as Word document.
If you can't just use mailmerge, I'd go with HTML.
 
Here is food for thought. Just like Access and Excel, Word uses the Component Object Model approach. The trick is to know the components and structure. Therefore, do some online research for Word Macros - which will include VBA manipulation. Try to find the Word component diagram that defines the layout of the big, butt-ugly set of collections that is a Word document.

You will find, for example, that you have a collection of Paragraphs (defined by having hard RETURN/ENTER between two strings of text), Words (defined by spacing characters), Sections (defined by specific types of Section Break characters). You can make selections with a starting and ending element (i.e. paragraphs or words) and then set the attributes for each, with highlighting, font style selections, color changes, etc. There are methods that one can use that will insert data at various levels.

For instance, if you really wanted to do it this way, you could set up "boilerplate" files or "template" files that you would insert into your raw data file via MailMerge or by doing a Word App that does a MailMerge as an intermediate step but then grabs the result of that step to do an Insert operation.

I've looked over the methods available but never tried doing things at that level - never needed to. My most recent foray into Word generation was to diddle with Word tables (which look surprisingly like Excel Spreadsheets except not so complex) to insert information or to copy data provided by users on a machine-readable form. Also did a Word document cross-referencer that gave me page numbers where a particular character name was used. All of it driven by Access using a Word.Application object. They are pretty versatile but just require a bit of reading first.
 
Hmm, thanks, that's a very interesting thought. I wonder if maybe using tables and having all the desired content inside (even if it's huge) would be the simplest way of navigating/building. Will investigate further.
 
The design goal should also follow one simple premise: If you need tables, use tables. If you need paragraph-based narrative, use paragraph-based narrative. Don't let the ease of an alternative method make you deviate from the goal of the document's intended format.
 
Ben, re Doc's suggestion of using Word's COM, I find it much easier to record a macro in Word, then copy/paste the vba into Access and modify it accordingly. I also liberally use Word's bookmarks, especially in the first cell of a table.
 

Users who are viewing this thread

Back
Top Bottom