ช่วยด้วยครับ 32bit API Open/Save dialog function
กระทู้เก่าบอร์ด อ.Yeadram

 3,174   11
URL.หัวข้อ / URL
ช่วยด้วยครับ 32bit API Open/Save dialog function

Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _     Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _     Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long

คือผมอยากให้มันเปืดได้กับงินโด้ 64 bit อ่ะครับ เครื่องผมเป็น วินโด้7,64bit อ่ะครับ
ผมต้องแก้ตรงไหนครับ

11 Reply in this Topic. Dispaly 1 pages and you are on page number 1

1 @R17204
http://stackoverflow.com/questions/4746938/is-there-an-open-file-dialog-for-access-2010-64bit

ลองเปลี่ยนเป็นใช้ MS Office Dialog แทนครับ
โดย Add Reference Microsoft Office 14.0 Object library ก่อน
แล้วใน code ใช้ function Application.FileDialog(msoFileDialogFilePicker)
2 @R17205
MS Office Dialog ใช้ได้กับทั้ง 32, 64 bit
3 @R17208
1. เพิ่มเงื่อนไขแยกระหว่าง 32bit กับ 64bit ด้วย #If...#Else...#End If
2. เพิ่ม PtrSafe หน้า Function
3. เปลี่ยน Type จาก Long เป็น LongPtr บางตัว ตามตัวอย่าง

#If VBA7 Then 'แก้ตามบรรทัดต่อไปนี้ นอกนั้นเหมือนเดิม
    Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" _     Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" _     Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
...
...
    Type tagOPENFILENAME
        hwndOwner As LongPtr
        hInstance As LongPtr
        lpfnHook As LongPtr
        ...
...
#Else '32bit ทุกอย่างเหมือนเดิม
    Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" _     Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" _     Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
...
...
#End If
4 @R17214
ขอบคุณครับ เดี๋ยวลองไปทำก่อนนะครับ ผลเป็นยังไงเดี๋ยวมาแจ้งให้ทราบครับ
ขอบคุณครับ
5 @R17216
ปัญหาข้างบน โอเคละครับ ติดอีกที่อ่ะครับ ตามรูปเลยครับ

6 @R17218
อื่ม เหมือนคุณใส่ #If...#End If ไม่ถูกหรือเปล่าครับ
เอางี้ครับไว้เดี๋ยวผมโพสโค๊สเต็มให้ดูเลยดีกว่า เพราะผมลืมไปว่า หลังจากเปลี่ยน type จาก Long เป็น LongPtr การใช้ฟังก์ชั่น Len() จะต้องเปลี่ยนเป็น LenB() ด้วย ไม่งั้นค่าจะผิดอีก รอก่อนแล้วกันครับ
7 @R17219
Option Explicit

#If VBA7 Then
    Type tagOPENFILENAME
        lStructSize As Long
        hwndOwner As LongPtr '(64 bit)
        hInstance As LongPtr '(64 bit)
        strFilter As String
        strCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        strFile As String
        nMaxFile As Long
        strFileTitle As String
        nMaxFileTitle As Long
        strInitialDir As String
        strTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        strDefExt As String
        lCustData As Long
        lpfnHook As LongPtr '(64 bit)
        lpTemplateName As String
    End Type

    Declare PtrSafe Function aht_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare PtrSafe Function aht_apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare PtrSafe Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#Else
    Type tagOPENFILENAME
        lStructSize As Long
        hwndOwner As Long
        hInstance As Long
        strFilter As String
        strCustomFilter As String
        nMaxCustFilter As Long
        nFilterIndex As Long
        strFile As String
        nMaxFile As Long
        strFileTitle As String
        nMaxFileTitle As Long
        strInitialDir As String
        strTitle As String
        flags As Long
        nFileOffset As Integer
        nFileExtension As Integer
        strDefExt As String
        lCustData As Long
        lpfnHook As Long
        lpTemplateName As String
    End Type

    Declare Function aht_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function aht_apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (OFN As tagOPENFILENAME) As Boolean
    Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
#End If

Global Const ahtOFN_READONLY = &H1
Global Const ahtOFN_OVERWRITEPROMPT = &H2
Global Const ahtOFN_HIDEREADONLY = &H4
Global Const ahtOFN_NOCHANGEDIR = &H8
Global Const ahtOFN_SHOWHELP = &H10
Global Const ahtOFN_NOVALIDATE = &H100
Global Const ahtOFN_ALLOWMULTISELECT = &H200
Global Const ahtOFN_EXTENSIONDIFFERENT = &H400
Global Const ahtOFN_PATHMUSTEXIST = &H800
Global Const ahtOFN_FILEMUSTEXIST = &H1000
Global Const ahtOFN_CREATEPROMPT = &H2000
Global Const ahtOFN_SHAREAWARE = &H4000
Global Const ahtOFN_NOREADONLYRETURN = &H8000
Global Const ahtOFN_NOTESTFILECREATE = &H10000
Global Const ahtOFN_NONETWORKBUTTON = &H20000
Global Const ahtOFN_NOLONGNAMES = &H40000
Global Const ahtOFN_EXPLORER = &H80000
Global Const ahtOFN_NODEREFERENCELINKS = &H100000
Global Const ahtOFN_LONGNAMES = &H200000


Function GetOpenFile(Optional varDirectory As Variant, Optional varTitleForDialog As Variant) As Variant
Dim strFilter As String
Dim lngFlags As Long
Dim varFileName As Variant
    lngFlags = ahtOFN_FILEMUSTEXIST Or ahtOFN_HIDEREADONLY Or ahtOFN_NOCHANGEDIR
    If IsMissing(varDirectory) Then
        varDirectory = ""
    End If
    If IsMissing(varTitleForDialog) Then
        varTitleForDialog = "Please choose a Target File ..."
    End If

    strFilter = ahtAddFilterItem(strFilter, "All File (*.*)", "*.*")
    varFileName = ahtCommonFileOpenSave(OpenFile:=True, InitialDir:=varDirectory, Filter:=strFilter, flags:=lngFlags, DialogTitle:=varTitleForDialog)
    If Not IsNull(varFileName) Then
        varFileName = TrimNull(varFileName)
    End If
    GetOpenFile = varFileName
End Function

Function ahtAddFilterItem(strFilter As String, _
    strDescription As String, Optional varItem As Variant) As String
    If IsMissing(varItem) Then varItem = "*.*"
    ahtAddFilterItem = strFilter & _
               strDescription & vbNullChar & _
               varItem & vbNullChar
End Function

Private Function TrimNull(ByVal strItem As String) As String
Dim intPos As Integer
    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
        TrimNull = Left(strItem, intPos - 1)
    Else
        TrimNull = strItem
    End If
End Function

Function ahtCommonFileOpenSave( _
            Optional ByRef flags As Variant, _
            Optional ByVal InitialDir As Variant, _
            Optional ByVal Filter As Variant, _
            Optional ByVal FilterIndex As Variant, _
            Optional ByVal DefaultExt As Variant, _
            Optional ByVal FileName As Variant, _
            Optional ByVal DialogTitle As Variant, _
            Optional ByVal hwnd As Variant, _
            Optional ByVal OpenFile As Variant) As Variant
Dim OFN As tagOPENFILENAME
Dim strFileName As String
Dim strFileTitle As String
Dim fResult As Boolean
    If IsMissing(InitialDir) Then InitialDir = CurDir
    If IsMissing(Filter) Then Filter = ""
    If IsMissing(FilterIndex) Then FilterIndex = 1
    If IsMissing(flags) Then flags = 0&
    If IsMissing(DefaultExt) Then DefaultExt = ""
    If IsMissing(FileName) Then FileName = ""
    If IsMissing(DialogTitle) Then DialogTitle = ""
    If IsMissing(hwnd) Then hwnd = Application.hWndAccessApp
    If IsMissing(OpenFile) Then OpenFile = True
    strFileName = Left(FileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
    With OFN
        #If VBA7 Then
            .lStructSize = LenB(OFN) '(64 bit)
            .nMaxFile = LenB(strFileName) '(64 bit)
            .nMaxFileTitle = LenB(strFileTitle) '(64 bit)
        #Else
            .lStructSize = Len(OFN)
            .nMaxFile = Len(strFileName)
            .nMaxFileTitle = Len(strFileTitle)
        #End If
        .hwndOwner = hwnd
        .strFilter = Filter
        .nFilterIndex = FilterIndex
        .strFile = strFileName
        .strFileTitle = strFileTitle
        .strTitle = DialogTitle
        .flags = flags
        .strDefExt = DefaultExt
        .strInitialDir = InitialDir
        .hInstance = 0
        .lpfnHook = 0
        .strCustomFilter = String(255, 0)
        .nMaxCustFilter = 255
    End With

    If OpenFile Then
        fResult = aht_apiGetOpenFileName(OFN)
    Else
        fResult = aht_apiGetSaveFileName(OFN)
    End If

    If fResult Then
        If Not IsMissing(flags) Then flags = OFN.flags
        ahtCommonFileOpenSave = TrimNull(OFN.strFile)
    Else
        ahtCommonFileOpenSave = vbNullString
    End If
End Function
8 @R17220
อธิบายนิดหน่อยนะครับ การใส่เงื่อนไข #If VBA7 Then เป็นการตรวจสอบว่า MS Access นั้นใช้ VBA เวอร์ชั่น 7 หรือเปล่า ไม่ใช่เป็นการเช็คว่าเป็น 32bit หรือ 64bit
ผมไม่แน่ใจว่า MS Access 2007 เป็น VBA7 หรือเปล่า แต่หากคุณใช้ MS Access 2010 ขึ้นไป ไม่ว่าคุณจะใช้เป็น 32bit หรือ 64bit คุณไม่จำเป็นต้องใส่เงื่อนไข #If VBA7 ก็ได้ โดยการเรียก API มาใช้งานก็ให้ใส่เป็น Declare PtrSafe Function โดยมันก็จะรองรับทั้ง 32bit และ 64bit เอง เพียงแต่หากเราใช้งานโปรแกรมทั้งกับ MS Access 2003 ด้วย อย่างนี้ถึงต้องเขียน #If VBA7 แยกเอา ดังนั้นหากคุณมั่นใจว่าเครื่องใช้เป็น 2010 อย่างเดียว ไม่ว่าจะเป็น 32bit หรือ 64bit ก็เขียนเป็น Declare PtrSafe Function ได้เลยครับ
9 @R17264
ขอบคุรครับ
10 @R17265
เรียนคุณ TTT ครับ ผมขอสอบถามครับ ว่าหากผมต้องการนำชื่อฟิวที่ใช่เก็บที่อยู่ของรูป ผมควรนำชื่อฟิวนั้นไปใส่ตรงไหนของสูตรครับ
11 @R17269
ตัวอย่าง
1. สร้างการให้เรียกพาธที่ D:\Text
2. เมื่อเลือกไฟล์ในหน้าต่าง Dialog เสร็จ ให้เซฟชื่อไฟล์และพาธที่เลือกใน Text1

Private Sub Command0_Click()
    Text1 = GetOpenFile("D:\")
End Sub
@ ประกาศใช้งานเว็บบอร์ดใหม่ => บอร์ดเรียนรู้ Access สำหรับคนไทย
แล้วจะใส่ลิ้งอ้างอิงมาที่โพสต์เก่านี้หรือไม่ก็ตามสะดวกครับ
Time: 0.2989s