Link to home
Start Free TrialLog in
Avatar of julesb
julesb

asked on

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.
Avatar of JackNaif
JackNaif

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:

Jack
Avatar of julesb

ASKER

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.

Regards
Jules
No problem, Jules.
Just let me know if it was of any use.
Avatar of julesb

ASKER

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!

Jules
           
ASKER CERTIFIED SOLUTION
Avatar of SpazMODic
SpazMODic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial