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.