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

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

问题描述

我想要一个在列A中具有文件路径和名称的Excel电子表格。当运行宏时,假设应在用户的机器上打开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:\Windows\System32\notepad.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

提前谢谢! / p>

Thank you in advance!

推荐答案

这在Excel& Word中适用于我

This works for me in Excel & Word

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

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

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