1
votes

I am new to vba and developing a document that prompts the user to select a variable number of values from a combo box list. After selecting the values, I want to insert them in order onto the document itself as a Text Form Field. Let me show you how I generally am trying to get it to work.

First, the user selects values:

 [a]
 [b]
 [c]

And selects an "OK" button. Then, I am attempting to add these selected values into the word document starting at a bookmark. Value "a" should be inserted followed by a space character followed by a blank Text Form Field, followed by two carriage returns. In the end the result should look something like this:

 [bookmark]
 [a]'_'[blank_a]'^p'
 '^p'
 [b]'_'[blank_b]'^p'
 '^p'
 [c]'_'[blank_c]'^p'
 '^p'

Where [bookmark] is an invisible bookmark, '_' is a space, and '^p' is a carriage return. Currently my code is as follows:

 Dim myRange As Range
 Set myRange = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("START").Range.Start, _
 End:=ActiveDocument.Bookmarks("END").Range.End)

 For i = 1 To NUMBER_OF_RESPONSES
     Selection.FormFields.Add(myRange, wdFieldFormTextInput).Name = "question_" & i
     Selection.FormFields.Add(myRange, wdFieldFormTextInput).Result = "response_" & i
 Next i

Naturally, there are no insertions of literal spaces or carriage returns yet as I have not figured out how to do it. The result of this code is as follows:

 [START][blank_c][c][blank_b][b][blank_a][a][END]

I would like this order reversed and for there to be the aforementioned formatting inserted. Any pointers on how to go about doing it?

2

2 Answers

2
votes

I am not sure if I have missed something, but why not relying on simple paragraphs instead on Bookmarks? Here you have a code doing what you want and any other thing (you can modify the ranges of the paragraphs to perform as complex actions as you wish).

Dim curRange As Range
Dim start_i As Integer
Dim end_i As Integer
Dim NUMBER_OF_RESPONSES As Integer
NUMBER_OF_RESPONSES = 3
start_i = NUMBER_OF_RESPONSES + 1 '0
end_i = 1 'NUMBER_OF_RESPONSES

Set curParagraph = ActiveDocument.Paragraphs.First

curParagraph.Range.Text = "[START]"
i = start_i
Do
    If (start_i < end_i) Then
        i = i + 1
    Else
        i = i - 1
    End If

    Set curParagraph = curParagraph.Range.Paragraphs.Add
    curParagraph.Range.Text = "[question_" & i & "][" & "response_" & i & "]"
Loop While (i <> end_i)

Set curParagraph = curParagraph.Range.Paragraphs.Add
curParagraph.Range.Text = "[END]"
1
votes

Solution

Well now I feel silly for asking the question. The solution was pretty simple.

 ActiveDocument.Bookmarks("START").Select

 For i = 1 To NUMBER_OF_RESPONSES
     Selection.Font.Size = 11
     Selection.Font.Bold = True
     Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
         .Name = "question_" & i
     Selection.Font.Bold = False
     Selection.TypeText Text:=" "
     Selection.FormFields.Add(Range:=Selection.Range, Type:=wdFieldFormTextInput) _
         .Name = "response_" & i
     Selection.TypeParagraph
     Selection.TypeParagraph
 Next i

So the real issue was placing the cursor in the right location:

 ActiveDocument.Bookmarks("START").Select

From there I was able to use Selection to insert the desired FormFields and characters.

This link was pretty helpful.

And if you are reading this because you also are new and trying to learn what to do, check out how to record a macro. It's a good first step. Record the macro, view the code it generated, and use that code to guide your own development. Cool.