Form Field and Save As

A

ArcticWolf

Hi,

I have a Word doc with a form field (user ID) in which the user enters their
unique user ID. Can I automate Word so when the user exits the document it
automatically saves the file name as their user ID?
IE - my unique ID is 12345 so the file name will need to be 12345.doc

Thanks in advance

AW
 
G

Graham Mayor

This can be achieved with a simple macro saved in the form template and run
preferably on exit from the UserID form field

Sub SaveForm()
Dim fName As String
fName = ActiveDocument.FormFields("UserID").Result & ".doc"
ActiveDocument.SaveAs fName
End Sub

This will automatically name the document as required. The user will get a
prompt to save the document if it is closed without saving.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

tmirelle

Is it possible to do this with data from two fields?

ie last name & first name?

so result would be lastfirst.doc
 
G

Graham Mayor

Yes you can do it with the data from two fields.where firstname and lastname
are the names of the associated form fields.

Sub SaveForm()
Dim fName As String
With Active Document
fName = .FormFields("LastName").Result
fName = fName & .FormFields("FirstName").Result & ".doc"
.SaveAs fName
End With
End Sub

Run on exit from whichever field is completed second of the two fields (or
from a later field)

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

Double click the field with the form unlocked and assign it to the Run Macro
on Exit property.

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
T

tmirelle

This is amazing! thanks!!

....can you force where the document should save? e.g the desktop?

& probably no way to avoid having pop up warning about the macro, right?
 
G

Graham Mayor

You can add a path to the filename and it will save to it. The macro warning
is always going to be an issue if you save the macros in a document. Save
the document as a template and create new documents from it.

Sub SaveForm()
Dim fName As String
With ActiveDocument
fName = .FormFields("LastName").Result
fName = fName & .FormFields("FirstName").Result & ".doc"
.SaveAs "C:\Path\" & fName
End With
End Sub

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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jatman

i am trying to adapt the below code for Word 2007. i have tried the
following but to no success.

changed the .doc to .docx (office 2007 standard for saving) but when i run
the macro, i get an error and the fname line is highlighted yellow, when i go
to the line, it shows fname=""

i changed the .FormFields to .Bookmarks, then i get an erro also, but the
..Result is highlighted.

i have reviewed other sources, but the macro that is listed here is more or
less what i am looking for - simple.

any other suggestions on this is appreciated.

jat
 
G

Graham Mayor

The macro works with Word 2007, assuming of course that there is content in
the UserID (legacy field) text field. The following will error trap no
content:

Sub SaveForm()
Dim fName As String
On Error GoTo Noname
With ActiveDocument
fName = .FormFields("UserID").Result & ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault
End With
Exit Sub
Noname:
MsgBox "UserID field is empty"
End Sub

If you want to use the content of a bookmark then

Sub SaveForm()
Dim fName As String
On Error GoTo Noname
With ActiveDocument
fName = .Bookmarks("UserID").Range.Text & ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault
End With
Exit Sub
Noname:
MsgBox "UserID bookmark is empty"
End Sub

or you could use the built-in document properties

Sub SaveForm()
Dim fName As String
On Error GoTo Noname
With ActiveDocument
fName = ActiveDocument.BuiltInDocumentProperties("Author") & ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault
End With
Exit Sub
Noname:
MsgBox "Author field is empty"
End Sub


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jatman

the first two did not work (form fields and bookmarks,) probably on how i am
setting up the fields or something. so i tried the last one using the built
in document properties thinking it would not work that way i was hoping, and
it worked like a charm. from here, it should be a breeze.

thank you!
 
J

jatman

thought i was doing well, but one more question.

how do i join two built in document properties? i am using the Publish Date
and Title. i tried the following:

With ActiveDocument
fName = activedocument.builtindocumentproperties("Publish Date")
fName = fname & " " & ActiveDocument.BuiltInDocumentProperties("Title")
& ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault

i am not sure, but i think it has to do with the date. when i use the Title
alone, it worked. i added the date line to it and now i'm stuck. so i tried
the publish date alone and i get the "empty field error"

the Publish Date uses the date picker, properties display it as dddd,
mmmm-dd-yyyy, and it stores XML contents as Text (same as display.) i tried
the store as date also and no luck.

i really do not care as to how it shows, but the file should save as
"yyyy-mm-dd title of document"

thank you for your assistance so far!

jat
 
G

Graham Mayor

There is no 'built-in document property' called "Publish Date"
You could use the current date eg

With ActiveDocument
fName = Format(Date, "d" & _
Chr(32) & "MMMM" & Chr(32) & "yyyy") & _
.BuiltInDocumentProperties("Title") & ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault
End With

or you could create and use a custom document property called Publish Date
then

With ActiveDocument
fName = .CustomDocumentProperties("Publish Date") & _
.BuiltInDocumentProperties("Title") & ".docx"
.SaveAs fName, FileFormat:=wdFormatDocumentDefault
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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

Top