Automatically Naming a document

  • Thread starter Thread starter A_Classic_Man
  • Start date Start date
A

A_Classic_Man

I posted this on another group and did not get a response so I'll try
here.

I have a Word form with several fields one of which is a date field. I
would like the date entered in
the date field to show as the name of the form when the form is
emailed as
an attachment and/or saved. How is this accomplished?

Thanks in advance

Ron
 
I posted this on another group and did not get a response so I'll try
here.

I have a Word form with several fields one of which is a date field. I
would like the date entered in
the date field to show as the name of the form when the form is
emailed as
an attachment and/or saved. How is this accomplished?

Thanks in advance

Ron

For the Save part, see
http://www.word.mvps.org/FAQs/MacrosVBA/SetDefFilename.htm.

I don't know whether that will suffice for the email attachment or whether
you'll need a separate macro for that.
 
The big problem you are likely to encounter, whether you use a macro or the
method Jay pointed to is the question of reserved characters that are likely
to form part of the date. For example you would not be able to save a
document with the name 24/6/2008 - though 24 June 2008 would work. With this
proviso, assuming the field with the date is bookmarked Text1 - the
following macro run on entry to the next form field will initially save the
document with the name as the content of Text1

Sub Save_As_Date()
Dim oFld As FormFields
Dim sFname As String
Set oFld = ActiveDocument.FormFields
sFname = oFld("Text1").Result
MsgBox sFname
ActiveDocument.SaveAs sFname
End Sub

This will raise another issue relating to the use of macros at a remote
site, where you have no way of forcing the user to run macros contained in a
document. If you are going the whole hog and distributing a template then
you could add a button to send the document to you. The following macro uses
Outlook to send the document as attachment to the address entered having
saved the document with the required filename (subject to the same provisos
as above - plus the user has to be using Outlook)

Sub SendDocumentAsAttachment()
Dim bStarted As Boolean
Dim oFld As FormFields
Dim sFname As String
Set oFld = ActiveDocument.FormFields
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
If Len(ActiveDocument.Path) = 0 Then 'Document has not been saved
'Save the document
sFname = oFld("Text1").Result
ActiveDocument.SaveAs sFname
End If
'see if Outlook is running and if so turn your attention there
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then 'Outlook isn't running
'So fire it up
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Open a new e-mail message
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem 'and add the detail to it
.To = "(e-mail address removed)" 'send to this address
.Subject = ActiveDocument.name 'This is the message subject
.Body = "See attached document" ' This is the message body text
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue
.Send
'**********************************
'If you want to view the message before it goes
'change the line above from .Send to .Display
'Otherwise the message is sent straight to the Outbox
'and if you have Outlook set to send mail immediately,
'it will simply be Sent
'with no obvious sign that Outlook has operated.
'Apart from the copy in the Outlook Sent folder
'**********************************
End With
If bStarted Then 'If the macro started Outlook, stop it again.
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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

Back
Top