How to close the template or Document 1

M

malycom

Hi

Someone has very kindly helped with some code which I need to ammend in
order to be complete but I am not sure how to do it.....I have tried many
various possiblities.

First thing that happens when a certain document opens is a form appears
allowing a user to select a template.

This is the code behind one of the buttons on the form.

Private Sub CommandButton1_Click()
Word.Documents.Add Template:="G:\Template\ent_test.dot"
Me.Hide
ThisDocument.Close
Document1.Close
End Sub

This works great and opens a new document based on the template closing the
original document.

This is the code behind the template which fills certain fields with merged
data which works perfectly.

Sub AutoNew()
With ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
End Sub

My only problem is, once the template opens and the macro is run, although a
correct document opens, the template or at least a document called Document1
is also open. Ideally, I would like that document to close without saving
changes only leaving the document with the merged data open.

The reason why is it is giving the user to many options for confusion when
closing multiple documents.

I hope you understand what I am asking as it is not easy to explain.
 
S

Shauna Kelly

Hi Malycom

Can we back up a few steps here?

First, am I right in assuming that your overall aim is to allow the user
to do something that then allows the user to choose a template and then
your code will create a new document based on the template the user
chose?

You say:
when a certain document opens is a form appears allowing a user to
select a template

Do you specifically want the user to do File > New and open a specific
document in order to get the opportunity to choose the template? It
seems like an odd way to go about it. Is there some reason you chose to
achieve your aim in this way?


Second, in your code, you have:
Document1.Close

Have you created a variable named Document1? Do you know what this
Document1 is? Or is it just referring to the document that Word
(temporarily) names "Document 1"?

Finally, what version of Word are you using?

Shauna

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
M

malycom

Hi Shauna

Firstly I'll try and make it a little clearer. I appologise for the length
of the explanation in advance.

Currently we use inhouse software that has a "Letter" button. This letter
button when clicked will save a .dbf file into the root of the users C drive
named ENTMRG.DBF which contains all the fields of the form that the user was
viewing when they pressed the "Letter" button.

The inhouse software immediately opens a window allowing the user to pick a
document to open which would pull the data from the saved DBF file but our
inhouse software doesn't work as we would like it meaning I am ammending how
we can achieve our goals.

So, I have created a blank document which is THE document a user will select
from the window mentioned above. I have added a form to this document which
immediately activates. This form is going to allow the user to select
pre-written letters whcih will be saved as templates with Merge Fields
entered in the correct places which will match up to the DBF file.

The code behind each button will point to a template, an example of the
first button code being this below. By the way, where it said Document1
earlier, I had mistakingly left it in the code as I was trying to get this to
work by trying different things. The correct code is below.

Private Sub CommandButton1_Click()
Word.Documents.Add Template:="G:\Template\ent_test.dot"
Me.Hide
ThisDocument.Close
End Sub

(Not user if it needs both Me.Hide and ThisDocument.Close as I haven't tried)

Now, this opens a new document based on the template ent_test.dot and then
closes the current document which is fine, but what happens next is the code
in the template runs, that code being

Sub AutoNew()
With ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
End Sub

What appears to happen next is that this opens a document called Document 1
and closes the template, but then Document 1 opens the finalised Merged
document called Letter1 or something along those lines with all the correct
data displayed. Document 1 stays open with things such as Dear <<PRETITLE>>
<<LASTNAME>> and Letter 1 will say Dear Mr Smith.

I am trying to place some code somewhere so that as soon as the document
showing the correct data, ie: Dear Mr Smith is open, the other document,
Document 1 with Dear <<PRETITLE>> <<LASTNAME>> closes without the need to
save changes, thus ensuring that our users only have to deal with the nicely
merged document and not get confused when they see another document that
prompts for saving on closing.

I hope this clears things up a bit, although if you understand that lot, you
deserve a round of applause. I wrote it and I'm confused :)

Thanks in advance

Malcolm
 
J

Jean-Guy Marcil

malycom said:
Hi Shauna

Firstly I'll try and make it a little clearer. I appologise for the length
of the explanation in advance.

Currently we use inhouse software that has a "Letter" button. This letter
button when clicked will save a .dbf file into the root of the users C drive
named ENTMRG.DBF which contains all the fields of the form that the user was
viewing when they pressed the "Letter" button.

The inhouse software immediately opens a window allowing the user to pick a
document to open which would pull the data from the saved DBF file but our
inhouse software doesn't work as we would like it meaning I am ammending how
we can achieve our goals.

So, I have created a blank document which is THE document a user will select
from the window mentioned above. I have added a form to this document which
immediately activates. This form is going to allow the user to select
pre-written letters whcih will be saved as templates with Merge Fields
entered in the correct places which will match up to the DBF file.

The code behind each button will point to a template, an example of the
first button code being this below. By the way, where it said Document1
earlier, I had mistakingly left it in the code as I was trying to get this to
work by trying different things. The correct code is below.

Private Sub CommandButton1_Click()
Word.Documents.Add Template:="G:\Template\ent_test.dot"
Me.Hide
ThisDocument.Close
End Sub

(Not user if it needs both Me.Hide and ThisDocument.Close as I haven't tried)

Now, this opens a new document based on the template ent_test.dot and then
closes the current document which is fine, but what happens next is the code
in the template runs, that code being

Sub AutoNew()
With ActiveDocument.MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
End Sub

What appears to happen next is that this opens a document called Document 1
and closes the template, but then Document 1 opens the finalised Merged
document called Letter1 or something along those lines with all the correct
data displayed. Document 1 stays open with things such as Dear <<PRETITLE>>
<<LASTNAME>> and Letter 1 will say Dear Mr Smith.

I am trying to place some code somewhere so that as soon as the document
showing the correct data, ie: Dear Mr Smith is open, the other document,
Document 1 with Dear <<PRETITLE>> <<LASTNAME>> closes without the need to
save changes, thus ensuring that our users only have to deal with the nicely
merged document and not get confused when they see another document that
prompts for saving on closing.

I hope this clears things up a bit, although if you understand that lot, you
deserve a round of applause. I wrote it and I'm confused :)

Try this AutoNew instead:


Sub AutoNew()

Dim doctemp As Document

Set doctemp = ActiveDocument

With doctemp
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
.Close wdDoNotSaveChanges
End With

End Sub
 
M

malycom

Jean-Guy

Thank you very much. That works a treat.

Now, if you can answer one last final question I will name a planet after
you....or at least be very greatful.

Although the Document 1 form closes and the user is left with only the
merged document which is fantastic, there are still 2 save prompts. One for
the merged document and one for the template. It doesn't make any difference
whether they save the template or not as it is not affected, but is there a
way to do away with the prompt to save the template.

Regardless of whether you can help with this, thanks very much for your
previous response which was excellent.

Regards

Malcolm
 
J

Jean-Guy Marcil

malycom said:
Jean-Guy

Thank you very much. That works a treat.

Now, if you can answer one last final question I will name a planet after
you....or at least be very greatful.

Although the Document 1 form closes and the user is left with only the
merged document which is fantastic, there are still 2 save prompts. One for
the merged document and one for the template. It doesn't make any difference
whether they save the template or not as it is not affected, but is there a
way to do away with the prompt to save the template.

Try this instead:

Sub AutoNew()

Dim doctemp As Document

Set doctemp = ActiveDocument

With doctemp
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
ThisDocument.Saved = True
.Close wdDoNotSaveChanges
End With

End Sub
 
M

malycom

Hi Jean-Guy

I have made the changes you suggested but although there is only one
document open, there are still 2 prompts to save, 1 for the .doc and 1 for
the .dot.

I am happy with the way the rest xof the script is working and I am not to
worried about the prompts so thanks you very much for your help.

Kind regards

Malcolm
 
J

Jean-Guy Marcil

malycom said:
Hi Jean-Guy

I have made the changes you suggested but although there is only one
document open, there are still 2 prompts to save, 1 for the .doc and 1 for
the .dot.

I am happy with the way the rest xof the script is working and I am not to
worried about the prompts so thanks you very much for your help.

Which ".dot" is being prompted for a Save action? The one that contains the
AutoExec code?

If so, try this version instead:

Sub AutoNew()

Dim doctemp As Document

Set doctemp = ActiveDocument

With doctemp
With .MailMerge
.MainDocumentType = wdFormLetters
.OpenDataSource Name:= _
"C:\ENTMRG.DBF", ConfirmConversions:=False
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
End With
.AttachedTemplate.Saved = True
.Close wdDoNotSaveChanges
End With

End Sub

If it still does not work, then it means you have some other code in the
..dot that makes changes after the AutoExec code runs.
 
M

malycom

Hi

It's wierd Jean-Guy

One of my document templates works fine in that it closes with only 1 save
prompt for the merged letter, but another with exactly the same code embedded
still asks for 2 save prompts, one being the merged document and one being
the template, which has actually closed, well at least is not showing in the
task bar or anything.

Thanks for all your help though.

Malcolm
 
J

Jean-Guy Marcil

malycom said:
Hi

It's wierd Jean-Guy

One of my document templates works fine in that it closes with only 1 save
prompt for the merged letter, but another with exactly the same code embedded
still asks for 2 save prompts, one being the merged document and one being
the template, which has actually closed, well at least is not showing in the
task bar or anything.

If this behviour is constant, then, even if you think that your templates
are identical code-wise, there are differences! Different behaviour means
different code. Look carefully, you will definitely end up finding a
difference somewhere. Sometimes, the difference is in the environment. Try to
exactly recreate the behaviour under the same circumstances. Make sure no
other documents are currently opened... This could interfere.
 

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