thebiglebowski11
thebiglebowski11

Reputation: 1461

Opening Files with Filepath Excel-VBA

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:

Workbooks.Open myDestFilePath 

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

Upvotes: 0

Views: 15107

Answers (3)

SWa
SWa

Reputation: 4363

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

ThisWorkbook.FollowHyperlink "FilePath"

Won't open

For example:

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

Upvotes: 2

Tomamais
Tomamais

Reputation: 95

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:

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. :(

Upvotes: 1

hexboy
hexboy

Reputation: 79

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:

'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.

Upvotes: 0

Related Questions