Solved

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

Posted on 2003-11-11
6
318 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
Comment Utility
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
Comment Utility
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
Comment Utility
No problem, Jules.
Just let me know if it was of any use.
0
 

Author Comment

by:julesb
Comment Utility
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
Comment Utility
PAQed, with points refunded (125)

SpazMODic
EE Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
EvenOdd challenge 10 81
sumDigits  challenge 7 59
countPairs challenge 7 57
nestparen challenge 4 55
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

763 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

6 Experts available now in Live!

Get 1:1 Help Now