PDA

View Full Version : Form field drop downs with yes or no that controls a macro



Jeffhagel
09-08-2013, 11:59 AM
I have a 40 page word document that i would like to shorten when particular sections dont apply. I have everything locked so users complete only form fields. I wrote a very simple macro to unlock the document select all the text in a table and hide the contents then relock the file. The challenge i now have is how to run this macro. I havent been able to figure out how to connect it to a simple form field drop down so that it runs if "yes" or "no" is selected.

I welcome any tips

gmaxey
09-08-2013, 02:12 PM
Assign your macro to the dropdown OnExit event.

Jeffhagel
09-08-2013, 03:47 PM
Assign your macro to the dropdown OnExit event.

I am honoured that you took the time to reply. Im sorry my initial question maybe wasnt clear enough.

What i have is a macro to hide the text if a form user selects "no" from the drop down and a macro that unhides the text if a form user selects "yes". I thought it would be a good idea to build in a reversal as im sure users will change their mind time to time.

So i was hoping for guidence on adding this if/then scenario into my macro

gmaxey
09-09-2013, 04:24 AM
Sub MacroAssignedToExitEvent()
'A basic Word macro coded by Greg Maxey
If ActiveDocument.FormFields("Dropdown1").Result = "Yes" Then
'Your code to hide the text
Else
'Your code ot unhide the text
End If
End Sub

Jeffhagel
09-09-2013, 10:16 AM
Greg I again thank you so much for taking the time to answer my question....

below is the code that I am attempting to use (I think I incorporated your code correctly. Unfortunately I not getting the results I need and I don’t know why as my crude code for hiding or unhiding a table works when not inserted into your dropdown results code.

As you can see by my code I have bookmarked a form field “PRAYERSbookmark” and I am attempting to us the font ‘hidden” function to “HIDE” not only the form field but the entire table and all the lines and text within that table. When I test this on its own it works (both the hide & unhide code) but unfortunately something is happening when I put it all together that now results in the code only unlocking the document and relocking it without actually doing anything more


Sub MacroAssignedToExitEvent()
'A basic Word macro coded by Greg Maxey

Dim sPassword As String
sPassword = "88998899"
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=sPassword
End If

If ActiveDocument.FormFields("prayersyesno").Result = "Yes" Then
'Your code to hide the text
Selection.GoTo What:=wdGoToBookmark, Name:="PRAYERSbookmark"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
With Selection.Font
.Name = ""
.Hidden = True
End With
Else
'Your code ot unhide the text
Selection.GoTo What:=wdGoToBookmark, Name:="PRAYERSbookmark"
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdLine, Count:=2, Extend:=wdExtend
With Selection.Font
.Name = ""
.Hidden = False
End With
End If
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=sPassword
End If

End Sub


Again I greatly appreciate any guidance or help that you can lend

gmaxey
09-09-2013, 02:32 PM
I don't really know what you are trying to do, but if you are trying to show/hide text in a bookmark then this should do it:



Sub MacroAssignedToExitEvent()
'A basic Word macro coded by Greg Maxey
Dim bProtected As Boolean
Dim sPassword As String
Password = "88998899"
'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=sPassword
End If
If ActiveDocument.FormFields("prayersyesno").Result = "Yes" Then
ActiveDocument.Bookmarks("PRAYERSbookmark").Range.Font.Hidden = True
Else
ActiveDocument.Bookmarks("PRAYERSbookmark").Range.Font.Hidden = False
End If
'Reprotect the document.
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, _
NoReset:=True, _
Password:=sPassword
End If
End Sub