Group: microsoft.public.word.vba.general
From: "Graham Mayor"
Date: Monday, March 03, 2008 2:31 AM
Subject: Re: In Word 2003, a macro that reads screen size and cursor position

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



Alan Stancliff wrote:
> 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
>
> Shauna Kelly wrote:
>> 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
>>
>>
>>
>>
>> "Alan Stancliff" wrote in message
>> news:Ov8HrgUcIHA.4880@TK2MSFTNGP03.phx.gbl...
>>> 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.