Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Tue, 31 Jan 2006 11:09:49 +0530,    group: microsoft.public.word.vba.customization        back       


automatically populating VBA Word Form List box   
I have created a list box in a Word VBA form.  I want to populate the
list box with all the filenames that appear in a specific directory (eg
c:\my documents\precedents\"").  I want to be able to then add files to
this directory at any stage and know that the list box will
automatically pick these up.

The way I was taught to do this was to type each document name into
VBA, (eg with me.listbox additem (""), etc) however there must be a
faster and more efficient way.  There are 30+ files in the directory so
far and some 50 more to be added.

I also want to be able to create a new document based on the document
selected.  The way I was taught to do this was along the lines of:

select case me.listbox.text
case "blank document"
documents.add
template:=strPath & "Normal.dot",
newtemplate:=false
etc
end select

Again, this is a long way about it.  Is there a more efficient way?

Would making a database listing all the filenames and their paths be a
better way?

Thank you!


-- 
btcPosted from - http://www.officehelp.in
date: Tue, 31 Jan 2006 11:09:49 +0530   author:   btc

Re: automatically populating VBA Word Form List box   
The following code in the Initialize() event for the userform will display a 
dialog that allows you to select the folder and will then populate a listbox 
named lstfiles on the UserForm with the files that are in that folder.:

Dim MyPath As String
Dim MyName As String

'let user select a path
With Dialogs(wdDialogCopyFile)
    If .Display() <> -1 Then Exit Sub
    MyPath = .Directory
End With

'strip quotation marks from path

If Len(MyPath) = 0 Then Exit Sub

If Asc(MyPath) = 34 Then
    MyPath = Mid$(MyPath, 2, Len(MyPath) - 2)
End If

'get files from the selected path
'and insert them into the listbox lstfiles
MyName = Dir$(MyPath & "*.*")
Do While MyName <> ""
    lstfiles.AddItem MyName
    MyName = Dir
Loop

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

"btc"  wrote in message 
news:btc.22hj76@NoSpamPleaze.com...
>
> I have created a list box in a Word VBA form.  I want to populate the
> list box with all the filenames that appear in a specific directory (eg
> c:\my documents\precedents\"").  I want to be able to then add files to
> this directory at any stage and know that the list box will
> automatically pick these up.
>
> The way I was taught to do this was to type each document name into
> VBA, (eg with me.listbox additem (""), etc) however there must be a
> faster and more efficient way.  There are 30+ files in the directory so
> far and some 50 more to be added.
>
> I also want to be able to create a new document based on the document
> selected.  The way I was taught to do this was along the lines of:
>
> select case me.listbox.text
> case "blank document"
> documents.add
> template:=strPath & "Normal.dot",
> newtemplate:=false
> etc
> end select
>
> Again, this is a long way about it.  Is there a more efficient way?
>
> Would making a database listing all the filenames and their paths be a
> better way?
>
> Thank you!
>
>
> -- 
> btcPosted from - http://www.officehelp.in
>
date: Tue, 31 Jan 2006 13:32:32 +0100   author:   Doug Robbins - Word MVP

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us