ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > VSTO > VSTO中自定义函数获取打开对话框所选文件路径

VSTO中自定义函数获取打开对话框所选文件路径

作者:绿色风 分类: 时间:2022-08-18 浏览:92
楼主
DJ_Soo
代码:
  1. '获取文件路径,如果取消,返回null
  2.     Public Function openFileDg(ByVal Title As String, _
  3.                 Optional ByVal InitialFileName As String = "C:\")
  4.         Dim myStream As Object = Nothing
  5.         Dim openFileDialog1 As New OpenFileDialog()

  6.         With openFileDialog1
  7.             .InitialDirectory = InitialFileName '设置初始路径
  8.             .Filter = "excel files|*.xl*"       '后缀为xl*的默认为excel files
  9.             .FilterIndex = 1                    '指向第一个
  10.             .RestoreDirectory = True            '运行后'重置'初始路径
  11.             .Title = Title                      'Dialog标题
  12.         End With
  13.         If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
  14.             Try
  15.                 myStream = openFileDialog1.OpenFile()
  16.                 If (myStream IsNot Nothing) Then
  17.                     ' Insert code to read the stream here.
  18.                     openFileDg = myStream.name
  19.                 End If
  20.             Catch Ex As Exception
  21.                 MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
  22.             Finally
  23.                 ' Check this again, since we need to make sure we didn't throw an exception on open.
  24.                 If (myStream IsNot Nothing) Then
  25.                     myStream.Close()
  26.                 End If
  27.             End Try
  28.         End If
  29.     End Function
调用(sheet*.vb中调用):
  1. Sub open_xlsFile()
  2.         Dim Wb As Excel.Workbook
  3.         Dim openPath As String
  4.         openPath = openFileDg("Openfile")
  5.         If openPath <> Nothing Then
  6.             Wb = Me.Application.Workbooks.Open(openPath)
  7.         End If
  8.     End Sub
2楼
wqfzqgk
题不对路
3楼
wqfzqgk
  Application.GetOpenFilename也可以啊
4楼
DJ_Soo
为何题不对路?
你的方法在自建module之后是不能使用的貌似.
5楼
wqfzqgk
可以的,这就是一开始用VSTO时的疑惑
6楼
DJ_Soo
有在模块中调用的方法吗?
7楼
wqfzqgk
全局性globals
8楼
wqfzqgk
其实VSTO是很简单的,和VBA是一样的,只不过有时会大脑反转一下就可以啦,不过部署也有点麻烦的,况且代码不经过处理就会和源码一样,其实什么都是一样的,原理相通,呵呵

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap