Solved

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

Posted on 2003-11-11
6
327 Views
Last Modified: 2010-04-17
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.
0
Comment
Question by:julesb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 1

Expert Comment

by:JackNaif
ID: 9725175
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
0
 

Author Comment

by:julesb
ID: 9739796
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
0
 
LVL 1

Expert Comment

by:JackNaif
ID: 9740315
No problem, Jules.
Just let me know if it was of any use.
0
 

Author Comment

by:julesb
ID: 9865235
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
           
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 9887917
PAQed, with points refunded (125)

SpazMODic
EE Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question