File Open Dialog based on startup directory

A

Anand

Hi,

I'd like for word's File-Open & Save-As dialogs to point to the directory
from which I launched word. For example, I might just launch winword.exe from
the command line, say in directory: c:\abc\xyz in which case I'd like for
File-Open & Save-As to point to this directory. Could you point me to
suitable VBA source-code or inbuilt word menu options by which I can achieve
this ?

Thanks,
Anand.
 
A

Anand

Thanks - but no, but this doesn't seem to work. This macro appears to set the
file-open directory to the value specified by
Tools->Options->FileLocations->Startup

I realize now that my email title may be misleading since your solution does
refer to a startup directory. What I'm looking for is the directory from
which I launch winword.exe (assuming I launch it from the command line).
Suppose the following commands are executed:

c:\> cd abc\xyz
c:\abc\xyz> winword.exe (assuming winword.exe is in the system path)

Now I want the File-Open dialog box to point to c:\abc\xyz.

-Anand.
 
N

nc

try sFilePath = ActiveDocument.Path
this should return the path of the current active document.



Norm
 
S

Steve Yandl

Anand,

If I understand correctly what you want to do, I think you need to create a
script that would accept the current directory as an argument and then
launch an instance of the Word Application Object which could then be
manipulated to set its file open directory as the argument fed the script.
Assuming you're using cmd.exe on an XP or Win2000 or 2003 PC, you could use
%cd% as the argument to your script which would deliver the current
directory you were camped in with cmd.exe. Your script (a vbs script would
work fine) would use WScript.Arguments.Item(0) to return the argument which
would be the path string.

If you just wanted to know the directory winword.exe was launched from,
Application.Path would do it but I think you actually wanted to use the
current directory you were in immediately prior to launching Word.

Steve
 
S

Steve Yandl

Assuming my assumptions above are correct, try this. Create a new text file
named "myWord.vbs" and store in in your Windows folder or somewhere else in
the system path. Edit this text document to contain the following lines:

Dim fso, objWord
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists (WScript.Arguments.Item(0)) Then
MsgBox "You messed up the command line"
Else
Set objWord = CreateObject("Word.Application")
objWord.Application.ChangeFileOpenDirectory(Wscript.Arguments.Item(0))
objWord.Visible = True
End If

After saving the edited version of the vbs file, open cmd.exe. Change to
your preferred file open directory and then enter the line:
myWord "%cd%"
See if Word doesn't open with the default file open folder where you want.

Steve
 
J

Jezebel

Steve, I think you've missed the point of the thread. First, if the user is
starting Word from within a given directory, that directory necessarily
exists, so there's no point in checking for that. Second,
ChangeFileOpenDirectory doesn't work for the required purpose.

Try this:

1. Start Word from the command line, in a folder that Word doesn't normally
use for opening or saving.

2. In Word's VBA immediate window, type : ? CurDir. You'll see the name of
the folder you started Word from.

3. type: ChangeFileOpenDirectory CurDir

4. Switch back to Word's main window and use File > Open.
 
S

Steve Yandl

I understand exactly what you're saying and you're likely correct about what
the original poster is asking about. However, when I read the example that
the poster placed above my first post, I believe there is a chance that even
though he says he wants the file open directory to be the directory from
which he opened Word, I think he might actually mean that he might want Word
to recognize the current directory he was in under the command prompt
directory when he chose to launch Word.

He says,
I don't think he means that winword.exe is in c:\abc\xyz, rather, I think
c:\abc\xyz is an arbitrary path he wants to be able to navigate to and then
launch Word from the command prompt and have it recognize that he was in
c:\abc\xyz when he chose to do so.

Steve
 
J

Jezebel

I think he might actually mean that he might want Word
to recognize the current directory he was in under the command prompt
directory when he chose to launch Word.

That's what I think he meant, too. And although ChangeFileOpenDirectory
looks like it will set Word to use that directory, if you try it (as per my
example) you'll see it doesn't work for that purpose.
 
J

Jezebel

Perhaps we're talking at cross-purposes. Of course you can extract the path
info: if you launch Word from the command line, eg in folder c:\XYZ, then in
Word VBA the CurDir function reports the name of that folder -- "C:\XYZ" as
expected. It's the ChangeFileOpenDirectory command that doesn't work.
 
S

Steve Yandl

I tested the script before posting and the line:
objWord.Application.ChangeFileOpenDirectory(Wscript.Arguments.Item(0))

does in fact cause the file open directory to be set to whatever the current
directory was for the cmd.exe console window when Word was launched from
that window.

Steve
 
A

Anand

Thanks Steve. This worked out nicely. In addition to your vbscript, I created
another launch.bat file that says: myword "%cd%". So I dont have to type in
the extra "%cd%" either..

One small problem I have is that the instance of word window that gets
launched doesn't have focus.. I need to "click" on the word window before I
can say Crtl-N, Ctrl-O, etc.. pls let me know if this can be solved simply
within the script itself (clearly I'm not a VB guy :)

Thanks!
 

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