Manipluating Word docs in Access VBA?

D

Dennis

In Access 2003, I need to programmatically open a Word doc (invisible) and
spin through it looking for bookmark(s). When I find the bookmark(s), I will
insert text in that spot from a database table. Once that's done, I then need
to print the document and close it UNSAVED (thus discarding the insert-text
changes that I made prior to printing).

Can someone please help me out on this? I know it's possible, but have never
had to do it before. I'm a pretty experienced Access and VBA developer, but
this bit is new. What library(ies) do I need to link into the database, and
what syntax should I use to spin through the document(s) and do the text
insert(s)?

Any assistance would be deeply appreciated.

Thanks!
 
A

Albert D. Kallal

Any particular reason you are using book marks???

Using actual merge fields is far easier.

I have a working solution that lets you execute a word merge. You can choose
to print the word doc, and NOT save it....

The code is also designed to print the CURRENT record, OR YOU can throw a
query at the word doc, and it will print all of them (this is a big
advantage over that of book marks which is ONLY good for one record).

Furthermore, with bookmarks, you have to write code for each new document
you create (some of my clients would consider that dishonest that they call
me up for each new word doc they need to make a merge).

So, try the above sample:

The sample I have can be found here:
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

What is nice/interesting about my sample is that is specially designed to
enable ANY form with ONE LINE of code....

Thus, each time you build a new form, you can word merge enable it with
great ease.

Make sure you read the instructions from above, and you should eventually
get to the following page
http://www.members.shaw.ca/AlbertKallal/wordmerge/page2.html


Note that the merge can also use a query, and thus you don't have to merge
just "one" record..

After the merge occurs, you get a plain document WITHOUT any merge fields,
and this allows the end user to save, edit, or even email the document
(since the merge fields are gone after the merge occurs).

Give the above a try.

There is also options to print the document without saving.

MergeNoPrompts
strFromDocTemplate,[strDir],[bolFullPath],[strOutPutDoc],[strSql],[bolPrint],[strPrinter]

strFromDoc = name of template to merge from

strDir = optional - directory to use for templates

bolFullPath = optional - if true, then strDir above is NOT relative

strOutPutDoc = optional - this sets the name of the output document

(if not set, then when user saves the word doc,
then they will be prompted for the file name - this is the usual default
behavior)
strSql = optional - this can be any sql/query to provide
the data for the merge in place of the "one" current form.

bolPrint = optional - if set true, then the document is printed, and
THEN CLOSED. If you do not supply the strOutPutdoc, then the merged document
is closed and discarded.

strPrinter = optional - a printer string to send the print to. This
printer string does NOT change the currnet default printer. If left blank,
then the current default printer is used.

Another command that is used for "many" merge, and not just the current
record is
 
D

Dennis

Thanks! I'll look into your information more closely. The reason for the
bookmark instead of a merge-field is partly because of my own ignorance. I
need to insert some specific text at a specific point, but I don't want to
setup each document as a mail-merge file. In an Access table, I have the path
& filename to the document I want. My task is to open that single document,
insert the single line of text into a specific point in the file, and print.
Then close without saving. I was thinking that a bookmark in each document
would allow me to search for it (I'm assuming bookmarks have names, and the
name of the bookmark would be the same in EVERY document) and just do a
quickie insert from the table. I'm thinking that setting up a document as a
mail-merge file might be more work overall than just inserting text at the
bookmark.

Does that make sense?

Thanks again!




Albert D. Kallal said:
Any particular reason you are using book marks???

Using actual merge fields is far easier.

I have a working solution that lets you execute a word merge. You can choose
to print the word doc, and NOT save it....

The code is also designed to print the CURRENT record, OR YOU can throw a
query at the word doc, and it will print all of them (this is a big
advantage over that of book marks which is ONLY good for one record).

Furthermore, with bookmarks, you have to write code for each new document
you create (some of my clients would consider that dishonest that they call
me up for each new word doc they need to make a merge).

So, try the above sample:

The sample I have can be found here:
http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html

What is nice/interesting about my sample is that is specially designed to
enable ANY form with ONE LINE of code....

Thus, each time you build a new form, you can word merge enable it with
great ease.

Make sure you read the instructions from above, and you should eventually
get to the following page
http://www.members.shaw.ca/AlbertKallal/wordmerge/page2.html


Note that the merge can also use a query, and thus you don't have to merge
just "one" record..

After the merge occurs, you get a plain document WITHOUT any merge fields,
and this allows the end user to save, edit, or even email the document
(since the merge fields are gone after the merge occurs).

Give the above a try.

There is also options to print the document without saving.

MergeNoPrompts
strFromDocTemplate,[strDir],[bolFullPath],[strOutPutDoc],[strSql],[bolPrint],[strPrinter]

strFromDoc = name of template to merge from

strDir = optional - directory to use for templates

bolFullPath = optional - if true, then strDir above is NOT relative

strOutPutDoc = optional - this sets the name of the output document

(if not set, then when user saves the word doc,
then they will be prompted for the file name - this is the usual default
behavior)
strSql = optional - this can be any sql/query to provide
the data for the merge in place of the "one" current form.

bolPrint = optional - if set true, then the document is printed, and
THEN CLOSED. If you do not supply the strOutPutdoc, then the merged document
is closed and discarded.

strPrinter = optional - a printer string to send the print to. This
printer string does NOT change the currnet default printer. If left blank,
then the current default printer is used.

Another command that is used for "many" merge, and not just the current
record is
 
A

Albert D. Kallal

My task is to open that single document,
insert the single line of text into a specific point in the file, and
print.
Then close without saving.

Sure, just put in one merge field in the document, and then use my code. It
is certainly more easy then inserting a book mark.

You can also use code to insert text into a bookmark you placed in the
document. However this makes the code MORE difficult if you plan to print
more then one document at a time.

Hence, bookmarks are ok if you plan to only print ONE document at a time.

With using a real word merge field, you don't have to write code to insert
the text for each document, and you use a query with criteria (such as all
records with today's date).

Using word merge means you just launch my merge sample, create a new
document...then insert
the field name that has the text that you need, and save...

You are done....

About the only reason to use a book mark if you have a large block of text
with that requires line feeds (carriage returns) in the data...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top