In Word 2003, a macro that reads screen size and cursor position

A

Alan Stancliff

Is it possible for a macro to be written that can tell how many lines
are on the screen and which line the cursor is on when the macro runs?
Optionally, I would like to put these into message boxes.

I'm trying to figure something out.

Regards,

Alan
 
S

Shauna Kelly

Hi Alan

I'm not sure exactly what you're looking for. Maybe start with the
Window object (every visible document has at least one Window). Look at
its properties and methods. That may give you what you need. If not,
post back and tell us exactly what you're trying to do, and what version
of Word you're using.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Alan Stancliff

Hi Shauna,

I followed your advice, and as I am pretty much a beginner in VBA, I
went to this wonderful resource:
http://msdn2.microsoft.com/en-us/library/aa212469(office.11).aspx

I will be studying it more a little bit later and try to see what I can
noodle out on my own, and I will be back to ask more questions.

The version of Word I am using is Word 2003. I put that in the title of
the post, but I should have put it in the body of the question, too. An
answer to a question can not be more precise that the formulation of the
question itself.

What motivated my question was an earlier question I had posed here. The
URL for that question is:
http://www.microsoft.com/communitie...b2b58efec0aa&lang=en&cr=US&sloc=en-us&m=1&p=1

and if you are using a newsreader, the newsgroup identification of the
message is:
news://msnews.microsoft.com:119/[email protected]

Regards,

Alan
 
S

Shauna Kelly

Hi Alan

The page at
http://msdn2.microsoft.com/en-us/library/aa212469(office.11).aspx is
just a re-publication of what's in VBA help. In Word 2003, it's both
faster and easier to use Help in the code editor. For lots of terms in
the code, you can click on a word in your code (eg Document, or Range,
or Selection) and press F1 and get instant help on that object.
Otherwise, use Help > Microsoft Visual Basic Help and search for what
you need.

Reading your previous post alongside the present one leads me to believe
that you have some code that is tootling around doing something, and
then you want to take the user back to where they started. Right?

If so, then don't try to solve this problem: there are too many
different ways that users might have their screen set up, and you can't
possibly allow for them all (been there, done that!). Instead, prevent
the problem. Don't do anything in your code that changes the Selection.
You can *read* the selection, eg:
Dim rngOld as Word.Range
set rngOld = ActiveDocument.Selection.Range

but don't *set* the Selection, ie do not do
AnythingAtAll.Select

So, go through the code, find all occasions you've used .Select and find
a way around that. If you need help doing that, post back the relevant
bit of code.

You might also find it interesting to adjust the window sizes of your
document and the code so you can see both at once. Click anywhere in
your macro, then press F8 to execute the code line by line. When you see
the document's screen change, you know you've hit a line that you'll
need to deal with.

Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Alan Stancliff

Hi Shauna,

You wrote:
" Reading your previous post alongside the present one leads me to
believe
that you have some code that is tootling around doing something, and
then you want to take the user back to where they started. Right?"

That's exactly right.

Here's what I am trying to do, and I hope the explanation doesn't seem
too long or try your patience.

I transcribe medical dictation. A computer puts a Word 2003 document on
my screen, and this document has 3 continuous sections, such as one gets
by clicking on Insert>Break>Continuous in the standard menu. Section
one, which we cannot alter, has a bunch of demographic information,
including the first and last name of the patient. Not only can we not
alter it, we can't run macros or Word commands in it. It is protected.
However, we can highlight and select data in it.

Here is a bit of what Section 1 would look like (names changed to
protect patient confidentiality and to protect the guilty)

*******Continuous Section 1**************
Slicem N Stichem Medical Center - Confidential

Patient Name: DOE-SMITH, JANE
MRN: 553355
Date of Birth: 9/14/1900
*******Continuous Section 2**************
Etc etc.


Our data can be entered only in continuous section 2. We can run macros
and Word commands there (Section 3 contains footer information).

When I type out the document, I like to have an autocorrect entry that
contains the first and last name of the patient. That way, when I am
typing out the report, instead of typing out something like:
We placed Ignatz Kadiddlehopper-Schtuppen in the left lateral
decubitus position and made an incision over the
location of the gizzard,

I can type out:
We placed UU PP in the left lateral decubitus position
and made an incision over the location of the gizzard,
and UU will expand out into the first name and PP will expand out into
the last name. And no one will be able to accuse me of misspelling poor
old Mr. Kadiddlehopper-Schtuppen's name.

With the generous help of a bunch of people here and elsewhere, I have
made a macro that creates these autocorrect shortucts, but I'm not
entirely pleased with it. Here is what it does:

First of all, the macro notes where in the document the cursor is
located with this code:

' Remember where the cursor is at
' the start of the text. Call
' it rTmpCursorPosition
Dim rTmpCursorPosition As Range
Set rTmpCursorPosition = Selection.Range

It then goes to the top of section 1 with this bit of code:
' Go to section one
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=1,
Name:=""
Selection.Find.ClearFormatting

Then it looks for the section where one finds the words:
Patient Name:

and puts the cursor smack against the last name of the patient with this
bit of code:
' Find and highlight last name using
' using wild card search
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Patient Name:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=1

Then it selects just the last name with this bit of code:
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*,*"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Then this bit of code changes it to proper case, even if the last name
is a hyphenated one, and puts it into an autocorrect entry:

'Assign last name to autocorrect entry "varl"
Dim sText As String
sText = Selection.Text
If InStr(1, sText, "-") Then
sText = Replace(sText, "-", " ")
sText = StrConv(sText, vbProperCase)
sText = Replace(sText, " ", "-")
Else
sText = StrConv(sText, vbProperCase)
End If
AutoCorrect.Entries.Add Name:="varl", Value:=sText
'
'

The rest of the macro uses a similar logic.

At the end of the macro, the cursor is returned to its original position
with this code:
rTmpCursorPosition.Select
'End of Macro

One problem is that the screen jumps around a bit when playing the
macro. So I'd love it if you could tell me how to do that without
"setting" anything, as you said. If it's too big of a request, certainly
I'll understand. I have purchased a couple of books of VBA, as I am
beginning to find it an interesting diversion, and when I share my
macros with my colleagues, they really appreciate it.
 
S

Shauna Kelly

Hi Alan

There's something here about fishing, I think. I'd rather send you a
fishing rod and a tide chart than a box of pre-filleted, frozen, crumbed
fish fillets, if you know what I mean.

I suggest you create a little test document with a few paragraphs of
"quick brown fox" text to test out how this works.

Your code is using the .Find method of the Selection object.

The .Find method also applies to a range. So, you can (1) set a range
and (2) operate the .Find within that range. (Don't make it a collapsed
range. Why not? Try it out, and you'll see!)

So:

Sub TestFind()

Dim rngTest As Word.Range

Set rngTest = ActiveDocument.Paragraphs(2).Range
With rngTest.Find
.Text = "brown"
.Replacement.Text = "pink"
.Wrap = wdFindStop 'restrict the .Find to your range
.Execute Replace:=wdReplaceAll
End With

End Sub

If you step through the code with F8 you will see that the selection has
no bearing on the operation of the macro (it searches and replaces only
within the second paragraph) and the selection does not move.

If you want to search and replace in a whole document do
set rngTest = ActiveDocument.Range

So, for each .Find in your code:
(1) define a range as the range in which you want to operate the .Find
(2) apply the .Find method to your range.

Hope this helps. Post back if you need more help.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
A

Alan Stancliff

Shauna,

I very much appreciate your approach to my very large question--probably
a bit too large. Nevertheless, you are giving me food for thought.

Thanks for the tackle, bait, and fishing pole. Now I'm on my way to the
fishing hole.

Regards,

Alan
 
A

Alan Stancliff

Hi Shauna,

I was just revisiting this thread, trying to squeeze more understanding
out of it. So I made a little document with "The quick brown fox" in
several paragraphs and tried out the macro. I opened the Word document
and the macro editor side-by-side so that I could watch each step. When
I did so, I found that the macro did nothing at all.

Did I miss something here?

Regards,

Alan Stancliff
 
G

Graham Mayor

If you are taking the code literally, then of course it won't do anything as
the quick brown fox text doesn't contain the word 'brown'. Change the word
'brown' to 'fox'
i.e.
.Text = "brown"
to
..Text = "fox"

and the macro should replace each occurrence of 'fox' in the second
paragraph i.e.

ActiveDocument.Paragraphs(2).Range

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

Alan Stancliff

Hi Graham,

Thanks for the quick response.

Surely the phrase "the quick brown fox" contains the word "brown."

It looked to me as if Shauna's code is meant to replace the word "brown"
with "pink."

I still don't get the point.

Regards,

Alan Stancliff
 
D

Doug Robbins - Word MVP

Maybe you were concentrating too much on the macro code as that is were most
of the action appears to be taking place. There is no need to really have
the document and the VBE opened side by side.

But to see what is happening (or perhaps not happening would be a better
thing to say) select a paragraph other than the second paragraph in the
document. Then when you run the code, the word(s) "brown" in the second
paragraph is/are replaced by "pink", but the selection remains where you put
it, and does not move to the second paragraph.

This does work with the two windows side by side, but you will not see any
change in the document (pink substituded for brown) until the

.Execute Replace:=wdReplaceAll

command is executed. Maybe you were expecting it to happen earlier and did
not step through the code that far.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Graham Mayor

Now guess who looks foolish? :eek:)

However the macro does work on the second paragraph. Switch on the
formatting marks CTRL+* and make sure you have the required number of
paragraph marks to ensure that it is the second paragraph you are checking.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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