gaganji143
New Member
- Joined
- Feb 12, 2019
- Messages
- 5
Hey all. I'll try to make this brief and simple.
I have
How could I edit the documents, and create a VBA routine, so that I can open each document, look for a set of fields which might need to be filled in, and substitute a value?
For instance, something that works like:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">foreach document in set_of_templates
if document.FieldExists("Name")then document.Field("Name").value = strName
if document.FieldExists("Address")then document.Field("Name").value = strAddress
...
document.saveAs( thisWorkbook.Path &"\GeneratedDocs\ "& document.Name )
next document</code>Things I've considered:
I have
- 40 or so boilerplate word documents with a series of fields (Name, address, etc) that need to be filled in. This is historically done manually, but it's repetitive and cumbersome.
- A workbook where a user has filled a huge set of information about an individual.
- A way to programmatically (from Excel VBA) open up these boilerplate documents, edit in the value of fields from various named ranges in the workbook, and save the filled in templates to a local folder.
How could I edit the documents, and create a VBA routine, so that I can open each document, look for a set of fields which might need to be filled in, and substitute a value?
For instance, something that works like:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">foreach document in set_of_templates
if document.FieldExists("Name")then document.Field("Name").value = strName
if document.FieldExists("Address")then document.Field("Name").value = strAddress
...
document.saveAs( thisWorkbook.Path &"\GeneratedDocs\ "& document.Name )
next document</code>Things I've considered:
- Mail merge - but this is insufficient because it requires opening each document manually and structuring the workbook as a data source, I kind of want the opposite. The templates are the data source and the workbook is iterating through them. Also, mail merge is for creating many identical documents using a table of different data. I have many documents all using the same data.
- Using placeholder text such as "#NAME#" and opening each document for a search and replace. This is the solution I would resort to if nothing more elegant is proposed.