hungpecc1
Thành viên gắn bó
![](/diendan/data/PhoToDanhHieu/pip.gif)
![](/diendan/data/PhoToDanhHieu/pip.gif)
![](/diendan/data/PhoToDanhHieu/pip.gif)
- Tham gia
- 24/8/12
- Bài viết
- 1,709
- Được thích
- 2,304
- Giới tính
- Nam
Do một số ứng dụng khác không có hàm getopenfilename ---> em dùng hàm Api để tạo hộp thoại open file !
Tuy nhiên kết quả tên file lại khác với tên file khi sử dụng gàm getopenfilename của excel ( chênh nhau 1 ký tự)---> em tìm hiểu mãi mà chưa ra được nguyên nhân, mong các anh chị trên diễn đàn giải thích đoạn code của em sai chỗ nào !!
code của em như sau:
anh (chị) paste code sau vào 1 module --> chạy sub main, ??? không hiểu sao cùng 1 file mà strcomp lại ra kết quả -1
Thanks!
Tuy nhiên kết quả tên file lại khác với tên file khi sử dụng gàm getopenfilename của excel ( chênh nhau 1 ký tự)---> em tìm hiểu mãi mà chưa ra được nguyên nhân, mong các anh chị trên diễn đàn giải thích đoạn code của em sai chỗ nào !!
code của em như sau:
anh (chị) paste code sau vào 1 module --> chạy sub main, ??? không hiểu sao cùng 1 file mà strcomp lại ra kết quả -1
Mã:
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Type OPENFILENAME
lStructSize As Long 'The size of this struct (Use the Len function)
hwndOwner As Long 'The hWnd of the owner window. The dialog will be modal to this window
hInstance As Long 'The instance of the calling thread. You can use the App.hInstance here.
lpstrFilter As String 'Use this to filter what files are showen in the dialog. Separate each filter with Chr$(0). The string also has to end with a Chr(0).
lpstrCustomFilter As String 'The pattern the user has choosed is saved here if you pass a non empty string. I never use this one
nMaxCustFilter As Long 'The maximum saved custom filters. Since I never use the lpstrCustomFilter I always pass 0 to this.
nFilterIndex As Long 'What filter (of lpstrFilter) is showed when the user opens the dialog.
lpstrFile As String 'The path and name of the file the user has chosed. This must be at least MAX_PATH (260) character long.
nMaxFile As Long 'The length of lpstrFile + 1
lpstrFileTitle As String 'The name of the file. Should be MAX_PATH character long
nMaxFileTitle As Long 'The length of lpstrFileTitle + 1
lpstrInitialDir As String 'The path to the initial path :) If you pass an empty string the initial path is the current path.
lpstrTitle As String 'The caption of the dialog.
flags As Long 'Flags. See the values in MSDN Library (you can look at the flags property of the common dialog control)
nFileOffset As Integer 'Points to the what character in lpstrFile where the actual filename begins (zero based)
nFileExtension As Integer 'Same as nFileOffset except that it points to the file extention.
lpstrDefExt As String 'Can contain the extention Windows should add to a file if the user doesn't provide one (used with the GetSaveFileName API function)
lCustData As Long 'Only used if you provide a Hook procedure (Making a Hook procedure is pretty messy in VB.
lpfnHook As Long 'Pointer to the hook procedure.
lpTemplateName As String 'A string that contains a dialog template resource name. Only used with the hook procedure.
End Type
'=========================================================================
Public Function ShowOpen(Filter As String, InitialDir As String, DialogTitle As String) As String
Dim OFName As OPENFILENAME
OFName.lStructSize = Len(OFName)
OFName.hwndOwner = 0
OFName.lpstrFilter = Filter
OFName.nMaxFile = 255
OFName.lpstrFile = Space$(254)
OFName.lpstrFileTitle = Space$(254)
OFName.nMaxFileTitle = 255
OFName.lpstrInitialDir = InitialDir
OFName.lpstrTitle = DialogTitle
OFName.flags = 0
If GetOpenFileName(OFName) Then
ShowOpen = Trim(OFName.lpstrFile)
Else
ShowOpen = ""
End If
End Function
'================================================================
[B]Sub Main()[/B]
Dim s1$, s2$
s1 = Application.GetOpenFileName()
s2 = ShowOpen("All File(*.*)" + Chr(0) + "*.*" + Chr(0), "C:\", "Test")
[I][B]MsgBox StrComp(s1, s2[/B][/I])
[B]End Sub[/B]
Thanks!
Lần chỉnh sửa cuối: