Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want to create some required fields in word using VBA 2

Status
Not open for further replies.

1221314121

Technical User
Apr 12, 2005
13
AU
I’m trying, and currently failing, to setup a required field in an MS Word template using VBA.

I just want my MS Word template to check if all required fields are complete to allow save.

I wouldn’t mind a pop up box to let them know what they have missed

Any suggestions?

Cheer JADA
 
And what you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi PHV, thanks for the responce.

This is what i have so far. Its realy just a requiered field thats not so effective because you can still save without filling out the section. It's all ive been able to get going. Mind you i am by no means good with VBA

thanks for having a look:

--------------------

Private mstrFF As String

Public Sub AOnExit()
With GetCurrentFF
If Len(.Result) = 0 Then
MsgBox "You can't leave field: " & .Name & " blank"
mstrFF = GetCurrentFF.Name
End If
End With
End Sub

Public Sub AOnEntry()
Dim strCurrentFF As String

If LenB(mstrFF) > 0 Then
ActiveDocument.FormFields(mstrFF).Select
mstrFF = vbNullString
End If
End Sub


Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then

' CheckBox or DropDown
Set GetCurrentFF = .FormFields(1)

ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields _
(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function

-------------------

Cheers JADA

 
Hi JADA,

Can you give a bit more detail about your formfields - which ones have these macros set as entry and exit macros.

As an alternative, why not trap the document Save and don't let them save without first filling in your formfield(s) - it would probably be easier and more reliable.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Hi Tony,

I have a few 'text form fields' in succession
the first runs the macro on exit
and
the second runs the macro on entry

To give a little backround my template is an order form.
(and no i don't want it online)

You mention something about traping the document tony.
Could you or anyone please explain this concept as i am limmited in my understanding of VBA.

QUOTE "why not trap the document Save and don't let them save without first filling in your formfield(s) - it would probably be easier and more reliable."

 

So, when a new documentis created from your template, the first formfield is selected and when you exit from it, a check is made that it is not empty and a message box displayed if it is; when you then (try to) enter the second textbox, the first one will be reselected if it remains empty. What bit of that isn't working?

As for trapping the Document Save, just create a macro called FileSave and/or one called FileSaveAs. These will run instead of the built in commands and you can do what you like in them.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
To expand Tony's suggestion. Say you have a particular formfield (named Musthave, say), then use a function to evaluate the result, then use the function in both FileSave, and FileSaveAs to decide. Using the function to return a string allows you to have different messages to the user.
Code:
Function CheckField() As String

Select Case ActiveDocument.FormFields("MustHave").Result
  Case "" [COLOR=red]' the formfield is blank[/color red]
      CheckField = "The field MustHave is blank.  " & _
             "Please complete this field."
  Case "this is a good input"
      CheckField = ""
  Case "whatever"  [COLOR=red]' this is a BAD input[/color red]
      CheckField = "The input for MustHave is incorrect."
End Select
End Function

Sub FileSave()
If CheckField = "" Then
  ActiveDocument.Save
Else
  Msgbox CheckField
End If
End Sub

Sub FileSaveAs()
If CheckField = "" Then
  ActiveDocument.SaveAs [i]filename[/i]
  [COLOR=red]' OR use FileDialogs to display the SaveAs dialog[/color red]
Else  [COLOR=red]' it is blank or bad input, display message[/color red]
  Msgbox CheckField
End If
End Sub

here are a number of ways of going about this. It depends on where and how you are doing error trapping. Are you checking for valid input before the save?



Gerry
 
Thank you both so much

The only thing i can't get happening right now is the save as dialogue box to appeare

and one question Fumei - you show in your scripts that i can alternatively use the FieldDialogue to display SaveAs dialogue box ... is that what im after to allow me to save as to another location?

Thanks again Fumie and Tony you have gone above and beyond my expectations and if you could still help that would be great.

But if anyone could answer my question it would be much appreciated
 

You can display the SaveAs Dialog using
Code:
Application.Dialogs(wdDialogFileSaveAs).Show
or, if you have Word XP or Word 2003, you can also use
Code:
Application.FileDialog(msoFileDialogSaveAs).Show

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top