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

M

Mark

That did work. Part of the problem may have been that I did not know that I
had to click the mouse to get the data into the data fields. I just get
hitting the enter button and nothing would happen until I clicked the left
mouse button by accident.

I also had to cut and paste my .dotm file into the Template folder. Is there
a way to do that automatically?

Mark
 
D

Doug Robbins - Word MVP

You can use the tab key to move from one control to the next. You may want
to set the tab order of the controls so that when you tab, the focus is
moved from control to control in an orderly fashion. To set the tab order,
click on the View menu and then select Tab Order.

--
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

Doug has answered the first part of your question.

Word 2007 is less strict about folder location and you can make any folder a
trusted location so it does not automatically save templates in 'the
templates folder'. When you save a document as a template, if you want it in
a particular location you must select that location before saving from the
dialog. To facilitate this you can add your preferred templates location to
the places bar at the left of the dialog.

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


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

Graham Mayor

The lines

Set oDoc = Documents.Open(FileName:="C:\Documents and
Settings\Administrator\My Documents\A Consult Letter.doc")

are in fact one line. The news editor has broken the line into two. You must
rejoin them.
However you would be better with the template approach. It is not good
practice to re-use documents and sooner or later it usually leads to data
loss.

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


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

Mark

Graham,

That worked well and I now can create my letter using a Template rather than
re-using my actual document (which if it is not rendered "read only" can
create problems.

I tried to change the race (I have only 4 choices so far) to a ListBox. I
may be using the wrong code (I looked it up) as nothing happens. Here is my
code that I put in at the end of my userform in place of the oVars statement
after I added ListBox1 to my userform:

ListBox1.List = Array("Hispanic-American", "white", "African-American",
"Asian")

Mark
 
G

Graham Mayor

Add the following macro to the userform to populate the list box.

Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "Hispanic-American"
.AddItem "White"
.AddItem "African-American"
.AddItem "Asian"
End With
End Sub

You could populate the list from an array eg

aRace = Array("Hispanic-American", "white", "African-American", "Asian")
With Me.ListBox1
For i = 0 To UBound(aRace)
.AddItem aRace(i)
Next i
End With

but unless you are deriving a variable length array from elsewhere it is not
worth doing so here

include in the Private Sub CommandButton1_Click()
the line
oVars("varRace").Value = Me.ListBox1.Value
To add the selected value to the variable.

or if you are following Greg's example
add to UpdateLetterFields(frmCL As ConsultLetter)
the line
oVars("varRace").Value = frmCL.ListBox1.Value


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP

Use:

ListBox1.List = Split("Hispanic-American,white,African-American,Asian", ",")

--
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

I tried adding the code but I must be adding it to the wrong places as I keep
getting run time errors like "Object variable or With block variable not set".

Mark
 
D

Doug Robbins - Word MVP

Right click on the Form in the Project Explorer and select View Code. It is
in the code window that then opens that you should have the Initialize
statement that Graham suggested.

--
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

Doug,

I guess I am lost as here is what I tried as code in the userform area and
they did not work:

1)

Option Explicit



Private Sub ListBox1_Click()

End Sub

Private Sub CommandButton1_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("varRace").Value = Me.ListBox1.Value
Private Sub UserForm_Initialize()
ActiveDocument.Fields.Update
Unload Me
End Sub

Private Sub UserForm_Click()

End Sub



2)

Option Explicit



Private Sub ListBox1_Click()

End Sub

Private Sub CommandButton1_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("varRace").Value = Me.ListBox1.Value
With Me.ListBox1
..AddItem "Hispanic-American"
..AddItem "White"
..AddItem "African-American"
.AddItem "Asian"
End With
End Sub
Private Sub UserForm_Initialize()
ActiveDocument.Fields.Update
Unload Me
End Sub

Private Sub UserForm_Click()

End Sub


Mark
 
G

Graham Mayor

Delete everything from the userform code area and replace it with

Option Explicit
Private Sub CommandButton1_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("varRace").Value = Me.ListBox1.Value
ActiveDocument.Fields.Update
Unload Me
End Sub
Private Sub UserForm_Initialize()
With Me.ListBox1
.AddItem "Hispanic-American"
.AddItem "White"
.AddItem "African-American"
.AddItem "Asian"
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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