API-Call: GetOpenFileName-Problem

U

Udo Weik

Hello,

when I declare the function GetOpenFileName from comdlg32 in
a Visio-VBA-Script, I get the following compiler error:

Only comments may appear after End Sub, End Function, or End Property

Type OPENFILENAME is decalred at the head of the module.

Any hint?


Thanks in advance.
Udo


Code:

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

Sub GetFileFromAPI()

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

End Sub
 
J

JuneTheSecond

You would had declared API function at the declaration zone, for example:

Option Explicit
Private Declare Function GetOpenFileName Lib "comdlg32" Alias
"GetOpenFileNameA" (ByRef 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

Public Function GetFileName() As String
Dim file_str As OPENFILENAME
Dim lNULLPos As Long
Dim strFileName As String
Dim lRet As Long

GetFileName = ""

With file_str
.lStructSize = Len(file_str)
.lpstrInitialDir = CurDir()
.lpstrFilter = "BITMAP(*.bmp)" & vbNullChar & "*.bmp" & vbNullChar & _
"ALL(*.*)" & vbNullChar & "*.*"
.nMaxFile = 256
.lpstrFile = String(256, vbNullChar)
End With
lRet = GetOpenFileName(file_str)
lNULLPos = InStr(file_str.lpstrFile, vbNullChar)
strFileName = Left(file_str.lpstrFile, lNULLPos - 1)
If strFileName <> "" Then
GetFileName = strFileName
End If
End Function
 
J

JuneTheSecond

Please, add next proc to test the function.
Sub test()
Dim sFileName As String
Dim myDoc As Visio.Document
sFileName = GetFileName
Set myDoc = Application.Documents.OpenEx(sFileName, visOpenRW)
End Sub
 

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