Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sharon3874 - 03 Nov 2006 17:59 GMT
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.
Jonathan West - 03 Nov 2006 19:03 GMT
>I am new in VBA. I am sorry in advance if my question doesn't make
> sense.
[quoted text clipped - 7 lines]
> 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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

sharon3874 - 03 Nov 2006 19:41 GMT
Jonathan,
Thank you very much!!!

I appreciate your help!
sharon3874 - 03 Nov 2006 19:43 GMT
I actually used ActiveDocument.Path, but I believe your way should work
out too.
Jonathan West - 03 Nov 2006 19:51 GMT
>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.

Signature

Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.