View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
NickHK NickHK is offline
external usenet poster
 
Posts: 4,391
Default Problem with default file path

opi,
Depending the current drive, you may also need ChDrive. e.g.

Private Sub CommandButton1_Click()
Dim RetVal As Variant
MsgBox CurDir
ChDrive "C"
MsgBox CurDir
ChDrive "F"
ChDir "F:\Installs\"
RetVal = Application.GetOpenFilename()
MsgBox CurDir
End Sub

You obviously need valid drive and folder values for this to work.
This behaviour is why just get sharing violation if you try to delete the
folder pointed to by Application.GetOpenFilename(); because that folder is
set to the current directory.

NickHK

"opi" wrote in message
oups.com...
Hi everybody! My problem is this: I acces MS Excel from Visual Basic.
First I create Excel.Application object, and then I prompt user to open
a file. I do this with FindFile method. Upon execution of that method,
open dialog box is displayed which points to MyDocuments folder.
However, I would like to set the default path programatically.I tried
to do it by setting DefaultFilePath (and by doing chdir) before
executing FindFile, but it appears excel writes the default path to the
registry upon opening of the file, and only looks for that value.

For example: I set default path to c:\temp and open excel. Open dialog
points to c:\My Documents. I navigate to c:\temp and open any file from
there. Then I start my program again, I set default path to c:\work,
open dialog box now points to c:\temp because that is what excel has
written to registry
HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\E xcel\Options the last
time.

I have tried using GetOpenFilename but same thing happens. Is there a
workaround for this other than using CommonDialog control or writing my
path dinamically to the registry because none of that seems very
appealing...