I am new at this and would really appreciate if you can help me with the problem below.
here is the code...
Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameW" (pOpenFileName As OPENFILENAME) As Long
Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim openFile As OPENFILENAME
Function GetOpenFile()
Dim APIResults As Long
Dim sFilter As String
'I don't know what goes next here, but I want it to return the filepath AND the filename. I don't want to filter out any files (I want to view all of the files). Hope I don't confused you.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Go here http://www.mentalis.org/apilist/GetOpenFileName.shtml and click on the "Open Dialog" link at the bottom of the page in the examples section. There is also an example for common dialogs that you may find useful.
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
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
'Set the parent window
OFName.hwndOwner = Me.hWnd
'Set the application's instance
OFName.hInstance = App.hInstance
'Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Open File - KPD-Team 1998"
'No flags
OFName.flags = 0
'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox "File to Open: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub
0
JamesBingAuthor Commented:
what's Me.Hwnd? I got a compile error of Invalid use of Me Keyword
This here comes into the declarations
----------------------------------------------------------------------
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustomFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Dim tpyOpen As OPENFILENAME
Dim lngRetval As Long
Dim blnRetrievalIsNull As Boolean
With tpyOpen
.lStructSize = Len(tpyOpen)
.flags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST
.lpstrTitle = "Open..." & vbNullChar
.lpstrFilter = "All Files" & vbNullChar & "*.*" & vbNullChar
.nFilterIndex = 1
.lpstrFile = Space(1024) & vbNullChar
.nMaxFile = Len(.lpstrFile)
.lpstrFileTitle = Space(1024) & vbNullChar
.nMaxFileTitle = Len(.lpstrFileTitle)
.lpstrInitialDir = "C:\" & vbNullChar
End With
lngRetval = GetOpenFileName(tpyOpen)
OpenFileDialog = Trim(tpyOpen.lpstrFile)
' if you want to do a multiselect you will need to edit the output you get
blnRetrievalIsNull = lngRetval = 0
If blnRetrievalIsNull Then
Exit Function
End If
End Function
----------------------------------------------------------------------
0
0MoJoHCommented:
In response to Don-Acme's code,
I get the OpenDialog but the selected file doesn't open...
I copied-pasted the code in a new module and "call" it from a click event in Excel 97.
0
Don-AcmeCommented:
Hi 0MoJoH,
I checked my code and actually it should be working fine on any type of Windows.
I extended the code alot and I've created a very simple class for the use within our company so that nobody has to rewrite it... I'm still looking for the documentation since I've saved it somewhere and I cannot find it...
Hope you can use this code here:
PS: the lines that are set to Remarks are really not necessary for a simple Excel application.
PSPS: this code should be more a guideline to write your own it is definetly not the best you can get
Usage:
--------
1. Create a new class in excel, VB or whatever
2. paste the source code into the class
3. You can leave the default settings for the properties
4. try if you can open any file you need to.
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (lpofn As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias _
"GetSaveFileNameA" (lpofn As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustomFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
Dim m_strDefaultInitialDrive As String
Dim m_strDefaultOpenFileTitle As String
Dim m_strDefaultSaveFileTitle As String
Dim m_strDefaultFileType As String
Dim m_hexOpenFileFlags
Dim m_hexSaveFileFlags
Dim m_lngDefaultWindowHandle As Long
Dim m_lngDefaultExtensionIndex As Long
Dim m_lngDefaultEmptyStringForFile As Long
Public Property Let DefaultInitialDrive(ByVal strDefaultInitialDrive As String)
Dim blnIsNotDrive As Boolean
blnIsNotDrive = InStr(strDefaultInitialDrive, ":\") = 0
If blnIsNotDrive Then
strDefaultInitialDrive = "C:\" & vbNullChar
End If
m_strDefaultInitialDrive = strDefaultInitialDrive & vbNullChar
End Property
Public Property Get DefaultInitialDrive() As String
Thanks for your help.
This updated code is 5 times harder to understand than the initial one that you posted.
I guess I will have a lot of fun decrypting your "simple" class.
Ok.
I created a new class and pasted the code source.
Now I have no idea how to use it.
I mean,... following JamesBing's idea, the objective was to allow the user to browse and open a file via a dialog box.
How do I call the OpenFile function from a button ?
Which variable will be containing the name of the file that has been selected by the user to open ?
My god I wish my company was using Excel 2003 instead of 97.........
0
Don-AcmeCommented:
Hey 0MoJoH,
there are some properties, that will let you set specific settings for your Open File Button
This procedure here:
contains the default settings when you call the class.
-------------------------------------------------
Private Sub Class_Initialize()
m_strDefaultInitialDrive = "C:\" & vbNullChar
m_strDefaultOpenFileTitle = "Open" & vbNullChar
m_strDefaultSaveFileTitle = "Save As..." & vbNullChar
m_strDefaultFileType = "All Files" & vbNullChar & "*.*" & vbNullChar & vbNullChar
m_hexOpenFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST
m_hexSaveFileFlags = OFN_ENABLE_SIZING Or OFN_ENABLE_EXPLORER_STYLE Or OFN_ENABLE_LONG_FILENAMES Or _
OFN_ENABLE_PATH_MUST_EXIST Or OFN_ENABLE_FILE_MUST_EXIST Or _
OFN_ENABLE_OVERWRITE_PROMPT
m_lngDefaultWindowHandle = 0
m_lngDefaultExtensionIndex = 1
m_lngDefaultEmptyStringForFile = 1024
End Sub
-------------------------------------------------
The variable names are set to the same names as the properties you have.
-------------------------------------------------
To open a file use: OpenFileDialog
To save a file use: SaveFileDialog
-------------------------------------------------
-------------------------------------------------
The Enum "OFN_FLAGS" contains some settings for your window. I have chosen some settings
from the windows 2000 environment
regards...
The Don
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
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
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
'Set the parent window
OFName.hwndOwner = Me.hWnd
'Set the application's instance
OFName.hInstance = App.hInstance
'Select a filter
OFName.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) + "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
'create a buffer for the file
OFName.lpstrFile = Space$(254)
'set the maximum length of a returned file
OFName.nMaxFile = 255
'Create a buffer for the file title
OFName.lpstrFileTitle = Space$(254)
'Set the maximum length of a returned file title
OFName.nMaxFileTitle = 255
'Set the initial directory
OFName.lpstrInitialDir = "C:\"
'Set the title
OFName.lpstrTitle = "Open File - KPD-Team 1998"
'No flags
OFName.flags = 0
'Show the 'Open File'-dialog
If GetOpenFileName(OFName) Then
MsgBox "File to Open: " + Trim$(OFName.lpstrFile)
Else
MsgBox "Cancel was pressed"
End If
End Sub