My Google-Fu is weak today, hopefully this is a simple thing.
I need to set the InitDir property of a VB6 CommonDialog control to start at [My] Computer. If I set InitDir to an empty string, it just defaults to the current directory from the last open dialog.
My code:
With MyCommonDialogControl
.DialogTitle = "Choose Import File"
.Filter = "Import Files|*.dbf"
.InitDir = Environ("HOMEDRIVE") //Needs to be "My Computer"
.CancelError = False
.ShowOpen
If Len(.Filename) = 0 Then Exit Sub
InputFile = .Filename
End With
Thank you in advance for any assistance.
I’ve come across a couple of ways to do it – one is through the Environ method which appears to work in both VB6 and VBA – although I’ve never used this method, the other is through p/Invoke referencing: SHGetSpecialFolderLocation and SHGetPathFromIDList in the shell32.dll.
I didn’t have the code to hand, so I’ve copied and pasted from another site http://en.kioskea.net/faq/sujet-951-vba-vb6-my-documents-environment-variables
I can’t guarantee the correctness, but it looks very similar to code I’ve used in the past, so it should work with minimal debugging… anyway, at least it points you in the right direction.
Referencing Rep_Documents() will give you a string holding the path name of the My Documents folder. It’s just a case of assigning it to the file dialog’s InitDir property.