PDA

View Full Version : [SOLVED:] Cannot change directory for GetOpenFilename



indyman
09-06-2014, 12:11 PM
I am trying to read data from an Excel workbook into Word document. I have Office 2010 and WIn7. Here is the macro I have started in the Word document. I want to have the GetOpenFilename have the default directory of the active document be the starting directory of the File Open dialogue box. I have searched for hours for a solution, but none of them work. Every time it opens to the Libraries -> Documents folder, even though the msgbox clearly shows the strDefaultPath variable being set to the desired path. The desired path is on my G Drive. This should be simple, sowhat am I missing"


Sub ReadExcel()

Set appExcel = CreateObject("Excel.Application")

Dim Data_File As Variant

strDefaultPath = ActiveDocument.Path

ChDrive Left(strDefaultPath, 1)
ChDir strDefaultPath

MsgBox strDefaultPath

Data_File = appExcel.GetOpenFilename("Excel files (*.xlsx), *.xlsx")

End Sub

Any help is greatly appreciated.

snb
09-06-2014, 01:26 PM
Sub M_snb()
With Application.FileDialog(1)
.InitialFileName = "G:\OF"
.Filters.Add "Excel files", "*.xlsx; *.xlsm", 1
.FilterIndex = 1
.Show
c00 = .SelectedItems(1)
End With
End Sub

indyman
09-06-2014, 05:59 PM
Thaks snb, that will work for a fixed location. However, I am hoping for something that can dynamically understand what directory my active document is in. THe folder and drive will nto remain constant over time.

snb
09-07-2014, 02:29 AM
Sub M_snb()
With Application.FileDialog(1)
.InitialFileName = thisdocument.path
.Filters.Add "Excel files", "*.xlsx; *.xlsm", 1
.FilterIndex = 1
.Show
c00 = .SelectedItems(1)
End With
End Sub

indyman
09-08-2014, 04:02 PM
Thanks snb. That works perfectly, which I am sure does not surprise you. I had no idea that .initialFileName could be a path.