PDA

View Full Version : Solved: Form Fields in VBA



bryVA
08-07-2009, 10:13 AM
Hello,

I am trying to get a word document that is protected to take the information placed in a Drop Down form field and use that as part of the name when saving the document. I have the following code:

Sub Save_File()
Dim Today, FolderName$, DateValue$, NameofFile$, FullFileName$, NCC
Set aDoc = ActiveDocument
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
NCC = ActiveDocument.Bookmarks("Dropdown1").Range.Text
aDoc.Protect Type:=wdAllowOnlyFormFields, _
NoReset:=True
End If

FolderName$ = ActiveDocument.Path & "\"
DateValue$ = Format(Now, "yy-mm-dd hhmmss")
NameofFile$ = NCC
FullFileName$ = FolderName$ + NameofFile$ + " " + DateValue$

aDoc.SaveAs FullFileName$, _
FileFormat:=wdFormatDocument, LockComments:=False, _
Password:="", AddToRecentFiles:=True, WritePassword:="", _
ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
End Sub

This doesn't work because its not as it is not extracting the text from the drop down.

I also want to after saving the file to automatically print the file and then clear all the form fields. How would I do that?

I have attached a copy of what I am trying to do.

Thanks all,

-B

macropod
08-07-2009, 09:35 PM
Hi bryVA,

Try:
Sub Save_File()
Dim Today, FolderName$, DateValue$, FullFileName$
With ActiveDocument
NameofFile$ = .FormFields("Dropdown1").Result
FolderName$ = .Path & "\"
DateValue$ = Format(Now, "yy-mm-dd hhmmss")
FullFileName$ = FolderName$ + NameofFile$ + " " + DateValue$
.SaveAs FullFileName$, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, _
SaveFormsData:=False, SaveAsAOCELetter:=False
.PrintOut
If .ProtectionType <> wdNoProtection Then
If MsgBox("Clear Form?", vbYesNo) = vbYes Then
.Unprotect
.Protect Type:=wdAllowOnlyFormFields
End If
End If
End With
End SubNote the addition of the option to clear the form.

bryVA
08-10-2009, 07:32 AM
Thanks Macropod. That is exactly what I needed. Thanks for always helping me.

-B