Re: Input box based upon a list
- From: "Doug Robbins - Word MVP" <dkr@xxxxxxxxxxxxxxxxxx>
- Date: Fri, 26 Oct 2007 13:50:18 +1000
To populate a combobox (or listbox, you use the .AddItem command. If the
information that is to be loaded into the combobox will not change, you can
have the items to be loaded hard coded into the routine (usually the
Initialize Event of the userform)
If the information to be loaded is likely to change, it is more convenient
to store it outside of the procedure.
This routine loads a listbox (or combobox) 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, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.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.
To get hold of the information for the item that is selected in the
combobox, you use the .Value attribute of the combobox (if there is only one
column, you do not need to worry about setting the .BoundColumn attribute)
To cause the used form to be displayed, you use the .Show command in the
routine from which you want to cause it to be displayed.
--
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
"bryan" <bryan@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:008F1DBF-E4B9-4F14-97F8-FE6837729977@xxxxxxxxxxxxxxxx
I created my userform and inserted a combo box.
Being a novice at this:
1) How do I populate the combo box with info for selection
2) How do I call or invoke from my macro 'SaveIR'
3) How is the variable selected from the combo box passed back for me to
use
in my macro?
Thanks for all the info,
Bryan
"Russ" wrote:
Bryan,
I not sure what you mean by "If it needs to be on the form can I hide
it?"
The UserForm popup gathers information from the user, you store in a
variable and manipulate it as you please. You decide if and where you
would
display it in the main document, if at all. If you just need the user to
pick an item from a list to help develop a pathname to store the document
information, that is OK.
Information can also be stored in document variables and aren't normally
seen by the user.
Storing Values When a Macro Ends
http://snipurl.com/swu3
I see now that I can use the UserForm inside another macro, although as
mentioned I do not need the info on the document, I will only use it in
my
string of building the file name. If it needs to be on the form can I
hide it?
Thanks,
Bryan
"Russ" wrote:
Bryan,
See inline.
In reading the userform article, 2 thingsThe UserForm1.Show command can be issued from any module, the example
1) I would like the box to appear after I have selected my 'Save to
IR'
button
at the
website just talks about using an autoexec subroutine, but you could
issue
the command from your 'Save to IR' macro code.
2) I don't need or want this info on the documentLike a popup input box, a Userform is the Cadillac of information
retrieval
dialogs and can be made simple or extravagant. The information
gathered from
the user must be injected in the document by code, it doesn't
automatically
appear into the document.
http://gregmaxey.mvps.org/Repeating_Data.htm
Maybe I leave as an input box?
Thanks,
Bryan
"Doug Robbins - Word MVP" wrote:
Then it sounds like what you need is a userform and I think that you
perhaps
have the wrong idea about what that actually is.
See the article "How to create a Userform" at:
http://word.mvps.org/FAQs/Userforms/CreateAUserForm.htm
You would have a combobox on the userform that is loaded with the
items
from
which you want the user to make a selection.
--
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
"bryan" <bryan@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:929E1561-636A-45C1-94D7-FBC875FC1A78@xxxxxxxxxxxxxxxx
Autotextlist is not what I'm looking for.
Upon saving a doc, I wanted an Input Box for the user to supply
info.
Then I thought rather than having them type, I would supply a
drop-down of
options.
This is not inside the document.
Thanks,
Bryan
"Russ" wrote:
Does an autotextlist do what you want?
http://www.word.mvps.org/FAQs/TblsFldsFms/AutoTextList.htm
I would like to use an INPUT BOX that rather than having a user
key in
something, they would choose from a list. Is this possible?
If so how would you code?
A userform is not what I am looking for as this is not part of
the
document
Thanks,
Bryan
--
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
--
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
--
Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
.
- Follow-Ups:
- Re: Input box based upon a list
- From: bryan
- Re: Input box based upon a list
- References:
- Re: Input box based upon a list
- From: Russ
- Re: Input box based upon a list
- From: Doug Robbins - Word MVP
- Re: Input box based upon a list
- From: bryan
- Re: Input box based upon a list
- From: Russ
- Re: Input box based upon a list
- From: bryan
- Re: Input box based upon a list
- From: Russ
- Re: Input box based upon a list
- From: bryan
- Re: Input box based upon a list
- Prev by Date: Re: Input box based upon a list
- Next by Date: Re: Input box based upon a list
- Previous by thread: Re: Input box based upon a list
- Next by thread: Re: Input box based upon a list
- Index(es):