[英]Opening Files with Filepath Excel-VBA
是否可以僅使用文件路徑從excel vba打開所有類型的文件? 現在,我可以使用以下簡單方法打開工作簿:
Workbooks.Open myDestFilePath
但是,如果myDestFilePath實際上是.pptx文件或其他內容,該怎么辦?
在不使用API的情況下使用內部調用。 我沒有發現太多
ThisWorkbook.FollowHyperlink "FilePath"
不會開
例如:
ThisWorkbook.FollowHyperlink "D:\My documents\Movie.mov"
您可以使用ShellExecute VB函數。 只需聲明API標頭即可,您可以直接“調用”文件以在關聯的應用程序中打開它。 這是示例代碼:
http://www.tomasvasquez.com.br/blog/microsoft-office/usando-a-funcao-shellexecute-no-vba
對不起該鏈接,但是我正在用我的iPad寫這個答案,在這里很難格式化代碼。 :(
您可以絕對使用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
因此,如果您的問題是“我可以使用Excel打開由其他應用程序創建的文件”,則簡短的回答是“否”。 但是,如果問題是“我可以使用Excel VBA自動化其他應用程序以對非Excel文件執行操作嗎?” 答案是肯定的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.