Word VBA: Why a macro reads a file from 2 different directories

S

sharon3874

I am new in VBA. I am sorry in advance if my question doesn't make
sense.

I have a word document and there is a button which trigger a macro. in
the macro, I want to get some info from reading another doc.

the name of the file is like "additional_links.doc", since I didn't
specify the path, so I expected that the macro should read the doc from
the current directory. Here is what confused me, sometimes the macro
read the file from the current directory, sometimes it read from
another directory(I suspect the directory was set somewhere).

It really bothers me that it is not consistent. It read from these 2
directories back and forth, and I couldn't see any pattern of that.

Maybe it does not relate to word VBA, but has anyone have this problem
before?

Thanks.
 
J

Jonathan West

sharon3874 said:
I am new in VBA. I am sorry in advance if my question doesn't make
sense.

I have a word document and there is a button which trigger a macro. in
the macro, I want to get some info from reading another doc.

the name of the file is like "additional_links.doc", since I didn't
specify the path, so I expected that the macro should read the doc from
the current directory. Here is what confused me, sometimes the macro
read the file from the current directory, sometimes it read from
another directory(I suspect the directory was set somewhere).

You need to understand the meaning of "the current directory". This is the
directory that will be displayed in the dialog if you go to File, Open. This
will either be My Documents, or the location of the last file you opened
through the File Open dialog.
It really bothers me that it is not consistent. It read from these 2
directories back and forth, and I couldn't see any pattern of that.

Maybe it does not relate to word VBA, but has anyone have this problem
before?

If you want to specify a particular folder relative to the file containing
the code, then you may find it useful to know that the
Application.MacroContainer.Path property gives you the folder of the file
that contains the currently running code. You can then use that to build a
fully qualified pathname for the file you are trying to reach.
 
S

sharon3874

I actually used ActiveDocument.Path, but I believe your way should work
out too.
 
J

Jonathan West

sharon3874 said:
I actually used ActiveDocument.Path, but I believe your way should work
out too.
Beware, ActiveDocument is the document that happens to be open in the
current Word editing window. That isn't necessarily the same as the document
containing the code, which might not be open in its own window at all.
 

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