PDA

View Full Version : How to obtain the file path to a shortcut from which ActiveDocument was launched?



GeoffreyB
02-09-2011, 06:35 AM
I am building a document management system. In each subdirectory within the main document repository, there is a shortcut to a document template. When a user wants to create a new document, he/she navigates to the appropriate subdirectory for the document and clicks the shortcut.


When the user clicks the shortcut, MS Word is launched, showing the new document based on the template. When the user clicks Save, the document will normally be saved to the current directory which will be the default location for new documents specified in Word Options.


I want the current directory of a new document to be the same as the directory of the shortcut from which it was created. The user has already decided where the document is to be located by navigating to the appropriate subdirectory and clicking the shortcut. It should not be necessary for the user to navigate to the same location again within the Save As... dialog.


If I can obtain the path to the shortcut, I can programmatically save the document to the same directory or a new subdirectory.


Also asked here: http://stackoverflow.com/questions/4945288/how-do-i-use-vba-to-obtain-the-file-path-to-a-shortcut-from-which-the-activedocum

Tinbendr
02-09-2011, 08:55 AM
Maybe Debug.Print Application.Templates(ActiveDocument.AttachedTemplate).Path

David

fumei
02-10-2011, 02:08 PM
Careful. That would give the path to the template itself. This may, or may not, be the path to the shortcut. It depends on whether it really is a shortcut.

If the sub-folder has the template file itself, then clicking on it is NOT clicking on a shortcut.

Frosty
02-17-2011, 12:00 PM
I don't think what you're asking is possible from within the VBA environment. Word doesn't know that a shortcut launched it, as the shortcut is something in the file system... Word just knows that a Document New call was placed to the template.

You would probably need to set up some kind of batch file which calls the template and also writes the current directory into some location (regkey, textfile, etc). Then within VBA you could obviously check the regkey/text file values to see where the batch file wrote from.

But that kind of "exactly where did I start from" info doesn't get passed along to Word, it just knows what document is open, and what (if any) the attached template is.

Hope that helps.

fumei
02-17-2011, 12:23 PM
If it was, in fact, a shortcut. We do not know that for a fact.

We need to know what EXACTLY is happening. Why would you put a shortcut - a real Windows shortcut - in the folder like that? But, if it is one, then the issue I stated, and restated (better) by Frosty applies.

Using VBA to get the path to the template gets the path to the template, NOT the path to the shortcut. If the folder that contains the (Windows) shortcut is what is needed, then VBA by itself can not get it. Even using a batch file, while it would work, would probably need to be hard-coded - ie. a shortcut in U:\Whatever\Yadda would need a batch file that explicitly had U:\Whatever\Yadda.

THAT could rapidly become a pain.

So. Is it a real shortcut, or not?