How to add a List Box to a existing macro

R

Ray

I would like to add a flist box to an existing macro in Word VBA.
I saw the process to get the list of files in a directory and then display
them in a list box
I can get everything to work except adding a list box to the existing macro
or get it to call the list box from the existing macro
I have attempted to add the list box and it will add a new seperate list box
that the Macro does not recognize.
Thanks
 
D

Default User

I've read this posting 3 times, and still can't figure out what it is y're
after.
Pls rephrase yr question also informing us:

a) are we talking about a listbox in a userform?
b) adding a listbox to a macro? pls clarify

Krgrds,
Perry
 
D

Doug Robbins - Word MVP

List boxes can't be added to macros as there is no place for them in macros.
They belong on userforms.

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

Ray

Ok I am sorry to be so unclear
I found this example on the Frequently Asked Questions - Microsoft Word MVP
FAQ Site
the information follows:
For instance, in order to populate a list box

Article contributed by Dave Rado

You can use the Dir$ function to do this. (Dir and Dir$ are functionally
identical, but Dir$ runs marginally faster).

Dir$ is a lot faster than using FileSearch, unless many subdirectories need
to be searched or you need to use the advanced features of FileSearch such as
the LastModified property.

Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("c:\temp\*.*")
Do While MyFile <> ""
DirectoryListArray(Counter) = MyFile
MyFile = Dir$
Counter = Counter + 1
Loop

'Reset the size of the array without losing its values by using Redim
Preserve
Redim Preserve DirectoryListArray(Counter - 1)

To prove it worked you could run the following:

For Counter = 0 To UBound(DirectoryListArray)
'Debug.Print writes the results to the Immediate window (press Ctrl + G
to view it)'
Debug.Print DirectoryListArray(Counter)
Next Counter

To populate a Listbox from the array you could use:

ListBox1.List = DirectoryListArray

I could figure out everything except the listbox1.list line
for the life of me I can not figure out how to get the list box to accept
and display the information

Thanks
 
D

Doug Robbins - Word MVP

Do you have a userform in your template with the listbox on the userform?
If that code is contained in the Initialize() event of the userform, it will
load the Listbox1 list box with the names of the files.

If all of the above seems strange,
See the article "How to create a Userform" at:

http://word.mvps.org/FAQs/Userforms/CreateAUserForm.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
 
R

Ray

That worked!
Thanks
Now another question - can you pass the information from a moduel to the
userform?
Or do you have to put all the code in the userform to make things like this
work?
Thanks
 
D

Doug Robbins - Word MVP

The code can be in the module that calls the user form as in the following

Dim oform As frmShowMergeFields
Set oform = New frmShowMergeFields
Dim fld As Word.MailMergeDataField
For Each fld In Doc.MailMerge.DataSource.DataFields
oform.lstMergeFields.AddItem fld.Name
Next fld
oform.Show vbModal


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

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