X-Posted: Can I create a drop down list that will let me select an item
Author |
Message |
Dyla #1 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
I'm not sure if this VBA related, and apologise if it appears off-topic. I have a template for a report that is produced every week. I would like to customise it where a drop down list will appear on a page of the document, allowing the editor of the report to select a desired string of text. Is there any way to doing this? D.Dawson
|
Mon, 07 Nov 2005 18:33:47 GMT |
|
 |
Doug Robbins - Word MV #2 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Dylan, See: Please Fill Out This Form Part 1: Create professional looking forms in Word http://www.computorcompanion.com/LPMArticle.asp?ID=22 Part 2: Adding Automation to your Word forms. http://www.computorcompanion.com/LPMArticle.asp?ID=46 Part 3: Learn more VBA (macros) to automate your forms. http://www.computorcompanion.com/LPMArticle.asp?ID=119 Part 4: Use custom dialog boxes in your Word forms http://www.computorcompanion.com/LPMArticle.asp?ID=127 Part 5: Connect your AutoForm to a database to save input time and keep better records! http://www.computorcompanion.com/LPMArticle.asp?ID=136 If you wanted to use autotext to store the string, you could use the following routine in conjunction with a formfield type dropdown ' Macro created 15-11-97 by Doug Robbins to add the address corresponding to a drop down name ' Set myDrop = ActiveDocument.FormFields("Dropdown1").DropDown Company = myDrop.ListEntries(myDrop.Value).Name Address = ActiveDocument.AttachedTemplate.AutoTextEntries(Company).Value ActiveDocument.FormFields("Text1").Result = Address Please respond to the newsgroups for the benefit of others who may be interested. Hope this helps Doug Robbins - Word MVP Quote:
> I'm not sure if this VBA related, and apologise if it appears off-topic. > I have a template for a report that is produced every week. > I would like to customise it where a drop down list will appear on a page of > the document, allowing the editor of the report to select a desired string > of text. Is there any way to doing this? > D.Dawson
|
Mon, 07 Nov 2005 19:39:44 GMT |
|
 |
Jay Freedma #3 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Be aware that all of Doug's references are for "protected" forms. If your template is not for a protected form already, you can use an AutoTextList field and not have to protect the template. See http://www.mvps.org/word/FAQs/TblsFldsFms/AutoTextList.htm for information. -- Regards, Jay Freedman Microsoft Word MVP Word MVP FAQ site: http://www.mvps.org/word
Quote: > Dylan, > See: > Please Fill Out This Form > Part 1: Create professional looking forms in Word > http://www.computorcompanion.com/LPMArticle.asp?ID=22 > Part 2: Adding Automation to your Word forms. > http://www.computorcompanion.com/LPMArticle.asp?ID=46 > Part 3: Learn more VBA (macros) to automate your forms. > http://www.computorcompanion.com/LPMArticle.asp?ID=119 > Part 4: Use custom dialog boxes in your Word forms > http://www.computorcompanion.com/LPMArticle.asp?ID=127 > Part 5: Connect your AutoForm to a database to save input time and > keep better records! > http://www.computorcompanion.com/LPMArticle.asp?ID=136 > If you wanted to use autotext to store the string, you could use the > following routine in conjunction with a formfield type dropdown > ' Macro created 15-11-97 by Doug Robbins to add the address > corresponding to a drop down name > ' > Set myDrop = ActiveDocument.FormFields("Dropdown1").DropDown > Company = myDrop.ListEntries(myDrop.Value).Name > Address = > ActiveDocument.AttachedTemplate.AutoTextEntries(Company).Value > ActiveDocument.FormFields("Text1").Result = Address > Please respond to the newsgroups for the benefit of others who may be > interested. > Hope this helps > Doug Robbins - Word MVP
>> I'm not sure if this VBA related, and apologise if it appears >> off-topic. >> I have a template for a report that is produced every week. >> I would like to customise it where a drop down list will appear on a >> page of the document, allowing the editor of the report to select a >> desired string of text. Is there any way to doing this? >> D.Dawson
|
Tue, 08 Nov 2005 00:42:17 GMT |
|
 |
Dyla #4 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Both of these are good, but not exactly what I'm looking for. I apologise for not supplying more information to start with. I have a blank summary sheet, which is basically a table, and this is to be added to the back of reports. Then the summary items will be added to it. As it stands just now, the top of the sheet has a text box in which the reference number of the report is usually typed. And, I am looking for a way to do this mechanically. Regards Dylan
|
Tue, 08 Nov 2005 18:02:33 GMT |
|
 |
Dyla #5 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Quote: > Both of these are good, but not exactly what I'm looking for. I apologise > for not supplying more information to start with. > I have a blank summary sheet, which is basically a table, and this is to be > added to the back of reports. Then the summary items will be added to it. As > it stands just now, the top of the sheet has a text box in which the > reference number of the report is usually typed. And, I am looking for a way > to do this mechanically.
So, I thought creating a listbox of all the references that we use in my company would work. However, I cannot have a locked document and the list must be easy to scroll through, because it contains a couple of hundred entries. Quote:
|
Tue, 08 Nov 2005 18:56:16 GMT |
|
 |
Doug Robbins - Word MV #6 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Hi Dylan, In that case, I would be using a userform with a multicolumn listbox that is populated with data that is stored in an external file. See the article How to create a Userform at: http://www.mvps.org/word/FAQs/Userforms/CreateAUserForm.htm Here's an example of a routine that 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, 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 Suppliers.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. -- Please respond to the newsgroups for the benefit of others who may be interested. Hope this helps Doug Robbins - Word MVP Quote:
> > Both of these are good, but not exactly what I'm looking for. I apologise > > for not supplying more information to start with. > > I have a blank summary sheet, which is basically a table, and this is to > be > > added to the back of reports. Then the summary items will be added to it. > As > > it stands just now, the top of the sheet has a text box in which the > > reference number of the report is usually typed. And, I am looking for a > way > > to do this mechanically. > So, I thought creating a listbox of all the references that we use in my > company would work. However, I cannot have a locked document and the list > must be easy to scroll through, because it contains a couple of hundred > entries. > > Regards Dylan
|
Tue, 08 Nov 2005 19:43:53 GMT |
|
 |
Dyla #7 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Sory for the delay in getting back to you, I got disconnected there, for a while. Anyways, this example is what I am looking for, the external file holds all the references for the projects, my only reservation is that on an old computer the external file takes a while to load. I would like to reference to an access database rather than a word file. Also, how do I invoke the userform when the file is inserted to another document? Somethingh along the lines of... Private Sub Document_Inserted() frmContractorList.Show End Sub Where I would use Private Sub Document_New() when a new document is created, the document I want to use has to be inserted into another. Yours Dylan Dawson
|
Sat, 19 Nov 2005 15:21:43 GMT |
|
 |
Doug Robbins - Word MV #8 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Hi Dylan, The following routing populates a Listbox with data from a table in an Access database: Private Sub UserForm_Initialize() 'allocate memory for the database object as a whole and for the active record Dim myDataBase As Database Dim myActiveRecord As Recordset Dim i As Integer, j As Integer, m As Integer, n As Integer 'Open a database Set myDataBase = OpenDatabase("E:\Access97\Ely\ResidencesXP.mdb") 'Access the first record from a particular table Set myActiveRecord = myDataBase.OpenRecordset("Owners", dbOpenForwardOnly) 'Get the number of fields in the table j = myActiveRecord.Fields.Count 'Get the number of Records in the table 'Loop through all the records in the table until the end-of-file marker is reached i = 0 Do While Not myActiveRecord.EOF i = i + 1 'access the next record myActiveRecord.MoveNext Loop myActiveRecord.Close 'Set the number of columns in the listbox ListBox1.ColumnCount = j ' Define an array to be loaded with the data Dim MyArray() As Variant 'Load data into MyArray ReDim MyArray(i, j) For n = 0 To j - 2 Set myActiveRecord = myDataBase.OpenRecordset("Owners", dbOpenForwardOnly) m = 0 Do While Not myActiveRecord.EOF MyArray(m, n) = myActiveRecord.Fields(n + 1) m = m + 1 myActiveRecord.MoveNext Loop Next n ' Load data into ListBox1 ListBox1.List() = MyArray 'Then close the database myActiveRecord.Close myDataBase.Close End Sub In order for this code to run, you must establish a reference, in your template, to Microsoft DAO #.## Object Library. To do this, open the VB editor and choose References on the Tools menu. The numbers #.## will vary with your version of Word, maybe 3.51 or 3.60 I would suggest that the simplest thing to do is have a template with the userform in it and the table into which the data from the list box will be inserted. Then do a File>New to create a document from that template containing a table with the data for the report that you are working on, then copy that table and paste it into your report. The copying of the table to the clipboard could be done via the code in the userform so that then all you would have to do would be go to the location in the report where you want the information and press Ctrl+V. The code in the userform could also close the document that was created without saving it. Please respond to the newsgroups for the benefit of others who may be interested. Hope this helps Doug Robbins - Word MVP Quote:
> Sory for the delay in getting back to you, I got disconnected there, for a > while. > Anyways, this example is what I am looking for, the external file holds all > the references for the projects, my only reservation is that on an old > computer the external file takes a while to load. > I would like to reference to an access database rather than a word file. > Also, > how do I invoke the userform when the file is inserted to another document? > Somethingh along the lines of... > Private Sub Document_Inserted() > frmContractorList.Show > End Sub > Where I would use > Private Sub Document_New() > when a new document is created, the document I want to use has to be > inserted into another. > Yours > Dylan Dawson
|
Sat, 19 Nov 2005 17:46:30 GMT |
|
 |
Dyla #9 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Hi Doug, I get a bit confused at the line; For n = 0 To j - 2 Why have "j - 2" ? Extracted from your original code: 'Load data into MyArray ReDim MyArray(i, j) For n = 0 To j - 2 Set myActiveRecord = myDataBase.OpenRecordset("Owners", dbOpenForwardOnly) m = 0 Do While Not myActiveRecord.EOF MyArray(m, n) = myActiveRecord.Fields(n + 1) m = m + 1 myActiveRecord.MoveNext Loop Next n
|
Tue, 22 Nov 2005 16:53:16 GMT |
|
 |
Doug Robbins - Word MV #10 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Hi Dylan, I guess in the situation where I was using that code, I didn't want the last field loaded into the list box (though I did have the listbox.coumncount set to the number of fields in the table. Change it to j - 1 to get all of the fields in your table. Please respond to the newsgroups for the benefit of others who may be interested. Hope this helps Doug Robbins - Word MVP Quote:
> Hi Doug, > I get a bit confused at the line; > For n = 0 To j - 2 > Why have "j - 2" ? > Extracted from your original code: > 'Load data into MyArray > ReDim MyArray(i, j) > For n = 0 To j - 2 > Set myActiveRecord = myDataBase.OpenRecordset("Owners", > dbOpenForwardOnly) > m = 0 > Do While Not myActiveRecord.EOF > MyArray(m, n) = myActiveRecord.Fields(n + 1) > m = m + 1 > myActiveRecord.MoveNext > Loop > Next n
|
Tue, 22 Nov 2005 17:51:49 GMT |
|
 |
Dyla #11 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Is there a way to sort the data alphabetically when it appears in the listbox?
Quote: > Hi Dylan, > The following routing populates a Listbox with data from a table in an > Access database: > Private Sub UserForm_Initialize() > 'allocate memory for the database object as a whole and for the active > record > Dim myDataBase As Database > Dim myActiveRecord As Recordset > Dim i As Integer, j As Integer, m As Integer, n As Integer > 'Open a database > Set myDataBase = OpenDatabase("E:\Access97\Ely\ResidencesXP.mdb") > 'Access the first record from a particular table > Set myActiveRecord = myDataBase.OpenRecordset("Owners", dbOpenForwardOnly) > 'Get the number of fields in the table > j = myActiveRecord.Fields.Count > 'Get the number of Records in the table > 'Loop through all the records in the table until the end-of-file marker is > reached > i = 0 > Do While Not myActiveRecord.EOF > i = i + 1 > 'access the next record > myActiveRecord.MoveNext > Loop > myActiveRecord.Close > 'Set the number of columns in the listbox > ListBox1.ColumnCount = j > ' Define an array to be loaded with the data > Dim MyArray() As Variant > 'Load data into MyArray > ReDim MyArray(i, j) > For n = 0 To j - 2 > Set myActiveRecord = myDataBase.OpenRecordset("Owners", > dbOpenForwardOnly) > m = 0 > Do While Not myActiveRecord.EOF > MyArray(m, n) = myActiveRecord.Fields(n + 1) > m = m + 1 > myActiveRecord.MoveNext > Loop > Next n > ' Load data into ListBox1 > ListBox1.List() = MyArray > 'Then close the database > myActiveRecord.Close > myDataBase.Close > End Sub > In order for this code to run, you must establish a reference, in your > template, to Microsoft DAO #.## Object Library. To do this, open the VB > editor and choose References on the Tools menu. The numbers #.## will vary > with your version of Word, maybe 3.51 or 3.60 > I would suggest that the simplest thing to do is have a template with the > userform in it and the table into which the data from the list box will be > inserted. Then do a File>New to create a document from that template > containing a table with the data for the report that you are working on, > then copy that table and paste it into your report. The copying of the > table to the clipboard could be done via the code in the userform so that > then all you would have to do would be go to the location in the report > where you want the information and press Ctrl+V. The code in the userform > could also close the document that was created without saving it. > Please respond to the newsgroups for the benefit of others who may be > interested. > Hope this helps > Doug Robbins - Word MVP
Quote: > > Sory for the delay in getting back to you, I got disconnected there, for a > > while. > > Anyways, this example is what I am looking for, the external file holds > all > > the references for the projects, my only reservation is that on an old > > computer the external file takes a while to load. > > I would like to reference to an access database rather than a word file. > > Also, > > how do I invoke the userform when the file is inserted to another > document? > > Somethingh along the lines of... > > Private Sub Document_Inserted() > > frmContractorList.Show > > End Sub > > Where I would use > > Private Sub Document_New() > > when a new document is created, the document I want to use has to be > > inserted into another. > > Yours > > Dylan Dawson
|
Mon, 28 Nov 2005 15:34:31 GMT |
|
 |
Jonathan Wes #12 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Quote:
> Is there a way to sort the data alphabetically when it appears in the > listbox?
You would want to sort MyArray before loading its contents into the listbox. There are plenty of sorting routines available in the Internet which you could adapt. Just do a search with Quicksort and VB as the keywords. VB code for this sort of thing should work perfectly happily in VBA. -- Regards Jonathan West - Word MVP MultiLinker - Automated generation of hyperlinks in Word Conversion to PDF & HTML http://www.multilinker.com Word FAQs at http://www.multilinker.com/wordfaq Please post any follow-up in the newsgroup. I do not reply to Word questions by email
|
Mon, 28 Nov 2005 15:53:04 GMT |
|
 |
Dyla #13 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
I keep getting error messages when trying to operate the sorting routine. I'm using the following code and I get an error at the line highlighted, stating that "value = arr(index)=<Subscript out of Range>" any idea where I'm going wrong? Sub CombSort(arr As Variant, Optional numEls As Variant, _ Optional Descending As Boolean) Dim value As Variant Dim index As Long Dim firstItem As Long Dim Gap As Long Dim Swap As Boolean ' account for optional arguments If IsMissing(numEls) Then numEls = UBound(arr) firstItem = LBound(arr) Gap = numEls - firstItem + 1 Swap = False Do While (Gap > 1 Or Swap) ' divide Gap by 1.3 - the author says it's an empirical value If Gap > 1 Then Gap = (10 * Gap) \ 13 ' another empirical value If (Gap = 9 Or Gap = 10) Then Gap = 11 Swap = False For index = firstItem To numEls - Gap Quote: >>>>> value = arr(index)<<<<<<<<<<<<<<<<<<<<<
If (value > arr(index + Gap)) Xor Descending Then ' if the items are not in order, swap them arr(index) = arr(index + Gap) arr(index + Gap) = value Swap = True End If Next Loop End Sub
|
Fri, 02 Dec 2005 22:34:42 GMT |
|
 |
Doug Robbins - Word MV #14 / 14
|
 X-Posted: Can I create a drop down list that will let me select an item
Hi Dylan, The easiest thing to do is create a query in Access that does the sorting and use the method that I gave you to load the data from that query rather then from the table. Please respond to the newsgroups for the benefit of others who may be interested. Hope this helps Doug Robbins - Word MVP
I keep getting error messages when trying to operate the sorting routine. I'm using the following code and I get an error at the line highlighted, stating that "value = arr(index)=<Subscript out of Range>" any idea where I'm going wrong? Sub CombSort(arr As Variant, Optional numEls As Variant, _ Optional Descending As Boolean) Dim value As Variant Dim index As Long Dim firstItem As Long Dim Gap As Long Dim Swap As Boolean ' account for optional arguments If IsMissing(numEls) Then numEls = UBound(arr) firstItem = LBound(arr) Gap = numEls - firstItem + 1 Swap = False Do While (Gap > 1 Or Swap) ' divide Gap by 1.3 - the author says it's an empirical value If Gap > 1 Then Gap = (10 * Gap) \ 13 ' another empirical value If (Gap = 9 Or Gap = 10) Then Gap = 11 Swap = False For index = firstItem To numEls - Gap Quote: >>>>> value = arr(index)<<<<<<<<<<<<<<<<<<<<<
If (value > arr(index + Gap)) Xor Descending Then ' if the items are not in order, swap them arr(index) = arr(index + Gap) arr(index + Gap) = value Swap = True End If Next Loop End Sub
|
Sat, 03 Dec 2005 06:19:15 GMT |
|
|
|