View Single Post
 
Old 08-25-2016, 01:31 AM
gmayor's Avatar
gmayor gmayor is offline Windows 10 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