Create Document Variables from a multiline text box is a user form

A

Angela

I am creating a userform for a meeting agenda template. When the user types
in the agenda items in a multiline text box, I would like to assign each
agenda item (each paragraph or line) to a document variable. The number of
items will vary with each meeting, so that is why I decided to use the
multiline text box.

I have tried loops without success, please would someone be kind enough to
help me?

Thank you so much
 
J

Jay Freedman

You need to "parse" the string that you get from the text box's .Text
property -- that is, separate it into substrings with one agenda item in each
one. To do this, you'll have to know what character or set of characters
serves as the separator between agenda items in the big string. It may be a
paragraph mark (Chr$(13)) but that needs to be tested on some real input. I
seem to recall that text boxes use the pair of characters Chr$(13) &
Chr$(10), or maybe the reverse.

Once you know the right character(s), you should be able to use the Split()
function to break up the big string and put the substrings into a Variant
array. Then you can use a For Each loop to put the substrings into document
variables.

I don't have Word handy at the moment to provide sample code. Maybe someone
else will stop by and offer some.
 
W

Word Heretic

G'day Angela <[email protected]>,

I could commercially assist if required, this could be several hours
work at least.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Angela reckoned:
 
J

Jay Freedman

Now that I have Word available, I can answer your request.

I was correct in remembering the Chr$(13) & Chr$(10) issue in userform
text boxes, so the following code removes the extra character before
splitting the string.

You may want to change the way the document variables are named.
That's up to you. The code shown will create variables named
AgendaItem1, AgendaItem2, etc.

Private Sub cmdOK_Click()
Dim BigString As String
Dim StringArray As Variant
Dim idx As Integer

' The items in the text box are separated
' by two characters, Chr$(13) & Chr$(10).
' Change these to just Chr$(13).
BigString = Replace(txtInput.Text, _
Chr$(13) & Chr$(10), Chr$(13))

' Split the string into substrings at
' the Chr$(13) delimiters.
StringArray = Split(BigString, Chr$(13))

' Put each substring in a separate
' document variable.
With ActiveDocument
For idx = 0 To UBound(StringArray)
.Variables("AgendaItem" & (idx + 1)).Value _
= StringArray(idx)
Next idx
End With

Unload Me
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top