laoliu09 发表于 2012-2-26 15:09:57

如何调用API函数打开文件保存对话框和文件打开对话框,返回文件路径???

如何调用API函数打开文件保存对话框和文件打开对话框,返回文件路径,网上找的放在VBA里面就用不了,我不用窗口,要直接在模块代码里面实现!到底要注意什么呢?谢谢大家!

skymudy 发表于 2012-3-4 08:24:06

(vlax-invoke-method WinShell 'BrowseForFolder 0 msg 1 0)
用它试试吧,前面再添加下面的这两句:
(vl-load-com)
(setq winshell (vlax-create-object "Shell.Application"))

laoliu09 发表于 2012-2-27 17:57:21

?不用控件,就有函数实现,大侠帮帮我啦!

skymudy 发表于 2012-3-4 08:25:38

代码里的msn是提示选择文件夹的字符串,自己设定

laoliu09 发表于 2012-3-16 15:17:07

已经搞定了!成功调用API函数打开和保存文件,呵呵!可以说是标准模式了!

ntyks 发表于 2013-7-4 21:33:37

wwswwswws 发表于 2013-9-22 09:53:07

       '打开文件对话框结构
      Public 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 Const DLG_SHOWOPEN = 1
Private Const DLG_SHOWSAVE = 2
Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
      "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long



Public Function GetDlgRtnFileName(ByVal iAction As Integer, vOpenFile As OPENFILENAME, ByVal hWndOw As Long, ByVal sFilter As String, _
                           ByVal sTitle As String, ByVal DefExt As String, ByVal InitFileName As String) As String '用API函数获得打开对话框和保存对话框返回的文件名

         Dim lReturn As Long
         
         vOpenFile.lStructSize = Len(vOpenFile)
         vOpenFile.hwndOwner = ThisDrawing.HWND32 ' hWndOw 'Form1.hwnd
'         vOpenFile.hInstance = app.hInstance

         vOpenFile.lpstrFilter = sFilter
         vOpenFile.nFilterIndex = 1
         If iAction = 2 Then
            vOpenFile.lpstrFile = InitFileName & String((257 - Len(InitFileName)), 0)
         Else
            vOpenFile.lpstrFile = String(257, 0)
         End If
         vOpenFile.nMaxFile = Len(vOpenFile.lpstrFile) - 1
         vOpenFile.lpstrFileTitle = vOpenFile.lpstrFile
         vOpenFile.nMaxFileTitle = vOpenFile.nMaxFile
         vOpenFile.lpstrInitialDir = ThisDrawing.Path '& "\"
         vOpenFile.lpstrTitle = sTitle '"使用对话框API而不是控件" '"Use the Comdlg API not the OCX"
         vOpenFile.Flags = 2
         vOpenFile.lpstrDefExt = DefExt
         Select Case iAction
                Case DLG_SHOWOPEN
                     lReturn = GetOpenFileName(vOpenFile)
                Case DLG_SHOWSAVE
                     lReturn = GetSaveFileName(vOpenFile)
                Case Else   'unknown action
                     Exit Function
         End Select

         If lReturn = 0 Then
            GetDlgRtnFileName = "Cancel" 'MsgBox "用户按下Cancel按钮" '"The User pressed the Cancel Button"
         Else
            GetDlgRtnFileName = Trim(Left(vOpenFile.lpstrFile, InStr(1, vOpenFile.lpstrFile, Chr$(0), vbBinaryCompare) - 1))

         End If
End Function

yuanziyou 发表于 2013-12-15 21:00:16

wwswwswws 发表于 2013-9-22 09:53 static/image/common/back.gif
'打开文件对话框结构
      Public Type OPENFILENAME
         lStructSize As Long


请问函数怎么用?
vOpenFile 代表什么?

wwswwswws 发表于 2013-12-17 09:37:13

这两个函数的参数就是这个结构,在用的时候使用者要定义一个变量,所以vOpenFile是定义的一个这个OPENFILENAME的变量,请参考下面来使用:

Dim vvOpenFile As OPENFILENAME '你的结构变量定义
Dim sSaveFileName As String'你取回的文件名
Dim sFileName As String         '初始化文件名(可以先初始化给定一个全路径文件名,也可以不给)
Dim ssFilter As String             '文件类型过滤

ssFilter = "所有文件(*.*)" & Chr(0) & "*.*" & Chr(0)
sSaveFileName = GetDlgRtnFileName(2, vvOpenFile,ThisDrawing.hwnd, ssFilter, "保存文件", "*.*" & Chr(0), sFileName)

如果是64位系统, ThisDrawing.hwnd要变成ThisDrawing.hwnd32

yuanziyou 发表于 2013-12-17 23:25:18

wwswwswws 发表于 2013-12-17 09:37 static/image/common/back.gif
这两个函数的参数就是这个结构,在用的时候使用者要定义一个变量,所以vOpenFile是定义的一个这个OPENFILEN ...

感谢指点,我自己又揉和了一个多选模式打开的!
页: [1] 2
查看完整版本: 如何调用API函数打开文件保存对话框和文件打开对话框,返回文件路径???