VB中的getopenfilename函数(API)怎么用地?谢了

最好举个例子,说得通欲易懂些,
谢了
不要到网上找一大堆的资料来充噢!

第1个回答  2005-11-24
下面是GetOpenFileName的例子:
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 Command1_Click()
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = Form1.hWnd
OpenFile.hInstance = App.hInstance
sFilter = "Batch Files (*.bat)" & Chr(0) & "*.BAT" & 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)
If lReturn = 0 Then
MsgBox "The User pressed the Cancel Button"
Else
MsgBox "The user Chose " & Trim(OpenFile.lpstrFile)
End If
End Sub

选择颜色的函数是ChooseColor,选择字体的函数是ChooseFont。几个函数的用法类似,都是调用一个结构,结构的定义在Win32api.txt中可以找到,详细的说明在Win API的帮助中有。本回答被提问者采纳

VB中的getopenfilename函数(API)怎么用地?谢了
Private Sub Command1_Click()Dim OpenFile As OPENFILENAME Dim lReturn As Long Dim sFilter As String OpenFile.lStructSize = Len(OpenFile)OpenFile.hwndOwner = Form1.hWnd OpenFile.hInstance = App.hInstance sFilter = "Batch Files (*.bat)" & Chr(0) & "*.BAT" & Chr(0)Open...

VBA 这个Application.GetOpenFilename 的用法 举个例子 谢谢让我知道...
Application.GetOpenFilename(文件类型筛选规则(就是说明)优先显示第几个类型的文件标题是否允许选择多个文件名)打开类型只限excel文件 设置打开某类文件可以用下面的规则 文件类型说明文字,文件类型后辍Sub t1()Dim f 打开WPS文字,进入excel界面 点击上方工具栏中的开发工具,VB编辑器 进入Microsoft Visual...

VBA 中如何用GetOpenFilename获取到文件名
代码改一下 Private Sub CommandButton1_Click() Dim strfile As String strfile = Application.GetOpenFilename("所有文件(*.*),*.*") If strfile = "False" Then'strfile定义的是String类型,无法与boolean类型的False比较 MsgBox ("未选择任何文档") Exit Sub Else 'If ...

Excel VBA 进阶—— 打开文件方法:GetOpenFilename方法
GetOpenFilename方法是Excel VBA中的关键工具,它能显示"打开"对话框,让使用者选取文件进行后续操作。值得注意的是,此方法不会自动打开文件。使用语法:GetOpenFilename方法包含四个参数,确保使用者能够灵活操作。实际应用:GetOpenFilename方法常用于选择多个文件,并通过open方法执行文件操作,如多文件合并...

vba中getopenfilename能打开文件吗
vba中getopenfilename调出选择文件对话框,选择文件之后,函数返回文件路径名字符串,取消返回false.本身不打开文件,要使用workbooks.open方法打开文件

vba 中的 GetOpenFilename 在VB6.0中要怎么实现
如下代码亲测OK:Dim fileDlg As Object ' 文件对话框 ' 变量初始化 Set fileDlg = CreateObject("MSComDlg.CommonDialog") ' 打开附件 fileDlg.DialogTitle = "选择附件文件" fileDlg.Filter = "Excel (*.xls;*.xlsx)|*.xls;*.xlsx|" & _ "Word (*.doc;*.docx)...

VBA在使用用Application.GetOpenFilename时如何设定对话框打开路径?
您好,Application.GetOpenFilename 方法不具备这种功能的!替代方案如下:Sub Demo() With Application.FileDialog(msoFileDialogOpen) .InitialFileName = "D:\\" .Show End WithEnd Sub 如果实在要用 Application.GetOpenFilename 方法,可以在调用之前加两行代码,比如需要在打开前定位到的...

Excel VBA系列之GetOpenFilename方法的多选参数
在Excel VBA系列中,我们讨论了GetOpenFilename方法的参数设置,特别是文件多选的MultiSelect参数。设置MultiSelect参数为TRUE后,若选择多个文件,GetOpenFilename方法会返回一个数组对象。定义返回值为数组,并将结果赋给数组变量,如arr。通过将arr返回到工作表的单元格,可直观验证其为数组。代码Range("A1:...

excel vba Application.GetOpenFilename 如何为开启的视窗指定路径为当...
Sub test()Dim FileName As String Dim wb As Workbook FileName = Application.GetSaveAsFilename(ThisWorkbook.Path)If FileName = "False" Then Exit Sub Set wb = Workbooks.Add wb.SaveAs FileName End Sub

怎么用GetOpenFileName获取文件名字啊
执行GetOpenFileName成功后,用函数PathStripPath可以得到文件名。OPENFILENAME ofn;char strFile[MAX_PATH];memset(&ofn,0,sizeof(OPENFILENAME));memset(strFile,0,sizeof(char)*MAX_PATH);ofn.lStructSize=sizeof(OPENFILENAME);ofn.lpstrFilter="网页(*.html)\\0*.html*;*.htm*\\0";ofn....

相似回答