Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Excel VBA open file using default application

Tags:

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 

Thank you in advance!

like image 997
Michael11B12 Avatar asked Sep 20 '13 16:09

Michael11B12


People also ask

Can VBA open a file?

The Excel VBA implementation can open files and run macros on them. In Excel, you use VBA by inserting the code in the Visual Basic Editor. You can also choose the “Macro” button on the Developer Tab.

What applications does VBA work with?

Visual Basic for Applications runs as an internal programming language in Microsoft Office (MS Office, Office) applications such as Access, Excel, PowerPoint, Publisher, Word, and Visio.


1 Answers

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 

or ... as per Expenzor's comment below

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

like image 191
Tin Bum Avatar answered Oct 07 '22 21:10

Tin Bum