Excel VBA 如何使用默认应用程序打开文件 [英] How can Excel VBA open file using default application

查看:29
本文介绍了Excel VBA 如何使用默认应用程序打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 Excel 电子表格,它在 A 列中有一个文件路径和名称.当运行宏时,假设 A1 中指定的文件应该在用户的机器上打开.该文件可能是 .doc、.xls、.txt 等......而不是我的 vba 需要知道应用程序的完整路径,我怎么能让 vba 告诉机器请打开这个文件并使用你的应用程序与扩展相关联"?

I want an Excel spreadsheet that has a file path and name in column A. When a macro is run, let's say the file specified in A1 should be opened on the user's machine. The file could be .doc, .xls, .txt, etc.... rather than my vba needing to know the full path the to application, how could I have the vba tell the machine "please open this file and use your application associated with the extension" ?

我已经发现这适用于完整路径:

I have already found this to work with the full path:

dblShellReturned = Shell("C:WindowsSystem32
otepad.exe myfile.txt, vbNormalFocus)

我怎样才能让它与类似的东西一起工作:

how could I get it to work with something like:

dblShellReturned = Shell("myfile.txt", vbNormalFocus) ' how do I get this to work

先谢谢你!

推荐答案

这对我在 Excel &字

This works for me in Excel & Word

Sub runit()
   Dim Shex As Object
   Set Shex = CreateObject("Shell.Application")
   tgtfile = "C:Naxdud.txt"
   Shex.Open (tgtfile)
End Sub

或...根据 Expenzor 在下面的评论

or ... as per Expenzor's comment below

CreateObject("Shell.Application").Open("C:Naxdud.txt")

这篇关于Excel VBA 如何使用默认应用程序打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆