Looking for help with VBA. I have a form that is locked with drop down form boxes. There are many options in the drop down one being Please Select. I am looking for code so that when nobody has selected this box m when they try to save or send the document a message box appears asking them to fill it in. Only then should it save and send. Is there anyone who can help me here PLEASE.
[Solved] VBA: HELP needed for word VBA
- RichLeah
- Closed
-
-
-
Hi RichLeah and welcome,
You will need to create a class to handle events within your document.
Have a look at this pretty clear explanation.
http://www.mvps.org/word/FAQs/MacrosVBA/AppClassEvents.htmFor code to check your dropdown form control try something like this in the class,
Code
Display MorePrivate Sub WordApp_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean) Dim strName As String If CheckMyForm(strName) Then MsgBox "Please complete " & strName, vbExclamation ActiveDocument.FormFields(strName).Select Cancel = True End If End Sub Private Function CheckMyForm(Name As String) As Boolean Dim intIndex As Integer CheckMyForm = False For intIndex = 1 To ActiveDocument.FormFields.Count If ActiveDocument.FormFields(intIndex).Type = wdFieldFormDropDown Then If ActiveDocument.FormFields(intIndex).Result = ActiveDocument.FormFields(intIndex).DropDown.ListEntries(1).Name Then Name = ActiveDocument.FormFields(intIndex).Name CheckMyForm = True Exit Function End If End If Next End Function
It assumes the first entry in the dropdown list is the 'Please Select' style prompt that you do not want left unchanged by the user.Cheers
Andy -
I can not get this to work unfortunately. Wondering if you would be able to get it working from the attachment i have sent. I am also looking for it to pop up a box before it is sent as an attachment as well as the before save function. I do not have a lot of experience with VB and have just started to try to understand it. If you could do this and send it back it would be much appreciated.
Thanks
-
Hi,
I have sent you an example word doc that incorporates the code described.
Let us know if it solves your problem.Cheers
Andy -
Works perfect
Thanks
Very much
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!