Code to open a program by name

Excelpromax123

Board Regular
Joined
Sep 2, 2021
Messages
167
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone, I'm from Vietnam, so I use Google Translate, so the words may be difficult to understand, hope everyone understands. I need a code to open a program name (because the path often changes), so I can't get the path but have to get the program name

Currently I am using this code but this code is only Off, if you want to change it to On, then edit it somewhere. Sincerely thank!

Code:
Function TaskKill(sTaskName)
    TaskKill = CreateObject("WScript.Shell").Run("taskkill /f /im " & sTaskName, 0, True)
End Function

Sub turnoffthe program()
On Error Resume Next
TaskKill ("Notepad.exe")
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You indicated the path can change so here is some code that will find the path for you of the program name.extention that you provide it:

VBA Code:
Sub GetPathToFileName()
'
'   This macro should be able to find the location of any file on your C drive if you provide the name and file extention to look for.
'   It will display the CMD black box while it is searching, May take a minute or two to find the location.
'
    Dim FileName        As String
    Dim FilePath        As String
    Dim retVal          As String
'
    FileName = "Notepad.exe"                                        ' <--- Set this to the file name and extention that you are looking for
'
    retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & FileName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(2)
'
    FilePath = Left$(retVal, InStrRev(retVal, "\"))
Debug.Print FilePath
MsgBox FilePath
End Sub

The code will find the path to any filename.ext on the C Drive.
 
Upvote 0
You indicated the path can change so here is some code that will find the path for you of the program name.extention that you provide it:

VBA Code:
Sub GetPathToFileName()
'
'   This macro should be able to find the location of any file on your C drive if you provide the name and file extention to look for.
'   It will display the CMD black box while it is searching, May take a minute or two to find the location.
'
    Dim FileName        As String
    Dim FilePath        As String
    Dim retVal          As String
'
    FileName = "Notepad.exe"                                        ' <--- Set this to the file name and extention that you are looking for
'
    retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & FileName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(2)
'
    FilePath = Left$(retVal, InStrRev(retVal, "\"))
Debug.Print FilePath
MsgBox FilePath
End Sub

The code will find the path to any filename.ext on the C Drive.

thank you. I tried the above code to open the Paint program. But I can't run it, but it gives me an error

1630635472582.png

VBA Code:
Sub GetPathToFileName()
'
'   This macro should be able to find the location of any file on your C drive if you provide the name and file extention to look for.
'   It will display the CMD black box while it is searching, May take a minute or two to find the location.
'
    Dim FileName        As String
    Dim FilePath        As String
    Dim retVal          As String
'
    FileName = "mspaint.exe"                                        ' <--- Set this to the file name and extention that you are looking for
    retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & FileName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(2)
    FilePath = Left$(retVal, InStrRev(retVal, "\"))
Debug.Print FilePath
MsgBox FilePath
End Sub
 
Upvote 0
@Excelpromax123 I just ran the following code and it found the path to mspaint and opened it:

VBA Code:
Sub GetPathToFileNameAndExecute()
'
'   This macro should be able to find the location of any file on your C drive if you provide the name and file extention to look for.
'   It will display the CMD black box while it is searching, May take a minute or two to find the location.
'
    Dim FileName        As String
    Dim FilePath        As String
    Dim retVal          As String
    Dim ExecuteProgram  As Variant
'
    FileName = "mspaint.exe"                                        ' <--- Set this to the file name and extention that you are looking for
'
    retVal = Split(CreateObject("WScript.Shell").Exec("CMD /C FOR /r ""C:\"" %i IN (*" & FileName & ") DO (ECHO %i)").StdOut.ReadAll, vbCrLf)(2)
'
    FilePath = Left$(retVal, InStrRev(retVal, "\"))
'
    ExecuteProgram = Shell(FilePath & FileName, vbNormalFocus)
End Sub
 
Upvote 0
You need to open a program from excel or just open that program from window. If it is the second, I recommend you try Autohotkey.
 
Upvote 0
what is the extention(s) associated with the files that are opened with your program ?
I mean like .txt for notepad, .xlsm for excel and so on.
 
Upvote 0
If your question was addressed to me, the code will find path to all files, I assume, but it would only execute executable files, I also assume. So No, it would not open a txt file or xlsm file.

That might work with some additional code.
 
Last edited:
Upvote 0
@Jaafar Tribak my excel is 32 bit and it takes about 20-30 seconds to search and find on my computer. I guess the length of time would depend on how many files it has to look through.
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top