PDA

View Full Version : Hide Textfields when selecting options from a dropdown list



macca34
01-30-2014, 08:01 AM
I am trying to hide text fields with a word form when an option is selected from a drop down list. I am new to VBA and the code I have written below is obviously not correct. Grateful for any advice.


Sub Type_addition()
'Hide 'Current Information' if request is addition
If ActiveDocument.FormFields("DropDown") = "Addition" Then
ActiveDocument.FormFields("DGOld").Visible = False
Else
ActiveDocument.FormFields("DGOld").Visible = True
End If
End Sub

gmaxey
01-30-2014, 10:38 AM
Formfields don't have a "visible" property. I suppose you could create an autotext entry of the single field and another one with just a blank space. Add a bookmark where the field or space should appear and run this code:


Sub TypeAddition()
Dim oRng As Word.Range
With ActiveDocument
If .ProtectionType = wdAllowOnlyFormFields Then
.Unprotect
Set oRng = ActiveDocument.Bookmarks("bmFieldAnchor").Range
If .FormFields("Dropdown1").Result = "Addition" Then
Set oRng = .AttachedTemplate.AutoTextEntries("atField").Insert(Where:=oRng, RichText:="True")
Else
oRng.FormFields(1).Delete
Set oRng = .AttachedTemplate.AutoTextEntries("atEmpty").Insert(Where:=oRng, RichText:="True")
End If
.Bookmarks.Add "bmTableRange", oRng
.Protect wdAllowOnlyFormFields, True
End If
End With
End Sub

fumei
01-30-2014, 04:44 PM
The fact that you put the .Visible with the formfield makes me think you are not using Option Explicit.

macca34
01-31-2014, 01:55 AM
Greg
I have added a bookmark next to filed 'DGOld' and I ran the code. Nothing happend.

fumei
Option Explicit??

gmaxey
01-31-2014, 05:17 AM
Did you create the autotext entries atField and atEmpty? After doing so and creating the bookmark there should be no text field in the document unless the dd value is "Addition"

macca34
01-31-2014, 05:47 AM
I have created auotext entries called as above. I have created a boomark (go to) next to 'DGOld'. Nothing is happening.

gmaxey
01-31-2014, 06:37 AM
Are you running the macro on exit from the dropdown field? Here is an example template file: https://dl.dropboxusercontent.com/u/64545773/Field%20Demo.dot

macca34
01-31-2014, 06:59 AM
Link doesnt work. I'll try adding running the macro on exit. Thanks

macca34
01-31-2014, 09:14 AM
I've tried changing this a bit by show/hide table rows. Depending on a combobox selection rows in a table will show/hide. I have started writing the code but not sure how I can do this. Here is what I have at the moment.


Private Sub ComboBox1_Change()
ComboBox1.List = Array("Test1", "Test2", "Test3")
If ComboBox1.Value = "Test1" Then
With ActiveDocument.Tables(1).Rows(1)
.HeightRule = wdRowHeightAuto
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
End With
ElseIf ComboBox1.Value = "Test2" Then
With ActiveDocument.Tables(1).Rows(2)
.HeightRule = wdRowHeightExactly
.Height = ".5"
.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
.Borders(wdBorderRight).LineStyle = wdLineStyleNone
End With
' ElseIf ComboBox1.Value = "Test3" Then
'' With ActiveDocument.Tables(1).Rows(3)
' .HeightRule = wdRowHeightExactly
' .Height = ".5"
' .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
' .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
' .Borders(wdBorderRight).LineStyle = wdLineStyleNone
' End With
End If
End Sub

macca34
02-04-2014, 02:58 AM
Can anyone help me with this please.