Find all Open file names

G

George Seese

The "filename = Dir" can obtain file names in specific folders.

Is it possible to obtain similar info about files that are currently open?

Thanks,
George
 
M

Malcolm Smith

George

It certainly is. If you go through the Collection of Documents then you
will be able to do this.

For example:


Dim oDoc as Document

For each oDoc in Documents
msgbox oDoc.Name
Next oDoc


will bring up a message box with the name of each open document.

Hope that this helps

Malc
- www.dragondrop.com
 
G

George Seese

Thanks Malc,
A follow-up question:
Is there an event for the opening of a document?

If a form is being displayed, it's waiting for user input.
I (the code) would like to know when a new document is opened.

If there's no event, any ideas how to monitor the open documents every so
often and check for changes automatically?

I have WinXP and Word2000.
Thanks,
George
 
M

Malcolm Smith

Yes, there is code for this and it came in with Word 2000.

Let me dig out some code as this is something which I always fail to do
off the top of my head...

Okay, in Word 2000 and upwards create a CLASS module in one of your
start-up templates.

In this example call it 'clsApp'.

Put in the following code:


Option Explicit

Public WithEvents App As Word.Application


Private Sub App_DocumentOpen(ByVal Doc As Document)

msgbox "Hello, Hello, Hello. What's going on here then? Someone's
opened up a new document"

End Sub



Actually, just making the Public WithEvents declaration will allow you to
see 'App' in the left hand drop down control. In other words, you have
created your own event handling object.

Looking through the list of other available events to trap one can see a
list of rather useful and groovy events, such as DocumentChange which is
triggered when the user selects another document.

Anyway, have fun with this event and remember to create it in a class in a
start-up template. If you wish to take my advice; please don't put it
into Normal.dot (I won't mention why here as the flames from the last
religous wars haven't yet died down).

Hope this helps, if not then get back straight away to me.

Regards
Malc
www.dragondrop.com

Anyway, just drop
 
L

losmac

Yes, it's possible.

Dim doc as Document

For each doc in Documents
MsgBox doc.Name
Next doc
 
G

George Seese

Malc,
I'm having problems with your idea. I placed the sub into a new template.
The sub does not execute manually (cmd 8). The yellow highlight doesn't
appear.

Are you saying this code needs to be in a template in the Startup folder?
Does the message appear every time a file is opened - even when it's a
manual open?

When a file is opened and the message appears, is there a filename made
available?

-----------------
I've learned about AutoNew. In template, I have this code:
Sub AutoNew()
Msgbox "AutoNew works from Test1.dot"
End Sub
When a new document is created based on Test1.dot,
the msg appears.

When I do a similar thing for AutoOpen, nothing happens when I open another
document.
The Help for this says it runs "each time you open an existing document".
Does the document have to be based on the same template?

Thanks,
George
 
M

Malcolm Smith

Yes, you put it in a start-up template, i.e. one which lives in your
start-up folder.

Did you make the class as I suggested and put the code into the class?
If you still having trouble find my mail address on my site and I will
send you a start-up template which will do just as you wish.

- Malc
www.dragondrop.com
 
G

George Seese

Malc,
Thanks for the separate email.
The code works as advertised; it's what I was looking for.
I hope to understand how it works some day.
George
 

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