Another Word VBA question

D

DeanL

Hi all,

I have a Word doc that has a single table in it with 3 columns. The
first column is a job title, the second is the full name of that
person in that job and the third is the name with the last name
first. These values can change but not very often.

I have a second word doc that runs a macro on startup that displays
the first of 3 userforms. When the first userform appears, I need
Word/VBA to read values from the table in the first file (the name of
a person in a specific job) and put that value in one of the textboxes
on the userform. People do move on from this position so that's why I
need to use a table in one document that the relevant staff can change
when a person leaves or a new person starts so they don't have to mess
around with the code.

I've been trying to find an answer to this for the last day or so and
I'm getting to the point where I'm pulling my hair out so any help you
can offer would be gratefully received.

Many thanks in advance.

Dean...
 
D

Doug Robbins - Word MVP

Are you sure that it is a textbox into which you want the value to appear
and not a Listbox?



This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document
Dim myitem As Range
Dim i As Long, j As Long, m As Long, n As Long
Application.ScreenUpdating = False
' Open the file containing the client details
' Modify the path in the following line so that it matches where you
saved Clients.doc
Set sourcedoc = Documents.Open(FileName:="c:\Company.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


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

DeanL

Are you sure that it is a textbox into which you want the value to appear
and not a Listbox?

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
    Dim sourcedoc As Document
    Dim myitem As Range
    Dim i As Long, j As Long, m As Long, n As Long
    Application.ScreenUpdating = False
    ' Open the file containing the client details
    ' Modify the path in the following line so that it matches where you
saved Clients.doc
    Set sourcedoc = Documents.Open(FileName:="c:\Company.doc")
    ' Get the number or clients = number of rows in the table of client
details less one
    i = sourcedoc.Tables(1).Rows.Count - 1
    ' Get the number of columns in the table of client details
    j = sourcedoc.Tables(1).Columns.Count
    ' Set the number of columns in the Listbox to match
    ' the number of columns in the table of client details
    ListBox1.ColumnCount = j
    ' Define an array to be loaded with the client data
    Dim MyArray() As Variant
    'Load client data into MyArray
    ReDim MyArray(i, j)
    For n = 0 To j - 1
        For m = 0 To i - 1
            Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
            myitem.End = myitem.End - 1
            MyArray(m, n) = myitem.Text
        Next m
    Next n
   ' Load data into ListBox1
    ListBox1.List() = MyArray
    ' Close the file containing the client details
    sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
    ListBox1.BoundColumn = i
    Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document.  You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client.  It
assumes that the first row of the table containing the client details is a
header row.

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

Hi Doug,

Thanks for the code but I've already tried this approach and it just
caused a "deer in the headlights" look from my users. It is a textbox
on a Word userform I'm looking to populate and not a listbox with a
specific item from a specific cell in a table (it doesn't matter
whether the table is in Word, Excel or an Access DB). An example is
shown below:

Title Name Initials

JobA PersonA PAInit
JobB PersonB PBInit
JobC PersonC PCInit

The Job Title will never change in the table but the people in the Job
may change on a regular basis. My UserForm is used to fill out
details on a Word document and I need some text boxes to be auto
populated with the names of certain people based on their job title
when the UserForm Initializes and not give the user options to select
from as this has proved somewhat disastrous in the past with the users
selecting the wrong names, etc and not bothering to correct them. I
know that this is a training issue and have tried everything short of
a large mallet to get the staff to do this right so I'm trying to make
this as idiot proof as possible.

Another thing is that this table will be referenced by multiple
documents and each document will want to extract the name from a
specific cell in the table which will not change even if the person in
the job does. The UserForm in document A will always need the value
from the cell in row 2, column 2, and so on.

I hope someone can help me with this.

Many thanks...
 
D

Doug Robbins - Word MVP

What dictates whether the user is completing a form for Job A, Job B or Job
C.

It is definitely not covered in your posts. The work-flow needs to be
established in the first instance.

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

Are you sure that it is a textbox into which you want the value to appear
and not a Listbox?

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize()
and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document
Dim myitem As Range
Dim i As Long, j As Long, m As Long, n As Long
Application.ScreenUpdating = False
' Open the file containing the client details
' Modify the path in the following line so that it matches where you
saved Clients.doc
Set sourcedoc = Documents.Open(FileName:="c:\Company.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal
with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.

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

Hi Doug,

Thanks for the code but I've already tried this approach and it just
caused a "deer in the headlights" look from my users. It is a textbox
on a Word userform I'm looking to populate and not a listbox with a
specific item from a specific cell in a table (it doesn't matter
whether the table is in Word, Excel or an Access DB). An example is
shown below:

Title Name Initials

JobA PersonA PAInit
JobB PersonB PBInit
JobC PersonC PCInit

The Job Title will never change in the table but the people in the Job
may change on a regular basis. My UserForm is used to fill out
details on a Word document and I need some text boxes to be auto
populated with the names of certain people based on their job title
when the UserForm Initializes and not give the user options to select
from as this has proved somewhat disastrous in the past with the users
selecting the wrong names, etc and not bothering to correct them. I
know that this is a training issue and have tried everything short of
a large mallet to get the staff to do this right so I'm trying to make
this as idiot proof as possible.

Another thing is that this table will be referenced by multiple
documents and each document will want to extract the name from a
specific cell in the table which will not change even if the person in
the job does. The UserForm in document A will always need the value
from the cell in row 2, column 2, and so on.

I hope someone can help me with this.

Many thanks...
 

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