How do I select text?

E

edger

Dumb question, I suppose, but:

How can I select lines of text programatically? Can't record a macro of the
highlighting process, but there must be a way to select a range of lines (to
change the font, for example).
 
J

Jean-Guy Marcil

edger was telling us:
edger nous racontait que :
Dumb question, I suppose, but:

How can I select lines of text programatically? Can't record a macro
of the highlighting process, but there must be a way to select a
range of lines (to change the font, for example).

Before answering, we need to know the logic behind the selection. How does
the program know which lines of text to select? How can the user indicate
these to the program? etc.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

edger

I'm pretty much a self-taught newcomer to macro programming, so please
forgive my ignorance. I'm working on a Word macro which will be run by a
VBScript script.
It opens a template, inserts one file and then a second file, and reformats
the text of the second file only in the new document.

In addition to my original question, it would be very helpful to know some
general programming methods for identifying the lines in a document by
number. I guess one can identify the last line on a page using the
Lines.Count property. Is there a way to retrieve other info about a line of
text? Can one iterate through the lines of a text document (perhaps using
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext) and access the properties
of the "current" line?

In the present case, I want to know how to select the block of text I wish
to reformat -- can I identify it by means of the first and last lines and
"highlight" it?
What code would do this? More globally: is there a good text for teaching
oneself to program such basic things as this in Word macros?

Thanks very much.
 
J

Jean-Guy Marcil

edger was telling us:
edger nous racontait que :
I'm pretty much a self-taught newcomer to macro programming, so please
forgive my ignorance. I'm working on a Word macro which will be run
by a VBScript script.
It opens a template, inserts one file and then a second file, and
reformats the text of the second file only in the new document.

In addition to my original question, it would be very helpful to know
some general programming methods for identifying the lines in a
document by number. I guess one can identify the last line on a page
using the Lines.Count property. Is there a way to retrieve other info
about a line of text? Can one iterate through the lines of a text
document (perhaps using Selection.GoTo What:=wdGoToLine,
Which:=wdGoToNext) and access the properties of the "current" line?

In the present case, I want to know how to select the block of text I
wish to reformat -- can I identify it by means of the first and last
lines and "highlight" it?
What code would do this? More globally: is there a good text for
teaching oneself to program such basic things as this in Word macros?

Working with lines (and pages) is new to Word 2003. So, you have to make
sure that your code will only be used on Word 2003.

Play with this code to get an idea of how to work with lines. Lines belong
to the Rectangle object (which belong to the Pages object which in turn
belong to the Pane object, etc...). The rectangle object roughly equals an
area of text contained on a page.

Try the code below with a blank document, then add a header, then a footer,
the a text box...! See the big surprise with the text box?

So, working with lines is not really a beginner thing.

Dim objRectangles As Rectangles
Dim i As Long

Set objRectangles = ActiveDocument.ActiveWindow _
.Panes(1).Pages(1).Rectangles

MsgBox "There are " & objRectangles.Count & " ""Rectangles"" on this page."

For i = 1 To objRectangles.Count
MsgBox "Rectangle " & i & " has " & objRectangles(i).Lines.Count & "
line(s)."
Next

Try working with paragraphs instead, much easier.
Use the macro recorder and then, try navigating to:

http://word.mvps.org/Tutorials/index.htm

from there you have many helpful links, in particular:
http://word.mvps.org/FAQs/MacrosVBA/VBABasicsIn15Mins.htm
http://word.mvps.org/FAQs/MacrosVBA/UsingRecorder.htm
and if you go to that last one I mention, make sure to go to:
http://word.mvps.org/faqs/macrosvba/ModifyRecordedMacro.htm


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top