Re: Creating Filename from Form Data

Tech-Archive recommends: Fix windows errors by optimizing your registry



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



.



Relevant Pages

  • Re: Word 2007 Save As
    ... has nothing at all to do with the filename you showed here. ... The macro will run only the first ... "Jay Freedman" wrote: ... should apply to all of Windows, not just Word and not just Word 2007. ...
    (microsoft.public.word.vba.customization)
  • Re: How to insert an image file into Work AND have the file name d
    ... "Jay Freedman" wrote: ... Selection.InsertAfter vbCr & FileName ... There's nothing the macro can do to find out what the others were. ... Picture> From File dialog, multiple-selected pictures don't get ...
    (microsoft.public.word.docmanagement)
  • Re: On exit command
    ... see the trouble I am having with spellchecking only certain form fields and ... the end of the spellcheck code to have it STOP THERE and then go on to the ... "Jay Freedman" wrote: ... there can be only one On Exit macro per field. ...
    (microsoft.public.word.vba.general)
  • Re: On exit command
    ... see the trouble I am having with spellchecking only certain form fields and ... the end of the spellcheck code to have it STOP THERE and then go on to the ... "Jay Freedman" wrote: ... there can be only one On Exit macro per field. ...
    (microsoft.public.word.vba.general)
  • Re: splitting file name and path
    ... "Jay Freedman" wrote: ... >> In a macro I've written I need to get the filename and path names ... >> that would read everything up to the last slash, no matter if the ...
    (microsoft.public.word.vba.general)