Consulting

Results 1 to 2 of 2

Thread: Remove Excess Paragraphs Inside A TextBox

  1. #1
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    357
    Location

    Remove Excess Paragraphs Inside A TextBox

    I have a TextBox called "txtExplanation", which is used to populate a Content Control of the same name in my document.

    What I'm trying to achieve is the removal of any excess paragraphs that might be present within the TextBox. So in effect only a single paragraph is present between blocks of text and potential empty paragraphs removed after the last sentence.

    I'm currently getting an "Invalid or unqualified reference" on this line

    oRng.Text = .txtExplanation.Text
    Here is what I am trying to use. Of course it's possible that I'm approaching this the wrong way!

    Sub RemoveEmptyParagraphs()
    
        Dim occ    As ContentControl
        Dim oRng   As Range
        Dim oCtrl  As control
        Dim intCounter As Integer
    
        Set oRng = occ.Range
        
        oRng.Text = .txtExplanation.Text
        
        ' Set a range to the content control
        Set oRng = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range
        ' Fill the range with the content of the text box
        oRng.Text = oCtrl.Text
        
        ' Remove any empty paragraphs after last entered text
        intCounter = 1
        
        Do
            oRng.Paragraphs.Last.Range.Characters.Last.Delete
            
            intCounter = intCounter + 1
            ' Setting this to five should cater for the most extreme example
        Loop Until intCounter >= 5
        
    lbl_Exit:
        Exit Sub
    End Sub

  2. #2
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    357
    Location
    Apologies, please ignore this post as posted in haste!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •