Re: How to close the template or Document 1

Tech-Archive recommends: Fix windows errors by optimizing your registry



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




"Shauna Kelly" wrote:

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


"malycom" <malycom@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:06F2DD1B-D48F-4121-8AE4-9BED20D7A253@xxxxxxxxxxxxxxxx
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.



.


Quantcast