Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-25-2016, 01:31 AM
gmayor's Avatar
gmayor gmayor is offline Hiding and unhiding text based on text dropdown button Windows 10 Hiding and unhiding text based on text dropdown button Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Then whenever you leave the content control, you will either insert or remove the autotext entry at/from the bookmarked location.

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
Reply With Quote
Reply

Tags
vba code, word 2010



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding and unhiding text based on text dropdown button 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
Hiding and unhiding text based on text dropdown button 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

Other Forums: Access Forums

All times are GMT -7. The time now is 05:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft