Re: Creating Filename from Form Data
- From: Andrew <andrew@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 22:25:00 -0700
can a similar macro be used to create file names from the files' "properties"
data?
--
agibson
"Jay Freedman" wrote:
Should I assume that Model, Serial, RNumber, and Customer are the names of.
the form fields -- what you see in the Bookmark box in the Properties
dialogs?
VBA looks at those words and assumes that they're VBA variables, not field
names. And since you haven't assigned any value to those "variables",
they're empty (that's the default value for Variant type variables).
Instead, you need to declare the variables in Dim statements and assign the
contents of the fields to those variables. It works like this:
Sub FileSave()
Dim MyModel As String, MySerial As String
Dim MyNumber As String, MyCustomer As String
MyModel = ActiveDocument.FormFields("Model").Result
MySerial = ActiveDocument.FormFields("Serial").Result
MyNumber = ActiveDocument.FormFields("RNumber").Result
MyCustomer = ActiveDocument.FormFields("Customer").Result
With Application.Dialogs(wdDialogFileSaveAs)
.Name = MyModel & " SN" & MySerial & " " & _
MyNumber & " " & MyCustomer
.Show
End With
End Sub
A properly designed macro would also check that the fields aren't empty, and
possibly validate them (for example, to make sure the serial number contains
7 digits and no other characters, or whatever the rules are).
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
jeffy wrote:
I have attached the code that I am attempting to use.
When I insert it into my Template, I get a blank filename when I
attempt to save. My bookmarks are checjked and double checked.
Sub FileSave()
With Application.Dialogs(wdDialogFileSaveAs)
.Name = Model & " SN" & Serial & " " &
RNumber & " " & Customer
.Show
End With
End Sub
"Jay Freedman" wrote:
jeffy wrote:
I have created a Doc Template in Word, and I would like to extract
data from Form fields and create a filename.
A Macro example I have found do not seem to work, OR I am having
difficulty modifying it successfully to extract the data from my
form fields. Is there something I am issing?
There's really no way for us to tell whether you're missing
something unless you post the relevant code for us to look at. Also
explain exactly what does happen vs. what you expect to happen. Do
you get the wrong filename, or no filename, or do you get an error?
If there's an error message, quote the message exactly, and tell us
which line of code is highlighted in the editor when that happens.
Before you go to that trouble, double-check that the field names you
use in the code exactly match the spelling of the "bookmark names"
in the Properties dialogs of the fields in the form.
If you can remember, also tell us where you got the macro example so
we can look at that.
Finally, this topic would be more appropriate in one of the
programming newsgroups such as microsoft.public.word.vba.beginners.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
- Prev by Date: Re: adding power point to word document.
- Next by Date: Re: Automatically creating file names for dicuments based on data
- Previous by thread: keyboard typing error on letter -p, -puts a dash before letter -p
- Next by thread: Re: Automatically creating file names for dicuments based on data
- Index(es):
Relevant Pages
|