![]() |
|
#1
|
|||
|
|||
|
Hi,
I'm new here and also I'm new in working with Word forms. I have intermediate experience with Excel VBA. I attached Word file on which I'm working, but without success. What I want to do is set a restriction for drop-down form field. Is it possible at all to restrict each form field to be visible or not? If not, maybe it is possible to set each form field as inactive. E.x.: 1. If we choose A from drop down field then some form fields (bookmarks) are not visible. 2. If we choose B from drop down field then bookmark "bye" and "restriction" are not visible with formatting (line below "Bye") 3. If checked "yes" then all formfields (bookmarks) are not visible. As said I'm new and not sure what is difference between form field and bookmark. To do mentioned points should I use VBA code or can I just work on Cross-reference? Please advise me, I attached example file. Regards, AttiCuS |
|
#2
|
||||
|
||||
|
It is not possible to conditionally show/hide formfields. Doing that or conditionally enabling/disabling them would require a macro. Also, instead of Yes and No checkboxes, you should consider having a single dropdown with Yes/No choices. That way, there's no risk of both being checked. Field coding can be used to conditional show/hide your 'Bye' & 'Restriction' text.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Hi macropod,
Thank you for your replay. I tried to add simple VBA code, however without any success. What I want to do is: when I choose 'A' from 'drop_list' then in 'Text1" should appear text: 'xxx'. I used this code: Sub OnExitCB1B() If ActiveDocument.drop_list = "A" Then ActiveDocument.FormFields("Text1").Result = "xxx" End If End Sub But it doesn't work :-( Could you give an example how could look macro for enabling/disabling formfields? The case is that I have protected form to be filled. To start filling the template/form we have to choose one option from dropdown list. Then we get an info/text in the formfield below. And what is the most important depend by what we choose from dropdown some section in the document should be enabled/disabled to edit. What you can see each text, like 'Hello' or 'Bye' is added as formfield with disabled fill-in option. I was thinking also about adding text next to different heading. E.x.: when you choose 'A' from dropdown next to 'Bye" should appear text -'do not fill this section'. I'm just learning it and for now it is black magic for me ;-) I see that you have large experience in work with MS Word. I would really appreciate if you could show me the road how to solve it. Regards, AttiCuS |
|
#4
|
||||
|
||||
|
Here's some code to get you started. Note that I've included a reference to 'chbx_yes2', but the corresponding checkbox lacks the bookmark name.
Code:
Sub OnExitCB1B()
With ActiveDocument
Select Case .FormFields("drop_list").Result
Case "A"
.FormFields("Text1").Result = "xxx"
With .FormFields("Dropdown_yes_no")
.DropDown.Value = .DropDown.Default
.Enabled = False
End With
With .FormFields("chbx_yes2")
.CheckBox.Value = False
.Enabled = False
End With
With .FormFields("chbx_no2")
.CheckBox.Value = False
.Enabled = False
End With
Case "B"
.FormFields("Dropdown_yes_no").Enabled = True
.FormFields("chbx_yes2").Enabled = True
.FormFields("chbx_no2").Enabled = True
Case "C"
.FormFields("Dropdown_yes_no").Enabled = True
.FormFields("chbx_yes2").Enabled = True
.FormFields("chbx_no2").Enabled = True
End Select
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#5
|
|||
|
|||
|
Hi macropod,
Thanks, it works perfect! However, I have one more issue, which can't solve. Code:
Private Sub Document_Close()
Select Case ActiveDocument.FormFields("drop").Result
Case "Internal"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure that contract should be without benefits?", vbOKOnly
Cancel = True
End If
Case "Permanent"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure?", vbOKOnly
Cancel = True
End If
End Select
End Sub
Is it possible to run the same action if I click 'Save' - little disk in the left upper corner? Best regards, AttiCuS |
|
#6
|
||||
|
||||
|
You might try using a macro like:
Code:
Sub FileSave() Call Document_Close End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#7
|
|||
|
|||
|
Such a shame
that was so easy ![]() However, have one more question. I little modified my code insteady of vbOKOnly I used vbYesNoCancel. And now I don't know how to assign 'No' and 'Cancel'. If someone choose No or Cancel should go back to file with possible to continue filling the form. So, won't get question 'Do you want to save changes you made to...' Could you help me with that, as well? Anyway, I'm really grateful for your help so far ![]() Regards, AttiCuS |
|
#8
|
||||
|
||||
|
In that case, you might use something like:
Code:
Private Sub FileSave()
Call FieldCheck
End Sub
Private Sub Document_Close()
Call FieldCheck
End Sub
Private FieldCheck()
Select Case ActiveDocument.FormFields("drop").Result
Case "Internal"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure that contract should be without benefits?", vbOKOnly
End If
Case "Permanent"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure?", vbOKOnly
End If
End Select
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#9
|
|||
|
|||
|
Hi macropod,
Thank you for an example. However, it was not my intend. Now I saved my file as Microsoft Word Macro-Enabled Template. I want to use vbYesNoCancel buttons. But, I don't know how to assign each button. When someone choose 'Yes' the macro should let save the file, even if clicked 'Save' - little disk in left upper corner or want to close the file by pressing 'X'. But, if someone choose 'No' or 'Cancel' should go back to file with possible to continue filling the form. So won't get system question 'Do you want to save changes you made to...'. Code:
Private Sub Document_Close()
Select Case ActiveDocument.FormFields("drop").Result
Case "Internal"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure that contract should be without benefits?", vbYesNoCancel
Cancel = True
End If
Case "Permanent"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
MsgBox "Are you sure?", vbYesNoCancel
Cancel = True
End If
End Select
End Sub
Sub FileSave()
Call Document_Close
End Sub
|
|
#10
|
||||
|
||||
|
In that case you could use code like:
Code:
Private Sub Document_Close()
Dim Rslt As Long
Select Case ActiveDocument.FormFields("drop").Result
Case "Internal"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
Rslt = MsgBox("Are you sure that contract should be without benefits?", vbYesNoCancel)
Select Case Rslt
Case vbYes
Case vbNo
Case vbCancel
End Select
End If
Case "Permanent"
If ActiveDocument.FormFields("mob_ph_check").CheckBox.Value = False And ActiveDocument.FormFields("broad_check").CheckBox.Value = False And ActiveDocument.FormFields("car_check").CheckBox.Value = False And ActiveDocument.FormFields("vsp_check").CheckBox.Value = False Then
Rslt = MsgBox("Are you sure?", vbYesNoCancel)
Select Case Rslt
Case vbYes
Case vbNo
Case vbCancel
End Select
End If
End Select
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#11
|
|||
|
|||
|
Hi macropod,
You turned me to good way, as always ![]() However, still don't have any idea how to 'bite' it ;-) Code:
Select Case Rslt
Case vbYes
Me.Saved = True
Case vbNo
Exit Sub
End Select
When clicked little disk to save the file, I got question and this is ok. However when I chose button 'Yes' or 'No' nothing has happened. I want to make the action if I click X to close the file I got the question - and it works. Then when I choose 'Yes' the file should be saved but when choose 'No' I want to go back to file without system question if I want save the file and then continue filling template. The same action if I click little disk to save the file. Could you help me with that also? |
|
#12
|
||||
|
||||
|
Perhaps:
Code:
Private Sub Document_Close()
Dim Rslt As Long, bStatus As Boolean
With ActiveDocument
bStatus = (.FormFields("mob_ph_check").CheckBox.Value = False And _
.FormFields("broad_check").CheckBox.Value = False And _
.FormFields("car_check").CheckBox.Value = False And _
.FormFields("vsp_check").CheckBox.Value = False)
Select Case .FormFields("drop").Result
Case "Internal"
If bStatus = True Then
If MsgBox("Are you sure that contract should be without benefits?", vbYesNo) = vbYes Then .Save
Else
.Save
End If
Case "Permanent"
If bStatus = True Then
If MsgBox("Are you sure?", vbYesNo) = vbYes Then .Save
Else
.Save
End If
End Select
End With
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Macro to keep formatted form fields after mail merge or replace text with formatted form fields | jer85 | Word VBA | 2 | 04-05-2015 10:00 PM |
Form updating Bookmarks - writes to the bookmarks multiple times
|
PeterPlys | Word VBA | 13 | 01-14-2015 06:41 AM |
How To Find Bookmarks and/or custom fields Inside Building Blocks, Office 2010
|
thpanos | Word | 3 | 02-17-2013 08:49 AM |
Form Fields - Create blank form text field with if statement?
|
LAssist2011 | Word | 5 | 12-14-2011 03:02 PM |
| autopopulating REF fields + bookmarks | valz | Word | 10 | 09-01-2011 08:18 AM |