Code To find and run an EXE file out side of the Excel

  • Thread starter Thread starter A-Design
  • Start date Start date
A

A-Design

Hi,

I need to run another software ( like an EXE file) from excel ,is it
possible ? because this EXE file might be in different location on the hard
drive is it possible to write the codes the way that can look for this
specific file first (something like search) and then run it when it finds it
?Thanks,

Afshin.
 
To run it, just use the Shell command:

Shell("c:\winnt\notepad.exe")

To find it, you will have to have some clue where it might be. I don'
know of any built in file searching function in VBA. Someone may hav
posted one, so do a search for it (or a google search for VBA fil
search).

If it may be in a couple of places, you could use the DIR command t
see if it is there:

s = Dir("c:\windows\notepad.exe")
if s <> "" then Shell("c:\windows\notepad.exe")

s = Dir("c:\winnt\notepad.exe")
if s <> "" then Shell("c:\windows\notepad.exe")

etc.
 
I don't
know of any built in file searching function in VBA.

Use the filesearch object.

sStr1 = "excel.exe"
set fs = Application.FileSearch
With fs
.NewSearch
.FileName = sStr1
.LookIn = "C:\"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
End With
fs.Execute

sStr2 = ""
For r = 1 To fs.FoundFiles.Count
if instr(len(fs.foundfiles(r))-(len(sStr1)-1),fs.FoundFiles(r), _
sStr1,vbTextCompare) then
sStr2 = fs.FoundFiles(r)
fr = r
exist for
end if
Next r
if sStr2 <> "" then
msgbox fs.FoundFiles(fr) & " was found"
End If

It only searches one path and its subdirectories, so if you can narrow it
down to which drive it should work, or you can loop over all drives.
 
Thank Tom,

I am not sure about this but I think using the "Dir" command in "DOS" mode
plus " /S " switch and finally process the results would help, don't you
think so? Thanks,

Regards,
Afshin.


Tom Ogilvy said:
I don't
know of any built in file searching function in VBA.

Use the filesearch object.

sStr1 = "excel.exe"
set fs = Application.FileSearch
With fs
.NewSearch
.FileName = sStr1
.LookIn = "C:\"
.SearchSubFolders = True
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
End With
fs.Execute

sStr2 = ""
For r = 1 To fs.FoundFiles.Count
if instr(len(fs.foundfiles(r))-(len(sStr1)-1),fs.FoundFiles(r), _
sStr1,vbTextCompare) then
sStr2 = fs.FoundFiles(r)
fr = r
exist for
end if
Next r
if sStr2 <> "" then
msgbox fs.FoundFiles(fr) & " was found"
End If

It only searches one path and its subdirectories, so if you can narrow it
down to which drive it should work, or you can loop over all drives.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top