PDA

View Full Version : Getting started on document generation



ruli
01-02-2009, 08:28 AM
Hi,

After doing lots of VBA stuff in XLS I am now starting out in Word... and having quite some trouble getting started.

What I want to do is quite simple:
- I have a "library" of Word documents
- Using an input file I want to generate a document that merges selected documents or even parts of these documents) from this library
- Complicating factor will be that each part that has to be added will have a part that goes in the main section of the new document, with a reference to an appendix in the back.

I am trying to find some code to get me started, I will fine-tune things on my own. Any suggestions?

Thanks,
Ruud

ruli
01-03-2009, 08:00 AM
Just to be complete: Word 2003, VBA 6.3

EricFletcher
01-03-2009, 09:27 AM
...
What I want to do is quite simple:
- I have a "library" of Word documents
- Using an input file I want to generate a document that merges selected documents or even parts of these documents) from this library
- Complicating factor will be that each part that has to be added will have a part that goes in the main section of the new document, with a reference to an appendix in the back.
...
I'm not sure it will ever be simple, but it will certainly be less complicated if your source documents are consistently formatted.

Read about the RD and INCLUDETEXT field codes. These let you build a document by referencing the file names of other documents (RD is more for creating a ToC and/or indices).

If the source documents have internal references set up as REF field codes -- and if the bookmark names they use are unique across the set of source files -- the internal references will be able to be used to link to the appendix in the assembled document.

Finally, if the source files used styles, your task will be much easier: without a consistent underlying template, direct formatting and differing style definitions will cause chaos in the assembled file formatting.

The VBA assembly part will be the least of your worries IMHO; it it were me, I'd look for VBA methods to ensure that the source docs are set up properly first. Then the assembly routine might be fairly trivial.

fumei
01-06-2009, 10:07 AM
I have to completly agree with Eric. The VBA part of this is not terribly difficult.

IF - a big if, but this is why it is important to use Word properly - as Eric mentions, the underlying template is consistently, properly used across the documents, then again, the VBA part is not hard.

In other words, IF your library files are properly constructed, then building new documents using parts of them is not hard.

There are a number of ways. INCLUDETEXT, as Eric mentions. Or perhaps AutoText - this function is under-used IMO.

Could you explain "using an input file"? I do not understand what you mean by that. HOW are you determining which chunks to use?

ruli
01-08-2009, 02:13 AM
Thanks Eric and Fumei for your answers. I know that the code i require is probably not hard but as I said I am not used to working VBA in Word and I am struggling to find the commands that wouldn't give me too much trouble in Excel (goto top of document, goto next line, ... I'll explain below why I think I need that).

Thanks for the comments on the setup of the library files. I know that a lot of work has been done to make this happen and some more will be needed to finalize it. As we go through every "generated" document to customize it we will find any imperfections that cause chaos in the generated document.

I have been looking into the INCLUDETEXT which looks very promising to me. I like the fact that you're able to setup your bookmarks up front and have them work in the generated document automatically. Is there a way to get rid of the INCLUDETEXT fields after the assembling of the document (sp not seeing them using ALT-F9)? I would like the users to perceive the document as a whole and not as a pile of separate chunks.

My chunks contain local information on cities, activities and directions that combined together form a sort of road book that people can use while traveling.

My thought is to use a database (not Access) to manage the chunks: file location and bookmarks. From this database I want to generate a text file with selected chunks/bookmarks, this would be the input-file I mentioned in my initial posting. Now all I have to do is convert each line into an INCLUDETEXT field in Word using the file location and bookmark information on the input field line.

Does that make sense? If it does I need VBA commands to navigate the input file (after opening this in Word). I have found some basic stuff like Selection.MoveUp Unit:=wdLine but find this much more cumbersome than in Excel where you are far easier able to go exactly where you want to go.

Hope this helps explain what I am trying to do.

Thanks again,
Ruud

EricFletcher
01-08-2009, 09:24 AM
... Is there a way to get rid of the INCLUDETEXT fields after the assembling of the document (sp not seeing them using ALT-F9)? I would like the users to perceive the document as a whole and not as a pile of separate chunks. ...
As you have probably deduced, the INCLUDETEXT field code refers to a document, and brings its content in as needed -- but the content is not actually "there" in the target document. To resolve this, you need only unlink the field code: select it (or all) and press Ctrl-Shift-F9; in VBA, use the "unlink" action. The included content will be disconnected from the source, and you will no longer see them with Alt-F9.

I am intrigued by your application for this. How are the source items being generated? If they include bookmarks or references, you will need to ensure that such elements are unique so that they don't conflict in any combination of assembled components.

Given what you are trying to do, there may be a better alternative to the INCLUDETEXT field. You can access specific cells (or ranges of cells) within an Excel sheet (and presumably an Access database) using the LINK field. For example, to include content from a specific cell from an Excel sheet, I have a Word document with a LINK field that looks like this:

{ LINK Excel.Sheet.8 "SpotPrices.xls" NewLamb \t }

This gets the current content of the range named "NewLamb" from the Excel sheet, and inserts it as text (the \t switch) into my Word document. Check Word's Help for the various switch options; the defaults tend to bring content in as a cell (or table if >1 cell is included) and this may not work so well in your application if the linked content is textual.