Solved

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

Posted on 2003-11-11
6
321 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

864 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now