L
Lindsay Gillis
I'm using code to call the OpenFile API which allows the
user to point and click on a file name. I would like to
then assign that file name to be the value of a text box,
but I can't figure out how to strip away the full path
name before the file name (plus all the Chr(0)
characters). Here's the code I found on the Microsoft
knowledge base:
Option Explicit
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 cmdBrowse1_Click() ' my command button
On Error GoTo Err_cmdBrowse1_Click
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim TrimStr As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Form.Hwnd
'OpenFile.hInstance = App.hInstance (I had to
comment this out because the "app." returned a variable
not defined error - seems to work without it)
sFilter = "Image Files (*.jpg)" & Chr(0)
& "*.JPG" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Use the Comdlg API not the
OCX"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
Me.txtImage1 = Chr(34) & OpenFile.lpstrTitle & Chr
(34) ' this is my text box
If lReturn = 0 Then
MsgBox "The User pressed the Cancel Button"
Else
MsgBox "The user Chose " & Trim
(OpenFile.lpstrFile)
End If
user to point and click on a file name. I would like to
then assign that file name to be the value of a text box,
but I can't figure out how to strip away the full path
name before the file name (plus all the Chr(0)
characters). Here's the code I found on the Microsoft
knowledge base:
Option Explicit
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 cmdBrowse1_Click() ' my command button
On Error GoTo Err_cmdBrowse1_Click
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
Dim TrimStr As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Form.Hwnd
'OpenFile.hInstance = App.hInstance (I had to
comment this out because the "app." returned a variable
not defined error - seems to work without it)
sFilter = "Image Files (*.jpg)" & Chr(0)
& "*.JPG" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Use the Comdlg API not the
OCX"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
Me.txtImage1 = Chr(34) & OpenFile.lpstrTitle & Chr
(34) ' this is my text box
If lReturn = 0 Then
MsgBox "The User pressed the Cancel Button"
Else
MsgBox "The user Chose " & Trim
(OpenFile.lpstrFile)
End If