Avoiding the Find/Replace dialog in Word 2003

A

Alan Stancliff

This is a question related to Word 2003 and VBA


I posed this question in an earlier thread, but it was needlessly
complicated and wordy. Here's a clearer way of formulating my question,
perhaps. I hope I'm not breaking any good netizen conventions here.

This is the previous thread:
If you use a newsreader:
news://msnews.microsoft.com:119/[email protected]
If you use a browser:
http://www.microsoft.com/communitie...b2b58efec0aa&lang=en&cr=US&sloc=en-us&m=1&p=1

Right now, I have a routine that, in part, finds a medical record number
at the top of a document and creates an autocorrect entry for it. I put
it together with macro record and some hints from this forum. The way it
works is that the cursor jumps to the top of the document, finds the
words "Medical Record Number", selects the medical record number, which
is located just to the right, and assigns it to an autocorrect entry MRN.

I'd like to do that without relying on the dialog box, which causes
things to jump around. Assume my cursor is in section 2 and I want it to
stay that way at the end of the macro's run. How would I do that?

Here is the sample code:

Sub MyDemo()
'
' go to top of section
'
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=1,
Name:=""
'
' find medical record number
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Medical Record Number: "
.Forward = True
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
'
'Assign to Autocorrect entry mrn
'
AutoCorrect.Entries.Add Name:="mrn", Value:=Selection.Text
Selection.EndKey Unit:=wdLine

End Sub

Regards,

Alan
 
G

Graham Mayor

If your previous posts are correct then you have a document that contains

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

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

and you want to add the MRN number to an autocorrect entry called mrn
without any on-screen dialogs. That being the case the following macro will
do that

Dim strFind As String
Dim aText As String
Dim aEntry As AutoCorrectEntry

strFind = "MRN:*^13"
For Each aEntry In Application.Autocorrect.Entries
With aEntry
If .name = "mrn" Then
.Delete
End If
End With
Next aEntry

With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
Do While .Execute(findText:=strFind, _
Wrap:=wdFindContinue, Forward:=True, _
MatchWildcards:=True) = True
aText = Selection
aText = Mid(aText, 5, (Len(aText) - 6))
Application.Autocorrect.Entries.Add name:="mrn", Value:=aText
Exit Do
Loop
End With
Selection.EndKey Unit:=wdLine

However your current post suggests that the line now contains

Medical Record Number

That being the case the macro will not find it. You can change the line

strFind = "MRN:*^13"

to find the correct text

and if necessary change the line

aText = Mid(aText, 5, (Len(aText) - 6))

to extract the required part of that line to obtain the number.


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Greg Maxey

Alan,

If you stay away from using the selection object that won't happen.

If your Medical Record Number: XXXX is in the main text on the first page
then this should do:

Sub MyDemo()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng
With .Find
.ClearFormatting
.Text = "Medical Record Number: "
.Forward = True
.Execute
End With
.MoveEnd Unit:=wdWord, Count:=1
End With
AutoCorrect.Entries.Add Name:="mrn", Value:=oRng
End Sub

If it is in the header then this shouuld do:

Sub MyDemo2()
Dim oRng As Word.Range
Dim i As Long
For i = 1 To 3
Set oRng = ActiveDocument.Sections(1).Headers(i).Range
With oRng.Find
.ClearFormatting
.Text = "Medical Record Number: "
.Forward = True
If .Execute Then
oRng.MoveEnd Unit:=wdWord, Count:=1
Exit For
End If
End With
Next i
AutoCorrect.Entries.Add Name:="mrn", Value:=oRng
End Sub


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org~~~~~~~~~~~~~~~~~~~~~~~~~~
 
A

Alan Stancliff

Thank you Graham and Greg,

I'll be studying this, along with a post in a related thread by Shauna
Kelley, to try to learn more about VBA. More than 10 years ago, I did a
fair amount of programming during the days of DOS in both Powerbasic and
two implementations of WordPerfect macro command language (version 5x
and 6x). Version 6x was a real programming language, not a recorder, and
5x had if-then statements, variables, etc. Both were procedural, and 6x
was modular, as was Powerbasic. But I never really attempted anything
that had object-oriented concepts or a lot of GUI, and I am a bit thrown
by it all with its concept of containers, methods, properties, etc.
Little by little, I'll get the basic concept down.

So thanks for your patience with an old guy trying to learn something new.

Regards,

Alan Stancliff
 

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