I am trying to create a userform in which users type a title into a textbox and click a command button to add the reference. I want one text box and one command button to add a new item onto a separate line in an alpha list similar to:
a) Reference Title1 b) Reference Title2 c) Reference Title3
with each listed item on a new left aligned line.
Private Sub CommandButton3_Click()
'Define the variable oVars
Set oVars = ActiveDocument.Variables
oVars("REF").Value = TextBox7.Value
ActiveDocument.Fields.Update
Me.TextBox7 = Null
Me.TextBox7.SetFocus
End Sub
There is a screenshot of what I'm trying to accomplish. The upper portion is the form i've created and the bottom is what the outcome should hopefully look like. I apologize for not being better able to explain myself
I would solve this by adding a Rich Text Format content control to the document with 'Content control cannot be deleted' checked. If you add this in a paragraph formatted with a numbered style that will save you having to number the entries.
The routine below, which should be placed in a standard module (i.e. not the user form), will update the content adding the carriage return if it already contains text.
This method has the advantage that the text in the control can, if necessary, be directly edited without the user form, will update automatically, and can be repeatedly added to over the life of the document. Unlike bookmarks it cannot get deleted.
Sub AddTextToContentControl(workDoc As Document, ccTitle As String, textToAdd As String)
Dim ctrl As ContentControl
Set ctrl = GetContentControlByTitle(workDoc, ccTitle)
If Not ctrl Is Nothing Then
If ctrl.ShowingPlaceholderText Then
'replace the placeholder text
ctrl.Range.text = textToAdd
Else
'add to the existing text
ctrl.Range.text = ctrl.Range.text & vbcr & textToAdd
End If
End If
End Sub
You call this from your user form like this:
Private Sub CommandButton3_Click()
AddTextToContentControl ActiveDocument, "References", TextBox7.Value
Me.TextBox7 = Null
Me.TextBox7.SetFocus
End Sub