作者:绿色风
分类:
时间:2022-08-18
浏览:163
楼主 DJ_Soo |
代码:- '获取文件路径,如果取消,返回null
- Public Function openFileDg(ByVal Title As String, _
- Optional ByVal InitialFileName As String = "C:\")
- Dim myStream As Object = Nothing
- Dim openFileDialog1 As New OpenFileDialog()
- With openFileDialog1
- .InitialDirectory = InitialFileName '设置初始路径
- .Filter = "excel files|*.xl*" '后缀为xl*的默认为excel files
- .FilterIndex = 1 '指向第一个
- .RestoreDirectory = True '运行后'重置'初始路径
- .Title = Title 'Dialog标题
- End With
- If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
- Try
- myStream = openFileDialog1.OpenFile()
- If (myStream IsNot Nothing) Then
- ' Insert code to read the stream here.
- openFileDg = myStream.name
- End If
- Catch Ex As Exception
- MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
- Finally
- ' Check this again, since we need to make sure we didn't throw an exception on open.
- If (myStream IsNot Nothing) Then
- myStream.Close()
- End If
- End Try
- End If
- End Function
调用(sheet*.vb中调用):- Sub open_xlsFile()
- Dim Wb As Excel.Workbook
- Dim openPath As String
- openPath = openFileDg("Openfile")
- If openPath <> Nothing Then
- Wb = Me.Application.Workbooks.Open(openPath)
- End If
- 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总版主之一