Creating new document with specific filename

K

Kailash

Hi All,

I'm trying to use VBA to create new documents from a Form with a
filename from one of the fields in the documents. Any idea how to do
that?

current code:
With ActiveDocument.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
End With
.Execute Pause:=True
End With


Thanks
Kailash
 
M

Malcolm Smith

All you need to do is to get the name of the new file from the form.
Now, you also may want to consider what happens if the file already exists
with that name.

In fact you will want to work on this scenario but basically you will want
something like:


Sub CreateNewDocument (sFileName as string)

dim oDocument as Document

set oDocument = Documents.Add
oDocument.SaveAs sFileName

End Sub


You will perhaps also want to consider which template to create the new
document from; even if it's a blank document (personally I never create
anything based on Normal.Dot; even blank documents get based on my
Blank.dot).

There are loads of design issues I have now left you with. But, this
ought to start you off.

Malc
www.dragondrop.com
 
K

Kailash

This is if I want to automatically save the file; but I would only
like to force word to create the new document with the special
filename instead of the default "Catalog1" or "Catalog2" etc, without
saving it.

Any idea?

Thanks
Kailash
 
M

Malcolm Smith

Unless I misunderstand; my code will enable you to create a new document
and then save it with whatever name you want.

If you want to change the caption from the default "Document1" (or
whatever your local variant may be) then I am positive that you have to
save it first.

If I am misunderstanding you then please accept my apologies and explain
again where I am going wrong. Am blaming this on a totally sleepless
night last night.

Cheers
- Malc
www.dragondrop.com
 
K

Kailash

You are right; the code will allow me to create the new document and
then save it with a specific name automatically.

However, I only wanted to create a new document with the caption name
changed from the default "Document1" to a name generated from one or
two fields in the form itself. FYI - I usually need to carry some
additional selection on the created form before saving it in a
specific directory. I only wanted the saving name to be automatically
generated to save time.

Thanks
Kailash
 
M

Malcolm Smith

If you know what the name of the document is going to be then why not code

ActiveDocument.SaveAs sFileName

where sFileName is derived from a text box or form control, and where this
code runs on an event when the user leaves the form control or if it's a
user form when the user clicks on the OK button.

- Malc
www.dragondrop.com
 
C

Cindy M -WordMVP-

Hi Kailash,
However, I only wanted to create a new document with the caption name
changed from the default "Document1" to a name generated from one or
two fields in the form itself. FYI - I usually need to carry some
additional selection on the created form before saving it in a
specific directory. I only wanted the saving name to be automatically
generated to save time.
1) It sounds like you're talking about mail merge? With which version
of Word?

2) Do I understand correctly:
- you do NOT want to save the merge result document
- you do want to pre-set a default File/Save as name for the document,
and also have this appear in the title bar of the document window?

3) Are you executing the merge to a new document using code? Or is the
user doing this?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
K

Kailash J

Hi Cindy,

You are perfectly right; I'm using Office 2000 and a little code was made to
automatically carry out the mail merge for the selection record. All I
wanted to add was a particular File/Save as name which is derived from one
field in the form.

Can you help me on that?

Thanks

Kailash
 
C

Cindy M -WordMVP-

Hi Kailash,
You are perfectly right; I'm using Office 2000 and a little code was made to
automatically carry out the mail merge for the selection record. All I
wanted to add was a particular File/Save as name which is derived from one
field in the form.

Can you help me on that?
I think so :)

1) With Office 2000 you have to assume that, after the merge, the
ActiveDocument object is the merge result. Normally, this will be the case.
But if your app is "sharing" the system with a user who might be working in
Word at the same time, this may not be the case. Then, you'd have to check
that ActiveDocument meets some kind of criteria to be sure you have the right
document.

But for the moment, let's assume ActiveDocument is merge result.

2) You also want to be sure you've assigned an object variable to the main
merge document, so that you can still communicate with it reliably once the
merge has taken place.

3) Right off the top of my head (untested, that means), the kind of code you
need would look roughly like this:

dim docMain as Word.Document
Dim docResult as Word.Document

Set docMain = Documents.Open("PathInfoForMainmergeDoc")
With docMain.MailMerge
.Destination = wdSendToNewDocument
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
With .DataSource
.FirstRecord = .ActiveRecord
.LastRecord = .ActiveRecord
End With
.Execute Pause:=True
Set docResult = activeDocument
docResult.SaveAs FileName:= _
"C:\My Documents\" & _
docMain.DataSource.DataFields("xyz").Value & _
"abc.doc"
.Close wdDoNotSaveChanges
End With

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep 30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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