Creating Filename from Form Data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
jeffy said:
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.
 
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
 
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).
 
Your code looks great. I have inserted it as a macro, and double checked all
of the Bookmarks/Variable assignments against my template to be sure they are
correct.

Now it seems to do the standard First Line of Text filename when I select
Save As.

I appreciate your patience as I stretch my wings here.

Regards,

Jeff
 
Got it!!! I am using FileSave not FileSaveAs.

Works Great...Thanks again!!!

Jeff

jeffy said:
Your code looks great. I have inserted it as a macro, and double checked all
of the Bookmarks/Variable assignments against my template to be sure they are
correct.

Now it seems to do the standard First Line of Text filename when I select
Save As.

I appreciate your patience as I stretch my wings here.

Regards,

Jeff
 

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