Changing the directory using ChDir

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Why does ChDir not change the directory in the following code?

Code:
    ChDrive "C"
    ChDir Nz("C:\Users\Aziz\Desktop", "")

    strFilePathandName = objExcelApp.Application.GetOpenFilename(Title:="SELECT MS EXCEL FILE", FileFilter:="Excel Files *.xls* (*.xls*),")
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
ChDir and ChDrive wouldn 't affect where Excel instance of GetOpenFilename method looks for. It would work only if you run the code in Excel.

You need to use Access equivalent to get the file name via file open dialog:

Note: If you don't like using Late Binding then you need to add Office Object Library and declare fileDialog As Office.FileDialog.

VBA Code:
Sub selectFile()
Dim fileDialog As Object
Dim strFilePathandName As String
    Set fileDialog = Application.fileDialog(3)
    With fileDialog
        .Title = "SELECT MS EXCEL FILE"
        .InitialFileName = "C:\Users\Aziz\Desktop"
        With .Filters
            .Clear
            .Add "Excel Files", "*.xls*"
        End With
        If .Show = True Then
            ' File is selected
            strFilePathandName = .SelectedItems(1)
        Else
            ' No file selected - cancel clicked
            Exit Sub
        End If
    End With
    MsgBox strFilePathandName
End Sub
 
Upvote 0
I am trying to open a password protected Excel file, but I want to pass the filename and password from another function. However I need a file dialog type function which starts from a predefined folder.

The line

Code:
If .Show = True Then

makes the password dialog appear, which I want to avoid.

Can't find an equivalent of GetOpenFilename in Access.
I want late binding code.
 
Upvote 0
The code I provided as sample is in FilePicker mode since I used 3 as the function parameter:
msoFileDialogFilePicker = 3

So, all it is supposed to do is returning the file name as string that you selected in the file dialog.
It works as it is supposed to in my computer.
 
Upvote 0
Will have a look and get back to you.

BTW, will the .Show line not bring up the Password dialog box, which I want to avoid?
 
Upvote 0
Unfortunately the .Show line makes the password dialog appear.

I want a string variable to accept the filepath and filename of a password protected MS Excel from an initial directory without the MS Excel file opening up.
 
Upvote 0
As I said, it works as you need in my computer. It doesn’t ask password or something (it shouldn’t). It simply returns the selected file name as string. I have no idea why it doesn’t work at your side.

Someone else might come up with better solution.
 
Upvote 0
smozgur, I know I may be asking a lot, but would you be able to send me a sample MS Access db with the code so I can see what is different to your outcome compared to mine?
 
Upvote 0
We shouldn't share any files for security purposes in public forums. However, it is easy to test the code as I explain below.

Just create a new database, and go to VBE and create a new module. Copy and paste the code I put above and run it by pressing F5 button or Run command on the menu. Do not add or use any of your code, only the code I sent above. That's it. This way, we'll see how it is working in your computer.
 
Upvote 0
Tried the code on a new Access db, and I get the same problem. The MS Excel window opens up along with the password dialog box. When I press cancel, I re-select the Excel file and this time the message box gives me the filename. Hence the initial issue still hasn't gone away.
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,139
Members
449,207
Latest member
VictorSiwiide

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top