Save text blocks in variables

R

Ridge Kennedy

I'm looking for a way to select two (or more) specific lines of text and
save them as string variables.

I can navigate around to highlight the lines that I want to work with; but
have not found a way to "copy" them into a string variable -- as opposed to
just putting them onto the clipboard.

My research has led to use of the Range object, but I have found it very
difficult to comprehend how to define a range as a part of a paragraph. One
thing that would help would be if there was a way to define a current
selection as a range -- but I haven't found a way to do that.

Helpful pointers would be appreciated.

Ridge (in New Joisey)
 
J

Jay Freedman

I'm looking for a way to select two (or more) specific lines of text and
save them as string variables.

I can navigate around to highlight the lines that I want to work with; but
have not found a way to "copy" them into a string variable -- as opposed to
just putting them onto the clipboard.

My research has led to use of the Range object, but I have found it very
difficult to comprehend how to define a range as a part of a paragraph. One
thing that would help would be if there was a way to define a current
selection as a range -- but I haven't found a way to do that.

Helpful pointers would be appreciated.

Ridge (in New Joisey)

The Selection object in VBA represents the text that's currently
selected in the active document. So:

Dim myString As String
myString = Selection.Text

will store the selected text in the variable myString.

A Range object is a lot like the Selection object, with two main
exceptions:

- You can have many Range objects defined simultaneously, while there
can only be one Selection.
- A Range object doesn't necessarily have any connection to the
currently selected text. You can define a Range anywhere in the
document, modify its text or formatting, move it around, change its
size, etc. without any effect on the selected text and without
scrolling the screen.

You could define a Range to point to the same text as the Selection:

Dim myRange As Range
Set myRange = Selection.Range
myString = myRange.Text

or you can define it to point somewhere else completely:

Set myRange = ActiveDocument.Paragraphs(2).Range
myRange.MoveStart Unit:=wdCharacters, Count:=10
myRange.MoveEnd Unit:=wdCharacters, Count:=-5
myString = myRange.Text

gets all of the second paragraph of the document except the first 10
characters and the last 5 characters.

Macros frequently do a search, using myRange.Find, to locate certain
words or formatting. See the sample code in
http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm.
 
R

Ridge Kennedy

Jay said:
Dim myString As String
myString = Selection.Text

Great. I found

Selection.typetext "msString"

as one way to put the text back when I locate the spot.

Is that best, or is there a preferred method?

If you wanted to preserve the string data in the variable for later use by a
different procedure, would there be a convenient way to do that in a
variable, or would you need to prite it to a temp file or ini file or
registry, and access it later?
Dim myRange As Range
Set myRange = Selection.Range
myString = myRange.Text

I appreciate the added information. I was trying to figure this out, too,
while fumbling around.

Thank you.

Ridge
 
J

Jay Freedman

Ridge said:
Jay said:


Great. I found

Selection.typetext "msString"

as one way to put the text back when I locate the spot.

Is that best, or is there a preferred method?

I prefer to do nothing at all, or as little as possible, with the Selection
object. There are only a few situations where it's necessary: when the macro
has no way to "locate the spot" and must depend on the user to make a
choice, or when the macro requires one of the few Selection functions that
aren't available to Range objects.

Usually I'll figure out some way to use myRange.Find to locate the spot, and
then use myRange.Text = myStringVar to insert the text.
If you wanted to preserve the string data in the variable for later
use by a different procedure, would there be a convenient way to do
that in a variable, or would you need to prite it to a temp file or
ini file or registry, and access it later?

You could do any of those things, or you can save the string in a document
variable:

ActiveDocument.Variables("mySavedString").Value = myStringVar

This is an "invisible" location within the document file where you can store
strings. Since it's saved in the document file, it will be available if the
file is sent to another computer -- something that isn't true for any of the
external storage places you mentioned. On reopening the document, your code
can retrieve the string. Because the code will cause an error if you try to
retrieve the value of a document variable that doesn't exist, you need code
like this to do the job:

Dim myString As String
Dim myDocVar As Variable

For Each myDocVar In ActiveDocument.Variables
If myDocVar.Name = "mySavedString" Then
myString = myDocVar.Value
Exit For
End If
Next myDocVar

If myString <> "" Then
MsgBox myString
End If

This looks at each document variable (if there are any) in the active
document until either (a) it finds one with the right name and gets its
value or (b) it goes through the whole set and doesn't find the right name,
so myString is still the default empty string.

For more about this, look at the VBA help topics about the Variable object
and the Variables collection.
I appreciate the added information. I was trying to figure this out,
too, while fumbling around.

Thank you.

Ridge

You're welcome.
 

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