• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

VBA Word - Returning variable number of selected chars from a string

In Word I have a bookmark "address" which contains between 4 - 6 lines of text separated by paragraph marks.

I want to select from "address" the first 3 lines of the text (up to but not including the paragraph mark) and put them into a combo box as three separate items.  I think the best way to do this will be to put each line of text into its own variable (for e.g., line 1 into variable strAdd1, line 2 into strAdd2, line 3 into strAdd3) and then add the variables to the combo box dropdown list.  BUT how do I get VBA to select the text to go into the variables?  Line 1 will be everything to the left of the first paragraph mark, line 2 will be everything between the first and second para marks, and line 3 will be everything between the second and third para marks.
  • 2
  • 2
1 Solution
Put your cursor at the begining of one of the lines, then do

    Selection.EndKey Unit:=wdLine, Extend:=wdExtend

This selects the whole line.
Now you can use the text of the line by refering to it as Selection.Text.

You add it to your combo box (or do strAdd1 = Selection.Text), and then move to the next line and repeat. (A for-to-next might be a good choice here).

If you don't want to select the line (because it is bad for performance) or if one of these lines might actually be longer than a line (because if it extends up to the paragraph mark, then it is actually a paragraph, rather than a line) then you can use the paragraphs collection.

paragraphs(38).range.text is the text in the 38th paragraph in the document.

This still leaves open the question "How do I know which line/paragraph is indeed the first line of one of the addresses?"

Well if you know how to get from one of those to the next in a simple step, you can always record a macro which does only that, and then copy and paste those few lines into your code. (That's how I produced the "Selection.EndKey Unit:=wdLine, Extend:=wdExtend" at the begining of this) =)

Hope it helps:

julesbAuthor Commented:
Thanks Jack. Sorry for the delay in acknowledging your help, I've been waylaid from this by another project but will be back to the VBA gridnstone tomorrow!

I'll incorporate your suggestions into my code and let you know how I get on.

I'm on a course next week so may not have a chance to post again until the week of 24 November.

No problem, Jules.
Just let me know if it was of any use.
julesbAuthor Commented:
Finally got there via a different route!  I needed to then go back and select the second and third lines of the address also, and place each in a combo box.  We ended up using the InStr function in a loop as below:

For X = 0 To 2
strAdd1 = Left(strLetterAddress, InStr(strLetterAddress, Chr(13)) - 1)
cmbAttn.AddItem (strAdd1)
 strLetterAddress = Mid(strLetterAddress, InStr(strLetterAddress, Chr(13)) + 1)
Next X

But thanks again for your help!

PAQed, with points refunded (125)

EE Moderator

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now