specfic file name macro from form fields entries

T

TBolt

I want a document to automatically have the file name entered when an
employee clicks save or the save as. The file name will be a combination of
two form fields (Text6 and Text70). Is there a way to get the entries from
these two fields and append them into one file name?

I also would like to these save at a specific location on a network drive.

I have no experience in VB.

Word 2003



Any help would be appreciated!
 
T

TBolt

Thanks, but it does not seem to work. I am wanting to take what is entered
into Text6 (Name) and Text70 (Department) field and combine those entries
into an auto filename eg. JohnWarehouse.
I have searched and found code that does close to what I want. It uses one
form field to make a file name where as I want two form fields.


Sub FileSave()

If ActiveDocument.Type = 0 And ActiveDocument.Name =
ActiveDocument.FullName Then

If Len(Trim(ActiveDocument.FormFields("Text6").Result)) = 0 Then

MsgBox "You must enter a name into the form field", vbOKOnly +
vbExclamation, "Error"

Exit Sub

End If

With Application

With .Dialogs(wdDialogFileSummaryInfo)

.Title = ActiveDocument.FormFields("Text6").Result

.Execute

End With

With .Dialogs(wdDialogFileSaveAs)

If .Display = True Then .Execute

End With

End With

Else

ActiveDocument.Save

End If

End Sub



Sub FileSaveAs()

If ActiveDocument.Name = ActiveDocument.FullName Then

Call FileSave

Else

With Application.Dialogs(wdDialogFileSaveAs)

If .Display = True Then

.Execute

End If

End With

End If

End Sub
 
G

Greg Maxey

TBolt,

You can adapt that code easy enough:

Sub FileSave()
Dim myString As String
Dim oDoc As Word.Document
Dim oFF As FormFields
Set oDoc = ActiveDocument
Set oFF = oDoc.FormFields
If oDoc.Type = 0 And oDoc.Name = oDoc.FullName Then
If Len(Trim(oFF("Text6").Result)) = 0 Or _
Len(Trim(oFF("Text70").Result)) = 0 Then
MsgBox "You must enter text into both formfields 6 and 70.", _
vbOKOnly + vbExclamation, "Error"
Exit Sub
End If
myString = oFF("Text6").Result + oFF("Text70").Result
With Application
With .Dialogs(wdDialogFileSummaryInfo)
.Title = myString
.Execute
End With
With .Dialogs(wdDialogFileSaveAs)
If .Display = True Then .Execute
End With
End With
Else
oDoc.Save
End If
End Sub
 
T

TBolt

That works very good but I did notice it only works when only the "save" is
clicked and not when "save as" or when someone would close or click the (x)
to close. Is it possible to assign the code to a command button to save. I
tried putting the code in the click event of the command button and it did
nothing.
Thanks in advance
 
G

Greg Maxey

TBolt,

I don't think you want to use the built in FileSave and FileSaveAs commands
for this purpose. Consider what would happen if you wanted to save a file
that didn't have a formfield Text6 and Text70.

You might consider assigning this code to a menu, toolbar, or macrobutton:

Sub myFileSave()
Dim myString As String
Dim oDocName As String
Dim oDoc As Word.Document
Dim oFF As FormFields
Set oDoc = ActiveDocument
Set oFF = oDoc.FormFields
If Len(Trim(oFF("Text6").Result)) = 0 Or _
Len(Trim(oFF("Text70").Result)) = 0 Then
MsgBox "You must enter text into both formfields 6 and 70.", vbOKOnly +
vbExclamation, "Error"
Exit Sub
End If
myString = oFF("Text6").Result + oFF("Text70").Result
oDocName = oDoc.Name
oDocName = Left(oDocName, InStrRev(oDocName, ".") - 1)
If oDocName <> myString Then
With Application
With .Dialogs(wdDialogFileSummaryInfo)
.Title = myString
.Execute
End With
With .Dialogs(wdDialogFileSaveAs)
.Name = myString
.Show
End With
End With
Else
oDoc.Save
End If
End Sub
 
T

Tbolt

So I could add this code to a cammand button named "Save" (macro button
the same as a command button?)and it would bring up the dialog box with the
entries from Text6 and Text70 as the file name?

I am thinking that if I put a command button on the form for people to click
it they would not like click save as on the menue or press the X.

I am completely new at this VB and am finding it intresting but complex.
Have any suggestions on a good book for begginers?
 
G

Greg Maxey

TBolt,

If the macro were assigned to a menu command, toolbar command, or
macrobutton it would perform the same action that it performs now. That is,
if the file name doesnt' match the text in Text6 and Text70 it will display
the SaveAs dialog. To see the code at work, you can step through it line
for line using F8.

See these links:

http://word.mvps.org/FAQs/Customization/AsgnCmdOrMacroToToolbar.htm and
http://word.mvps.org/faqs/tblsfldsfms/UsingMacroButton.htm
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

I am not completely new at VBA and like you I also find it complex ;-). I
have never read a book on VBA. Reading the articles on Macors in the Word
MVP FAQ site is a good place to start.
 

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