Consulting

Results 1 to 5 of 5

Thread: Paragraph Return

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

    Paragraph Return

    I cannot believe I am struggling to sort this!

    I have a TextBox (txtExplanation) on my UserForm. If the user types anything into this box (which is optional) then I would like to add a paragraph return before and after when this is committed to the document at the content control point called Explanation.

    I have this code which I cannot get to even add a paragraph return at all. Now I know that this really ought to be an easy thing to do. I'm wondering if I'm tackling this the wrong way? I've scoured the internet without finding any solution. There's mention of the differences between carriage returns, line returns, & vbCrLf, & Chr (13) etc.

    If oCtrl.Name = "txtExplanation" Then
                                Set oCC = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1)
                                
                                Selection.TypeParagraph
                            Selection.TypeParagraph
                                
                            End If
                            
                            ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range.Text = oCC.Range.Text
                        Else
    Really hoping that someone can help. Thanks!

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You want to add an empty paragraph before and after a contentcontrol:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range
      oRng.Collapse wdCollapseStart
      oRng.MoveStart wdCharacter, -1
      oRng.InsertBefore vbCr
      Set oRng = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range
      oRng.Collapse wdCollapseEnd
      oRng.MoveStart wdCharacter, 1
      oRng.InsertAfter vbCr
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Greg, many thanks! This worked just as required.

    Simple yes, but not as simple as I was thinking.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Well, based on the code you posted, it is hard to determine what you were thinking. You haven't declared an oCC variable, you have two lines of code dealing with a selection with no indication where that selection is and then you apply the same document content control content that you had previously set to an undeclared variable to that same content control. Makes no sense at all.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Mentor
    Joined
    Aug 2020
    Location
    Hampshire
    Posts
    395
    Location
    Sorry, it doesn't make any sense without the rest of the code. I'd focussed on the problem piece.

    Here's the full code

    Sub FillForm()
        Dim oCtrl       As Control
        Dim oCC         As ContentControl
        Dim lngIndex    As Long
        Dim strTC       As String
    
        With m_oFrm
    
            For Each oCtrl In .Controls
                
                Select Case TypeName(oCtrl)
                    Case "TextBox"
                        If oCtrl.Name = "txtName" Then
                            strTC = strConv(oCtrl.Text, vbProperCase)
                            Set oCC = ActiveDocument.SelectContentControlsByTag("Name").Item(1)
                            oCC.Range.Text = strTC
                            For lngIndex = 1 To oCC.Range.Words.Count
                                If oCC.Range.Words(lngIndex).Characters(2) = "c" Then
                                    oCC.Range.Words(lngIndex).Characters(3) = UCase(oCC.Range.Words(lngIndex).Characters(3))
                                End If
                            Next
                            ActiveDocument.SelectContentControlsByTag("Name1").Item(1).Range.Text = oCC.Range.Text
                            
                            ' If explanation required, then add a paragraph either side of the entered text
                                
                                Dim oRng As Range
                                Set oRng = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range
                                oRng.Collapse wdCollapseStart
                                oRng.MoveStart wdCharacter, -1
                                oRng.InsertBefore vbCr
                                Set oRng = ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range
                                oRng.Collapse wdCollapseEnd
                                oRng.MoveStart wdCharacter, 1
                                oRng.InsertAfter vbCr
                            
                            ActiveDocument.SelectContentControlsByTag("Explanation").Item(1).Range.Text = oCC.Range.Text
                        Else
                            ActiveDocument.SelectContentControlsByTag(Replace(oCtrl.Name, "txt", "")).Item(1).Range.Text = oCtrl.Text
                        End If
                    Case "ComboBox"
                        ActiveDocument.SelectContentControlsByTag(Replace(oCtrl.Name, "cbo", "")).Item(1).Range.Text = oCtrl.Value
                End Select
            Next oCtrl
        End With
        
    lbl_Exit:
        Exit Sub
    End Sub

Posting Permissions

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