简体   繁体   English

Excel VBA如何使用默认应用程序打开文件

[英]How can Excel VBA open file using default application

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

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

Thank you in advance! 先感谢您!

This works for me in Excel & Word 这适用于Excel和Word

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

or ... as per Expenzor's comment below 或者......根据Expenzor的评论如下

CreateObject("Shell.Application").Open("C:\\Nax\\dud.txt")

The code below is a template. 下面的代码是一个模板。 However you might want to update the default (working) directory to the location of the file. 但是,您可能希望将默认(工作)目录更新为文件的位置。

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
                   (ByVal hwnd As Long, ByVal lpszOp As String, _
                    ByVal lpszFile As String, ByVal lpszParams As String, _
                    ByVal LpszDir As String, ByVal FsShowCmd As Long) _

Function StartDoc(DocName As String) As Long
      Dim Scr_hDC As Long
      Scr_hDC = GetDesktopWindow()
      StartDoc = ShellExecute(Scr_hDC, "Open", DocName, _
      "", "C:\", SW_SHOWNORMAL)
 End Function

VBA's Shell command wants an exe, so I've been launching the explorer.exe and passing in my file path as an argument. VBA的Shell命令想要一个exe,所以我一直在启动explorer.exe并传入我的文件路径作为参数。 It also seems to work with *.lnk shortcuts and web urls. 它似乎也适用于* .lnk快捷方式和网址。

Shell "explorer.exe C:\myfile.txt"

I can't comment on existing answers (not enough points), so I'm answering to add information. 我不能评论现有的答案(不够点),所以我正在回答添加信息。

Working from Access 2010, I ran into silent failures with the following syntax: 从Access 2010开始,我使用以下语法遇到了静默失败:

Dim URL As String
URL = "http://foo.com/"
CreateObject("Shell.Application").Open URL

I could get it to work if I wrapped URL in parentheses, but that just seems wrong for subroutine (instead of function) call syntax. 如果我在括号中包装URL ,我可以让它工作,但这对于子例程(而不是函数)调用语法来说似乎是错误的。 I tried swallowing the return value, but that failed with function call syntax, unless I doubled up the parentheses. 我尝试吞下返回值,但是函数调用语法失败了,除非我将括号加倍。 I realized that the parentheses weren't just syntactic sugar - they had to be doing something, which lead me to believe they might be facilitating implicit casting. 我意识到括号不仅仅是语法糖 - 它们必须做某事,这让我相信它们可能会促进隐式铸造。

I noticed that Open expects a Variant , not a String . 我注意到Open需要Variant ,而不是String So I tried CVar , which did work. 所以我尝试了CVar ,它确实起作用了。 With that in mind, the follwing is my preferred approach since it minimizes the "why are there extraneous parentheses here?" 考虑到这一点,以下是我的首选方法,因为它最小化了“为什么这里有无关的括号?” questions. 的问题。

Dim URL As String
URL = "http://foo.com/"
CreateObject("Shell.Application").Open CVar(URL)

The lesson is that when making OLE Automation calls, be explicit about having Access VBA cast things appropriately! 经验教训是,在进行OLE自动化调用时,要明确让Access VBA正确地进行投射!

Shell32.Shell COM object aka Shell.Application can be used that wraps the ShellExecute Win32 API function: Shell32.Shell COM对象又名Shell.Application可用于包装ShellExecute Win32 API函数:

  • Add a reference to Microsoft Shell Controls And Automation type library to VBA project via Tools->References... , then 通过Tools->References...添加对Microsoft Shell Controls And Automation类型库的引用到VBA项目,然后

     Dim a As New Shell32.Shell Call a.ShellExecute("desktop.ini") 
  • Alternatively, without any references: 或者,没有任何参考:

     Call CreateObject("Shell.Application").ShellExecute("desktop.ini") 

Interestingly, here (WinXP), when using a typed variable (that provides autocomplete), ShellExecute is missing from the members list (but works nonetheless). 有趣的是,在这里(WinXP),当使用类型变量(提供自动完成)时,成员列表中缺少ShellExecute (但仍然有效)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM