使用 Filepath Excel-VBA 打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9610122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:28:42  来源:igfitidea点击:

Opening Files with Filepath Excel-VBA

excelvbafilepath

提问by thebiglebowski11

Is it possible to open all types of files from excel vba with just the file path? Right now I can open workbooks with the simple:

是否可以仅使用文件路径从 excel vba 中打开所有类型的文件?现在我可以用简单的方法打开工作簿:

Workbooks.Open myDestFilePath 

but what if myDestFilePath is really a .pptx file or whatever else?

但是如果 myDestFilePath 真的是一个 .pptx 文件或其他任何文件呢?

采纳答案by hexboy

You can absolutely use Excel VBA to automate other apps, but you need to use the correct application object for the task. If you're trying to open PowerPoint files, you will need to use PowerPoint to do it. Here's a crude example:

您绝对可以使用 Excel VBA 来自动化其他应用程序,但您需要为该任务使用正确的应用程序对象。如果您尝试打开 PowerPoint 文件,则需要使用 PowerPoint 来执行此操作。这是一个粗略的例子:

'remember to add the powerpoint object library (Tools->References)
Sub OpenPPTFile()
    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation

    Set pptApp = New PowerPoint.Application

    Set pptPres = pptApp.Presentations.Open("filename.pptx")

    'here you can add code to have powerpoint do all kinds of nifty things to your file

    pptPres.Close

    pptApp.Quit

    Set pptPres = Nothing
    Set pptApp = Nothing
End Sub

So if your question is "Can I use Excel to open files created by other apps", the short answer is no. However, if the question is "Can I use Excel VBA to automate other applications to perform operations on non-Excel files?" the answer is yes.

因此,如果您的问题是“我可以使用 Excel 打开其他应用程序创建的文件吗”,那么简短的回答是否定的。但是,如果问题是“我可以使用 Excel VBA 自动化其他应用程序对非 Excel 文件执行操作吗?” 答案是肯定的。

回答by SWa

To use internal calls, without an API. I haven't found much that

使用内部调用,无需 API。我没有发现太多

ThisWorkbook.FollowHyperlink "FilePath"

Won't open

打不开

For example:

例如:

ThisWorkbook.FollowHyperlink  "D:\My documents\Movie.mov"

回答by Tomamais

You can use the ShellExecute VB function. Just declare de API header and you can "call" a file directly to open it in associated application. Here is an example code:

您可以使用 ShellExecute VB 函数。只需声明 de API 标头,您就可以直接“调用”文件以在关联的应用程序中打开它。这是一个示例代码:

http://www.tomasvasquez.com.br/blog/microsoft-office/usando-a-funcao-shellexecute-no-vba

http://www.tomasvasquez.com.br/blog/microsoft-office/usando-a-funcao-shellexecute-no-vba

Sorry about the link, but I am writing this answer from my iPad, and its terrible to format code here. :(

抱歉链接,但我是从我的 iPad 上写这个答案的,在这里格式化代码很糟糕。:(