Browse a file

S

Shauno

I am creating an inport routine for project that will open an XML file and
parse the contents to produce a custom project plan.
All works perfectly, HOWEVER i need to provide a way for the user
to browse for the file to use.
I am looking for a standard file open dialog but clearly dont want project
to try and then open the file itself - just give me the name and path.

Is this possible- i can't find a way ?
 
E

Eren Ersonmez

Shauno-

Normally, I would suggest that you use a common dialog box control in a
form, but I am not sure that it is available in VBA. But you can copy paste
the code below in a separate module and then you will have a GetXMLFilePath
funtion at your disposal. Then you can use it like:

myXmlFilePath = GetXmlFilePath("Please browse to the XML file")

Here's the code:

----------------------------------
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
strFilter As String
strCustomFilter As String
lMaxCustFilter As Long
lFilterIndex As Long
strFile As String
lMaxFile As Long
strFileTitle As String
lMaxFileTitle As Long
strInitialDir As String
strTitle As String
lFlags As Long
iFileOffset As Integer
iFileExtension As Integer
strDefExt As String
lCustData As Long
lpfnHook As Long
strTemplateName As String
End Type

Private Function GetFilePath(Filter As String, Title As String, Optional
DefaultDir As String) As String

Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim i As Integer

OpenFile.lStructSize = Len(OpenFile)
'OpenFile.hwndOwner = Application.h
OpenFile.hInstance = 0
OpenFile.strFilter = Filter
OpenFile.lFilterIndex = 1
OpenFile.strFile = String(257, 0)
OpenFile.lMaxFile = Len(OpenFile.strFile) - 1
OpenFile.strFileTitle = OpenFile.strFile
OpenFile.lMaxFileTitle = OpenFile.lMaxFile
If IsMissing(DefaultDir) Or (DefaultDir = "") Then
OpenFile.strInitialDir = CurDir
Else
OpenFile.strInitialDir = DefaultDir
End If
OpenFile.strTitle = Title
OpenFile.lFlags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
GetFilePath = ""
Else
i = InStr(OpenFile.strFile, vbNullChar)
If i > 0 Then
GetFilePath = Left(OpenFile.strFile, i - 1)
Else
GetFilePath = OpenFile.strFile
End If
End If
End Function
Function GetXMLFilePath(Title As String, Optional Path As String) As String
GetXMLFilePath = GetFilePath("XML (*.xml)" & Chr(0) & "*.xml" & Chr(0),
Title, Path)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top