Mailmerge to a .dot file or ??

D

dixie

I create letters by mailmerging data from Microsoft Access into a .doc file
that I have set up to do this. When I do this, it is from inside an Access
mdb. The code basically looks like this:

Dim objword As Word.Document

Set objword = GetObject C:\letters\letter.doc, "Word.document")

objword.Application.Visible = True

objword.Mailmerge.Execute

objword.ActiveWindow.WindowState = wdWindowStateMaximize

Set objword = Nothing



The letter.doc file has a data source which points to a text file that
Access also sends out at the same time.



When this operates, two word files open individually, one is usually called
someting like forms1.doc and is the merged letter ready to either alter or
print. When that has been done and you close it, there is a 2nd word
document open - the original merge letter with the field in it. When I go
to close this, it asks me if I want to save it.



Now, what I want to happen is have only the finished letter open so it can
be printed, but I don't wish to have the actual merge letter open as some
users unfortunately do things like alter it or save it to another name or
generally interfere with it which they should not b able to do. Making it
read only is not a solution.



How do I overcome this difficulty? Is there some different code I can use
in the Access module that will not open the merge document at all, or open
it so it is not visible and then automatically close it before the merged
document is closed. I am a total novice at programming Word, but can you
program Word to close it down automatically. Or can I have these merge
letters as .dot templates and have only the merged file open - if so how do
I do that.



Largely, as you can see, my total lack of expertise in this area is showing
and I just know what I need to achieve, but how eludes me entirely.



Can anyone please help?



dixie
 
D

Doug Robbins - Word MVP

Hi Dixie,

Save letter.doc as a template and use the following procedure:

Dim WordApp As Object
Dim WordDoc As Object

On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error GoTo 0
'Create a new document from the template
Set WordDoc = WordApp.Documents.Add("insert template path\name here")

'Balance of your code here to execute the merge

CreateWordApp:

Set WordApp = CreateObject("Word.Application")
Resume Next

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dixie

Thanks Doug, I've had a muck around with that and have created a template
called letter1.dot.

The full code I am using at this stage is as follows:

Private Sub btnNewWord_Click()
'The following code is experimental only from Doug Robbins
Dim WordApp As Object
Dim WordDoc As Object

On Error GoTo CreateWordApp
Set WordApp = GetObject(, "Word.Application")
On Error GoTo 0
'Create a new document from the template
Set WordDoc = WordApp.Documents.Add("c:\lettertemplates\Letter1.dot")

'Balance of your code here to execute the merge

WordDoc.Application.Visible = True
WordDoc.Mailmerge.Execute
Set WordDoc = Nothing
Set WordApp = Nothing

CreateWordApp:

Set WordApp = CreateObject("Word.Application")
Resume Next

End Sub

What is now happening is that I am still getting 2 word documents opening,
one called FormLetter1.doc which is the merged doc file and a 2nd called
Document1.doc which is a mailmerge document (the codes are visible in it).
I am also getting a Resume without error message when the code runs, after I
return to Access from the Word docs.

I am obviously still doing something wrong as I am still getting 2 documents
opening instead of 1? Can you see the problem in the code above. It is
most likely in the way I execute the merge, but I am not sure. What I want
is for the merged document being the only one visible to the user.

dixie
 
D

dixie

I have been doing a bit of research on the web and a bit of experimentation
and the following code _seems_ to do the job. Could you please comment on
whether it is good or has problems

Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
'create and instance of word
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add("c:\lettertemplates\letter1.dot")
'perform mailmerge
wrdDoc.MailMerge.Execute
'close the original form document
wrdDoc.Saved = True
wrdDoc.Close False
'release references
Set wrdDoc = Nothing
Set wrdApp = Nothing

The other thing I want to ask is with Windows XP, if the templates were on a
network drive, would every user have access to the letters, or would the
fact they were created by someone else prevent them from opening?

Thanks for you help

dixie
 
D

Doug Robbins - Word MVP

Hi Dixie,

I should have mentioned about using

wrdDoc.Close wdDoNotSaveChanges

as well to get rid of the mailmerge maindocument.

As long as you have the correct path to the template in the code and the
users have access to that location, there should not be a problem.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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