AutoNew or AttachAs "name??" to create file name from data

J

JBark

Hi,

Is it possible to use the AutoNew property to show a form when a template
file (.dot) is opened, have the user input text into three form fields, then
pass those three fields data to three bookmarks in the doc as well as create
the new doc file name when it is opened?

OR..

Is it possible to use some sort of AttachAs "file Name??" using three
bookmarks that when I click the submit button at the end of my document it
will attach to my Outlook message as ( Form "field1" "field2"
"field3".doc).

I'm already using the MVP script SendDocumentAsAttachment to create the
email subject line created from the three bookmarks in my doc. Now if I can
just get those three bookmarks/text fields to also create the new doc name
either when the doc is created or attached things will be golden. Any
suggestions?
 
D

Doug Robbins - Word MVP

If your document is a form that is protected for filling in forms, you could
use the following:

With ActiveDocument
.SaveAs .FormFields("Field1").result & .FormFields("Field2").result &
..FormFields("Field3").result & ".doc"
End With

Alternatively, you could have a Userform displayed when the document is
created from the template

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

and for more detail, the following pages of fellow MVP Greg Maxey's website
:

http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

JBark

Thanks Doug. Can a .SaveAs property write the file name as a temporary name
into memory though. I don't want the users to actually save the form to a
specific path. I really just want the name of the doc and the doc itself as
an attachment when it gets sent through email. Can this .SaveAs property be
integrated with SendDocumentAsAttached() property?
 
D

Doug Robbins - Word MVP

Better to just save the document, have the code send it and then use the
Kill command to delete the file.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

JBark

Kill Command? That's a new one to me! Do tell. I had the code written so that
the file name and the subject line of the email was pulled from the text
fields but then I was ending up with all these saved files all over the
place. I even setup a second command button in the doc set to .display
instead, so that the user can attach additional files if neccessary to the
same email. If I can add the kill code to this script it would be great. Can
you suggest where to put it in this?

Private Sub CommandButton1_Click()

With ActiveDocument
.SaveAs .FormFields("POrequired").Result & " " &
..FormFields("CLIENTrequired").Result & " " &
FormFields("PRODUCTrequired").Result & ".doc"
End With

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.Subject = "PO# " & ActiveDocument.FormFields("POrequired").Result _
& " " & ActiveDocument.FormFields("CLIENTrequired").Result _
& " " & ActiveDocument.FormFields("PRODUCTrequired").Result
'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With

If bStarted Then
oOutlookApp.Quit
End If

Set oItem = Nothing
Set oOutlookApp = Nothing

End Sub
 
J

JBark

Doug, I found a post you had with someone else where you recommended the kill
command and I added that to my command_click code with does an automatic
attachment then kills the file, however, it does not work when I try to
combine it with my Outlook code set to .display so that the user can attach
extra files to the email. This is the code with it set to .display. Any
suggestions? How do I get the code to "wait" until I press "send" in Outlook?
Do I need to make a separate sub command? Thanks.

**NOT WORKING CODE FOR DISPLAY MODE**

Private Sub CommandButton2_Click()

With ActiveDocument
.SaveAs .FormFields("POrequired").Result & " " &
..FormFields("CLIENTrequired").Result & " " &
FormFields("PRODUCTrequired").Result & ".doc"
End With

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "(e-mail address removed)"
.Subject = "PO# " & ActiveDocument.FormFields("POrequired").Result _
& " " & ActiveDocument.FormFields("CLIENTrequired").Result _
& " " & ActiveDocument.FormFields("PRODUCTrequired").Result
'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Display
End With

If bStarted Then
oOutlookApp.Quit
End If

Set oItem = Nothing
Set oOutlookApp = Nothing

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
Doc.Close wdDoNotSaveChanges
Kill DocPathName

End Sub
 
J

JBark

Actually - I just noticed the kill code is not really working. The code
closes the Word doc but the file is still written to the hard drive and not
deleted? What am I doing wrong?
 
J

JBark

OK - I know, I just keep replying to myself as I become enlightened.
SO...I know you can kill the file but it still remains saved to the hard
drive and not deleted - and I need it deleted. THEREFORE - is it possible to
have a File > Open command execute after the KILL command and use the
previous stored full path name of the file to be deleted to get rid of the
file, then reopen the template again like as if the user were to begin a new
document? Does anybody understand what I'm saying? I'm probably asking too
much of the program.
 
J

JBark

All I really need to know at this point is how I can get the kill command to
work with the .display mode instead of the .send mode. Is it possible? I
really appreciate your feedback. Thanks.
 
D

Doug Robbins - Word MVP

Here is what the VBA Help file says on it:

Kill Statement

Deletes files from a disk.

Syntax

Kill pathname

The required pathname argument is a string expression that specifies one or
more file names to be deleted. The pathname may include the directory or
folder, and the drive.

Remarks

In Microsoft Windows, Kill supports the use of multiple-character (*) and
single-character (?) wildcards to specify multiple files. However, on the
Macintosh, these characters are treated as valid file name characters and
can't be used as wildcards to specify multiple files.



--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

JBark

I guess I just don't know enough as to how to sequence everything together
properly so that the full file name with its path will be remembered so it
can be deleted when the kill statement is executed.
 
D

Doug Robbins - Word MVP

If you replace the following part of your code

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
Doc.Close wdDoNotSaveChanges
Kill DocPathName

with

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
MsgBox DocPathName

what does the message box display?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

JBark

It tells me the full path name of the saved file - which is a good thing. At
least I know the full path is being stored. I've also had a conversation with
Jay about this and he is convinced the code will not execute to kill the file
because the macro is tied to the document and once the document is closed,
the code stops executing so the kill statement nevers gets read. (Which I
understand) He suggested I assign the macro to a custom toolbar and I did but
then the macro will not run at all. If I click the toolbar button, I get a
blank message box and all it says is Microsoft Visual Basic with a red circle
x.
 
D

Doug Robbins - Word MVP

Please do not post the same problem to two different newsgroups.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

JBark

It wasn't really the same post. It started off in a completely different
direction, but it led me back to the same place - unavoidably. It certainly
was not intentional. As in this post ended up leading me in a different
direction than the name intended.

Anyhow - I finally achieved what I was looking for the macro to do and I am
posting it here so that someone may learn as I have. The code works great to
just send the file as an attachment, then it deletes the file once the code
is finished. It works great. And yes, it does need to be attached to a
toolbar outside of the document itself.

In case the person reading this post is wondering what this macro is doing,
here it is: The user fills in fields in a Word document, clicks a Submit
Button on the Toolbar which launches this macro, the macro does a Save As and
pulls data from three different fields in my document to create the file name
and the subject line of the email. It sets the Send To Address to a
predetermined email address. It attaches the word doc to the email, sends the
email through Outlook, then closes the Word doc and deletes it from your hard
drive. This way the user is not forced to keep all the word docs on their
hard drive or delete them later.

Here's the code. Of course you would have to replace my custom fields with
your own.

Sub SendRequestAsAttachment()

With ActiveDocument
.SaveAs .FormFields("POrequired").Result & " " &
..FormFields("CLIENTrequired").Result & " " &
..FormFields("PRODUCTrequired").Result & ".doc"
End With

Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem

On Error Resume Next

If Len(ActiveDocument.Path) = 0 Then
MsgBox "Document needs to be saved first"
Exit Sub
End If

Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If

Set oItem = oOutlookApp.CreateItem(olMailItem)

With oItem
.To = "insert an email address here"
.Subject = "PO# " & ActiveDocument.FormFields("POrequired").Result _
& " " & ActiveDocument.FormFields("CLIENTrequired").Result _
& " " & ActiveDocument.FormFields("PRODUCTrequired").Result
'Add the document as an attachment, you can use the .displayname property
'to set the description that's used in the message
.Attachments.Add Source:=ActiveDocument.FullName, Type:=olByValue, _
DisplayName:="Document as attachment"
.Send
End With

If bStarted Then
oOutlookApp.Quit
End If

Dim Doc As Document
Dim DocPathName As String
Set Doc = ActiveDocument
DocPathName = Doc.FullName
'Check that file exists
If Len(DocPathName) > 0 Then
'First remove readonly attribute, if set
SetAttr DocPathName, vbNormal
'Then delete the file
Doc.Close wdDoNotSaveChanges
Kill DocPathName
End If
End Sub

My next step from here is to figure out how to call an InputBox to send this
email with multiple other attachments selected by the user, because using
this macro in .Display mode does not work properly.
 

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