Need help for macro to add name to several places in a document

M

Mark

I need to run a macro that brings up a dialog box prompting me for a person's
name then types the name in 3 places in the document (I usually put an
asterisk * where I want the names to be placed in my effort to use find and
replace).

I tried using find and replace but that does not work in this macro as I
can't get it to prompt me for the new name.

I assume that I should use a variable but I am not sure how to get it to
prompt me for the name then place the name in my document.

Thanks for any help on this problem,

Mark
 
M

Mark

Jay,

Thanks for your answer. However, the problem is that I want to use the macro
to enter other non-repeated information (which I know how to do by using
input boxes) into my document. The name part is the only repeated information
and I don't want to have to enter it in 3 times.

The page that you sent me to gives me techniques that are too difficult for
me to integrate into my macro.

Mark
 
D

Doug Robbins - Word MVP

Show us your macro so that we can give you a method that integrates with it.

--
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, originally posted via msnews.microsoft.com
 
M

Mark

Below is my macro. I am looking to see is I can change the part of the macro
where I add the patient's name 3 times into one step.

It would also be nice to be able to take the doctor's last name (from where
I enter the full name) and have it pasted to the place where I type in just
the doctor's last name.

Sub CL()
'
' CL Macro
'
'
Documents.Open FileName:="C:\Documents and Settings\Administrator\My
Documents\A Doctor Letter.doc", ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:=InputBox(Prompt:="Type patient's name:",
Title:="Patient Name", Default:="")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:=InputBox(Prompt:="Type patient's name:",
Title:="Patient Name", Default:="")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "*"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:=InputBox(Prompt:="Type patient's name:",
Title:="Patient Name", Default:="")
Selection.MoveUp Unit:=wdScreen, Count:=1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Dr."
.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:=wdCharacter, Count:=1
Selection.TypeText Text:=InputBox(Prompt:="Type doctor's last name:",
Title:="Doctor's Last Name", Default:="")
Selection.MoveUp Unit:=wdLine, Count:=2
Selection.TypeText Text:=InputBox(Prompt:="Type Doctor's Full Name
name:", Title:="Doctor's Full Name", Default:="")
Selection.TypeParagraph
Selection.TypeText Text:=InputBox(Prompt:="Type street address:",
Title:="Street address", Default:="")
Selection.TypeParagraph
Selection.TypeText Text:=InputBox(Prompt:="Type city,state,zip:",
Title:="city,state,zip", Default:="")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:=InputBox(Prompt:="Type patient's age:",
Title:="Patient age", Default:="")
Selection.Find.ClearFormatting
With Selection.Find
.Text = "#"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.TypeText Text:=InputBox(Prompt:="Type patient's race/sex:",
Title:="Patient race/sex", Default:="")
Selection.Find.ClearFormatting
Selection.EndKey Unit:=wdLine
Selection.TypeText Text:=" "
End Sub
 
D

Doug Robbins - Word MVP

I would suggest that you dump all of that and create a userform in a
template from which you create the documents and have the code in the user
form set the values of variables in the document and whereever you want the
information to appear, insert a docvariable field

See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

and the following pages of fellow MVP Greg Maxey's website :

http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm

http://gregmaxey.mvps.org/Populate_UserForm_ListBox.htm


--
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, originally posted via msnews.microsoft.com
 
G

Graham Mayor

I agree with Doug. The best way to handle this is with a userform saved in a
template. Instead of opening an existing document and deleting information
then writing new information from a raft of inconvenient input boxes. Save
the document as a template - without the field content - and create new
documents from it.

If you collect the variable text in a user form (you only need collect it
once!) then you can do whatever you want with the collected text by
assigning all or parts of it (or different information depending on what is
selected) to docvariables You can then place the information as many times
as you need by placing docvariable fields to insert the information and
adding code to update those fields.

The following example uses InputBoxes to demonstrate the principles
involved, to update two fields { DocVariable varDoctor } and { DocVariable
varPatient }

Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varDoctor").Value = _
InputBox("Type doctor's name:", "Doctor")
oVars("varPatient").Value = _
InputBox("Type patient's name:", "Patient Name")
ActiveDocument.Fields.Update


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Mark

Graham,

Your code was actually exactly what I needed and enabled me to finish my
project and make it work exactly the way I wanted it to.

I can see how a user form would do the same thing even better in one compact
box, but trying to learn how to do it (from both the links on the web that
you have supplied and my book "Word 2007 Macros & VBA) result in giving me a
monster headache with little progress. It is easy for those who know how to
do it but for novices like me, creating a user box for the first time is
quite tough.

Thanks again for the help,

Mark
 
G

Graham Mayor

Actually a simple userform with a few text fields is not as difficult as you
imagine. The same thing can be achieved using Doug's instructions at
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm, substituting the
following code for the listing in section 10


Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varDoctor").Value = Me.TextBox1.Value
oVars("varPatient").Value = Me.TextBox2.Value
ActiveDocument.Fields.Update
Unload Me

The only thing I would suggest is to use more meaningful names for TextBox1,
TextBox2, CommandButton1 and UserForm1 as this makes it easier to keep track
of but it will work exactly as he has suggested even with the default names.
Once you get past the basics you can then go on to investigate Greg's web
pages.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Mark

Graham,

I was able to make the userform with your help. However, I did not start the
project with the Template selected but rather just as I do usually to create
my macros in Project Normal.

Now that I have created my userform, how do I call it up (it does not show
up in the macro list using Alt-F8) to bring it up to so that I can fill it in
and see if it works?

The instructions in Step 12 do not seem to apply to Word 2007.

Thanks,

Mark
 
G

Greg Maxey

Mark,

You would call your UserForm using a procedure in a standard project
module:

Sub CallUF()
Dim oFrm As UserForm1 'or whatever name you gave to your form
Set oFrm = New UserForm1
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End Sub
 
M

Mark

Greg,

That was very helpful. I was even able to add an open statement that first
loads my document containing the fields to be filled.

The only problem is that after filling in all the info, when I keep pressing
enter it only cycles back to textbox 1 and to the other textboxes without
putting the typed info in to the fields.

Thanks,

Mark
 
G

Graham Mayor

The macro and form would normally go in the document template, but for your
own use it could work from the normal template. However your reply gives the
clue to the problem You say you have used the macro to open the document and
it seems likely that the wrong document is now being addressed by the
userform and so nothing appears to happen.

Open your document. Save the document as a template (DOT or DOTM - NOT DOTX)
with the docvariable fields in place. Move the macro and the userform to the
new template - see http://www.gmayor.com/installing_macro.htm

Rename Greg's macro AutoNew thus

Sub AutoNew()
Dim oFrm As UserForm1 'or whatever name you gave to your form
Set oFrm = New UserForm1
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End Sub

Remove any reference you may have added to opening the document.

Now create a new document from the template. The form will pop up, you can
fill it and it will update the focvariable fields in the document (provided
they are in the body of the document - if they are elsewhere you will need a
better targeted update macro.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

Mark

For added info, here is my code for my userform:

Private Sub UserForm_Click()
Dim oVars As Variables
Set oVars = ActiveDocument.Variables
oVars("varDoctor1").Value = Me.TextBox1.Value
oVars("varDoctor2").Value = Me.TextBox2.Value
oVars("varStreetAddress").Value = Me.TextBox3.Value
oVars("varCityStateZip").Value = Me.TextBox4.Value
oVars("varPatient").Value = Me.TextBox5.Value
oVars("varAge").Value = Me.TextBox6.Value
oVars("varRaceSex").Value = Me.TextBox7.Value
ActiveDocument.Fields.Update
Unload Me
End Sub


And here is the code for the macro that brings up the userform with my
document:

Sub CallUF()
Documents.Open FileName:="C:\Documents and Settings\Administrator\My
Documents\A Consult Letter.doc", ConfirmConversions:= _
False, ReadOnly:=False, AddToRecentFiles:=False,
PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
Selection.Find.ClearFormatting
Dim oFrm As ConsultLetter 'or whatever name you gave to your form
Set oFrm = New ConsultLetter
oFrm.Show
Unload oFrm
Set oFrm = Nothing
End Sub


Mark
 
G

Graham Mayor

Change Private Sub UserForm_Click()
to
Private Sub CommandButton1_Click() 'or the button name used.

If you are working with a limited and fixed number of doctors you could use
a list box for the doctors names.
I would suggest also using list boxes for race and sex, but it is the name
here that is the problem

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


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

Greg Maxey

Mark,

First, I agree with Graham that you should create a template and not recycle
an existing document. However, if you want to continue that course you
could use code something like this:

In a standard module:

Option Explicit
Dim oDoc As Word.Document

Sub CallUF()
Dim oFrm As ConsultLetter
Set oDoc = Documents.Open(FileName:="C:\Consult Letter") 'Use your own path
of course.
Set oFrm = New ConsultLetter
oFrm.Show
Unload oFrm
Set oFrm = Nothing
Set oDoc = Nothing
End Sub

Sub UpdateLetterFields(frmCL As ConsultLetter)
Dim oVars As Variables
Set oVars = oDoc.Variables
oVars("varDoctor1").Value = frmCL.TextBox1.Value
oVars("varDoctor2").Value = frmCL.TextBox2.Value
oVars("varStreetAddress").Value = frmCL.TextBox3.Value
oVars("varCityStateZip").Value = frmCL.TextBox4.Value
oVars("varPatient").Value = frmCL.TextBox5.Value
oVars("varAge").Value = frmCL.TextBox6.Value
oVars("varRaceSex").Value = frmCL.TextBox7.Value
oDoc.Fields.Update
End Sub

In the UserForm module CommandButton_Click use:

Private Sub CommandButton1_Click()
Me.Hide
UpdateLetterFields Me
End Sub

Since you are just learning, you should learn to minimize UserForm code as
much as possible to only the code required to manage the form itself. Here
you are passing the form as an argument to a called procedure in the
standard module. The called procedure does the work of updating the
document fields with the form data.
 
M

Mark

Greg,

I tried your code but I get a compile error that "variable not defined" at
the second line where I have the correct path for my document.

Set oDoc = Documents.Open(FileName:="C:\Documents and
Settings\Administrator\My Documents\A Consult Letter.doc") 'Use your own path
of course.

I am not sure what is wrong as that is the correct path to open the document.

Mark
 
M

Mark

Graham,

Thanks for the help as I really would like to learn how to do this properly.

I did reference the correct file to open as I could see the proper file open
up in word as I ran the macro.

I did all the steps that you instructed but I guess I am not sure what you
mean by "create a new document from the template" which should pop up my
userform. I opened the new .dotm file but nothing happens. I am not sure what
exactly I should do here.

Mark
 
D

Doug Robbins - Word MVP

Instead of opening the template, you should use File>New and select the
template as the basis for the document that you want to create.

When you do that, the autonew macro will run. It will NOT run if you use
File>Open.

--
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, originally posted via msnews.microsoft.com
 

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