PDA

View Full Version : [SOLVED:] Paragraph Return



HTSCF Fareha
11-04-2020, 02:19 PM
I cannot believe I am struggling to sort this! :crying:

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!

gmaxey
11-04-2020, 06:31 PM
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

HTSCF Fareha
11-05-2020, 02:26 AM
Greg, many thanks! This worked just as required.

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

gmaxey
11-05-2020, 07:43 AM
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.

HTSCF Fareha
11-05-2020, 11:26 AM
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