#1
|
|||
|
|||
Hiding and unhiding text based on text dropdown button
Hi All,
I am using MS Word 2010 I am looking for a VBA in word for the following issue: I have the following dropdown button, with the options: Yes and No. Name of the dropdown box is choose. In case yes is choses I want the VBA to hide row 13+14 in word. If No is chosen than unhide row 13+14 in word. Thanks in advance for your help. Anand |
#2
|
||||
|
||||
What kind of dropdown, what kind of rows and where are they?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Hi Paul,
It is drop down list, created through Drop-Down list Contend Control (Developer). With rows I meant lines in a word documnent. Lines 13+14 in the word document. The drop-down and the lines are in the same word document. In the first page you make your choices, with the drop down buttons. The text below should hidden / unhidden based on the choices you made. Thanks and Regards, Anand |
#4
|
||||
|
||||
What you ask is not as straightforward as you envisage. For a start 'Line' is a vague concept in Word and is determined by text flow.
If I was doing this I would do it differently. I would start with a bookmark inserted at the location where the text is to appear (or disappear). Name the bookmark (say) BMPara13 Save the text you want to display/hide as an autotext entry in the document template and call it Para13. In the ThisDocument module add the code Code:
Option Explicit Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean) Select Case ContentControl.Title Case "SelectPara" If ContentControl.Range.Text = "No" Then BBToBM "BMPara13", ThisDocument.AttachedTemplate, "Para13" Else FillBM "BMPara13", "" End If Case Else 'The user exited some other content control that we don't care about. End Select lbl_Exit: Exit Sub End Sub The code above calls upon a pair of functions to insert/remove the autotext Code:
Public Sub FillBM(strBMName As String, strValue As String) 'Graham Mayor - http://www.gmayor.com Dim oRng As Range With ActiveDocument On Error GoTo lbl_Exit Set oRng = .Bookmarks(strBMName).Range oRng.Text = strValue oRng.Bookmarks.Add strBMName End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub Public Sub BBToBM(strBMName As String, strTemplate As String, strBBName As String) 'Graham Mayor - http://www.gmayor.com Dim oRng As Range Dim iLen1 As Integer, iLen2 As Integer With ActiveDocument iLen1 = Len(ActiveDocument.Range) On Error GoTo lbl_Exit Set oRng = .Bookmarks(strBMName).Range Application.Templates(strTemplate). _ BuildingBlockEntries(strBBName).Insert _ Where:=oRng, _ RichText:=True iLen2 = Len(ActiveDocument.Range) oRng.End = oRng.End + (iLen2 - iLen1) oRng.Bookmarks.Add strBMName End With lbl_Exit: Set oRng = Nothing Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
vba code, word 2010 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Hiding text and macro security | subspace3 | Word VBA | 4 | 10-05-2014 10:01 AM |
Word Macro for hiding text | Phil Pense | Word | 1 | 07-23-2012 08:17 PM |
Mark text in a text box and copy to clipboard (with button) | ArthurM | PowerPoint | 4 | 02-19-2012 11:33 AM |
Macro to populate a text form field based on dropdown selection | koloa | Word | 0 | 10-20-2011 11:52 AM |
Hiding text | Reg06 | Visio | 0 | 01-15-2011 08:04 AM |