command for listing documents in a LisBox

B

Benji

Ok sorry I have alot of questions.
I want to list documents in a listbox based on the filename. When the user
clicks on the document and hits ok they are brought right to the document
and it is then
the document on screen and the form disappears.
I believe the command I might be wanting to use is ActiveDocument?
The documents are actually on a network server on our z: drive under forms
folder
so the path would be z:\forms\documents name


Thanks
 
D

DA

Hi Benji

Refer to Jay's answer to your other question on how to
get array items into an array, then refer to this article
which shows you how to populate your list box.

http://word.mvps.org/faqs/userforms/LoadListBoxIntoArray.h
tm

The command to open a selected document from your list
box would be like this:

Documents.Open FileName:= <formName>.<ListBoxName>.Value

Hope that helps,
Dennis
 
B

Benji

DA,

Thanks
when I use the Documents.Open command I am getting an error. I am using this
on a OK command button under my form. I want the users to be able to select
the
file and then choose ok to open it but I am getting an error. I have the
correct information in the command but I think I have a problem with .value
is that supposeto be something specific ie if its 1st in the list will it be
1 ? or is it always just value

Thanks
 
B

Benji

Thanks Doug,

I dont actually want to see which one is selected but the one that is
selected I want to hit an ok button and run word to that particular document
and then have the document become the forescreen and the form disapear
without putting the document on the task bar. ANy help appreciated

thanks
Ryan
 
M

Malcolm Smith

Which bit exactly are you having problems with? The listing of the
documents or the opening of the document?

Is one allowed to select more than one document?
Does the document list list documents in the sub folders?

- Malc
www.dragondrop.com
 
B

Benji

Malcolm,

The list does infact list documents from a subfolder and the user is only
allowed to select one document. I am having problems with my ok cmd button.
Where I want the user to be able to hit the ok button on the form and have
the document open in word then hid or make the form disappear without
putting the document that just opened in word immediately on the task bar.

so opening the document that is selected with word making it the present
windows and hidding the or disappearing the form.

THanks
 
M

Malcolm Smith

I'm not sure about the bit where you don't want the document appear in the
task bar.

But, assume that you have the following code in a start-up template and
the action is triggered by a toolbar button or a key-chord.

This code is written straight off the top of my head, so excuse the odd
compilation error.

This should do what you wish.

- Malc
www.dragondrop.com



Sub OpenFileSelectionForm()

dim oForm as clsSelectDocument
dim sDocument as string
dim oDoc as Document


set oForm = New clsSelectDocument
oForm.Tag = "Cancel" ' Where we are going to return the user response
oForm.Show
'------------
if oForm.Tag = "OK" then
if oForm.lstSelectDocument.ListIndex >= 0 then
sDocument = oForm.lstSelectDocument.Value
end if
end if
unload oForm
set oForm = Nothing

if len(sDocument)> 0 then
set oDoc = Documents.Open sDocument
if not oDoc is nothing then
oDoc.Activate
end if
end if

end sub


' In the form class:

Private sub clsSelectDocument_Initialize()

' Populate the listbox, lstSelectDocument, with the documents paths here

end sub

private sub cmdOK_Click()

Me.Tag = "OK"
Me.Hide

end sub

private sub cmdCancel_Click()

Me.Hide

end sub
 
B

Benji

Malcolm

As you may have guessed I am kinda lost none the less you say
populate the listbox with 1stselectdocuments. Im not sure how to do this
will this work

' Populate the listbox, lstSelectDocument, with the documents paths here
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)

LisBox1.list = DirectoryListArray

===============

Thanks
Ryan
 
M

Malcolm Smith

I asked which bits you were having problems with. You didn't mention the
listing of the documents! All you mentioned was that you had a problem
with the OK button!

All you need to do is to write a recursive Dir$() function to go through
all of the folders and sub-folders.

I wouldn't bother with the array; I would just add to the List with the
..AddItem command.

There is one scary part in your code where you define 1000 elements in the
array. What happens if there are 2000 documents? What happens after five
years there are 100,000 documents?

You may have to consider the problem a little more carefully.

- Malc
www.dragondrop.com
 

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