MS Office Forum / Word / Programming / September 2005
File Open Dialog based on startup directory
|
|
Thread rating:  |
Anand - 19 Aug 2005 23:39 GMT 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.
Anne Troy - 20 Aug 2005 00:24 GMT Anand, storing documents in the same folder as program files is NOT a good idea. :) ************ Anne Troy www.OfficeArticles.com
> Hi, > [quoted text clipped - 9 lines] > Thanks, > Anand. Jezebel - 20 Aug 2005 00:42 GMT In theory,
ChangeFileOpenDirectory CurDir
not sure that it actually works, though.
> Hi, > [quoted text clipped - 9 lines] > Thanks, > Anand. Anand - 20 Aug 2005 03:12 GMT 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.
> In theory, > [quoted text clipped - 15 lines] > > Thanks, > > Anand. nc - 20 Aug 2005 11:48 GMT try sFilePath = ActiveDocument.Path this should return the path of the current active document.
Norm
>Thanks - but no, but this doesn't seem to work. This macro appears to set the >file-open directory to the value specified by [quoted text clipped - 31 lines] >> > Thanks, >> > Anand. Steve Yandl - 21 Aug 2005 00:53 GMT 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
> Thanks - but no, but this doesn't seem to work. This macro appears to set > the [quoted text clipped - 36 lines] >> > Thanks, >> > Anand. Steve Yandl - 21 Aug 2005 03:26 GMT 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
> Thanks - but no, but this doesn't seem to work. This macro appears to set > the [quoted text clipped - 36 lines] >> > Thanks, >> > Anand. Jezebel - 21 Aug 2005 03:55 GMT 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.
> 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 [quoted text clipped - 57 lines] >>> > Thanks, >>> > Anand. Steve Yandl - 21 Aug 2005 04:11 GMT 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,
>>> 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. 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
> 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 [quoted text clipped - 75 lines] >>>> > Thanks, >>>> > Anand. Jezebel - 21 Aug 2005 04:54 GMT 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.
Steve Yandl - 21 Aug 2005 05:19 GMT If you try the script I submitted; it uses the ChangeFileOpenDirectory method and it works as the original poster wanted (or the way we suspect he wanted). When he launches winword.exe from the command prompt, he leaves the cmd.exe or command.com process and winword.exe had no way to extract the path info about the current directory status of the command interpreter unless it is retained through a script or batch file.
Steve
>I think he might actually mean that he might want Word >> to recognize the current directory he was in under the command prompt [quoted text clipped - 3 lines] > 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. Jezebel - 21 Aug 2005 06:02 GMT 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.
> If you try the script I submitted; it uses the ChangeFileOpenDirectory > method and it works as the original poster wanted (or the way we suspect [quoted text clipped - 12 lines] >> 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. Steve Yandl - 21 Aug 2005 13:50 GMT 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
> 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, [quoted text clipped - 18 lines] >>> 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. Anand - 20 Sep 2005 22:16 GMT 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!
> 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 [quoted text clipped - 56 lines] > >> > Thanks, > >> > Anand.
|
|
|