ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with Shell command (https://www.excelbanter.com/excel-programming/379117-problem-shell-command.html)

Andrew Hall NZ

Problem with Shell command
 
I want to be able to open some pdf and htm files from Outlook and from xcel.
I am using

Sub testsh()
Dim target As String
target = "C:/vba/copy7.htm"
Shell "Start " & target
End Sub

This fails with "file not found"

Given that I have successfully opened the file to read the text from excel
and also that it runs fine from cmd.exe, I know the path is right. Is there
some security setting or reference that is missing? This problem occurs
regardless of the file I attempt to target. I am running XP in Classic mode.

TIA

Andrew


Andrew Hall NZ

Problem with Shell command
 

I have partially answered my question, but I would still be interested in
why the simpler Shell command does not work. The following works

Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" (ByVal hWnd As Long, _
ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Sub OpenFile()
Dim file As String
file = "C:\vba\copy7.htm"
Call ShellExecute(0&, vbNullString, file, _
vbNullString, vbNullString, vbNormalFocus)
End Sub


Andrew Hall NZ

Problem with Shell command
 

Sorry about the repeat posts but maybe this is useful for others as well. I
have a new problem. While the second example sub above opens an htm file OK,
it does not seem to work for pdf files, which is what I really want. Any
suggestions on how to get over this. The sub runs fine with the pdf target
but nothing happens.

Andrew

Andrew Hall NZ

Problem with Shell command
 
To end my monologue, the solution given in the second post is now working for
all file types. I am still unclear why Shell itself does not work, perhaps it
is not compatible with XP, but in any case my problem is solved.

Andrew

Chip Pearson

Problem with Shell command
 
If I recall correctly, the "Start" program was only in Windows 9x versions.
I don't think it was ever in NT (including XP) versions. The "file not
found" error is not referring to the file named in Target, but rather the
"Start" program.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Andrew Hall NZ" wrote in message
...
I want to be able to open some pdf and htm files from Outlook and from
xcel.
I am using

Sub testsh()
Dim target As String
target = "C:/vba/copy7.htm"
Shell "Start " & target
End Sub

This fails with "file not found"

Given that I have successfully opened the file to read the text from excel
and also that it runs fine from cmd.exe, I know the path is right. Is
there
some security setting or reference that is missing? This problem occurs
regardless of the file I attempt to target. I am running XP in Classic
mode.

TIA

Andrew




Chip Pearson

Problem with Shell command
 
Andrew,

Here's something I dug up from one of my library files. It uses
FindExecutable to get the exe file that is associated with the extension of
the specified filename. (e.g., it gets Excel.exe for 'xls' extensions,
WinWord.exe for 'doc' extensions, etc). If different programs are specified
for different shell verbs (like OPEN and PRINT), it uses the exe specified
for the OPEN verb.

Public Declare Function FindExecutable Lib "shell32.dll" Alias
"FindExecutableA" ( _
ByVal lpFile As String, _
ByVal lpDirectory As String, _
ByVal lpResult As String) As Long

Const MAX_PATH = 260&

Function ShellProgramFromFile(FName As String) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
' StartProgramFromFile
' This function finds the exe filename associated with the
' extension of FName and Shell's to that program, passing
' it FName.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''
Dim ExeName As String
Dim Res As Long
Dim Pos As Long
If Trim(FName) = vbNullString Then
ShellProgramFromFile = False
Exit Function
End If

If Dir(FName, vbNormal + vbHidden + vbSystem) = vbNullString Then
ShellProgramFromFile = False
Exit Function
End If

ExeName = String$(MAX_PATH, vbNullChar)
Res = FindExecutable(FName, vbNullString, ExeName)
If Res < 32 Then
ShellProgramFromFile = False
Exit Function
End If
Pos = InStr(1, ExeName, vbNullChar)
If Pos Then
ExeName = Left(ExeName, Pos - 1)
End If

Shell Chr(34) & ExeName & Chr(34) & Chr(32) & Chr(34) & FName & Chr(34)
ShellProgramFromFile = True
End Function

Sub Test()
Dim FName As String
Dim Res As Boolean
FName = "C:\FW9.pdf" '<<<< CHANGE
Res = ShellProgramFromFile(FName:=FName)

End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Andrew Hall NZ" wrote in message
...
To end my monologue, the solution given in the second post is now working
for
all file types. I am still unclear why Shell itself does not work, perhaps
it
is not compatible with XP, but in any case my problem is solved.

Andrew




Chip Pearson

Problem with Shell command
 
"Chip Pearson" wrote in message news:Ou7tJr%
If I recall correctly, the "Start" program was only in Windows 9x
versions. I don't think it was ever in NT (including XP) versions.


After doing a bit of research and testing, I find that I was wrong. Start is
available, but it is a cmd command, not an exe file. That's why its
available in a cmd window but not with Shell. Shell can't find a file named
"Start" so you get the "file not found" error on "Start" not your Target
file.

I knew there was something odd about Start on NT systems, I just forgot the
details. Your approach using ShellExecute or using my approach with
FindExecutable should work in any environment.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Chip Pearson" wrote in message
...
If I recall correctly, the "Start" program was only in Windows 9x
versions. I don't think it was ever in NT (including XP) versions. The
"file not found" error is not referring to the file named in Target, but
rather the "Start" program.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Andrew Hall NZ" wrote in message
...
I want to be able to open some pdf and htm files from Outlook and from
xcel.
I am using

Sub testsh()
Dim target As String
target = "C:/vba/copy7.htm"
Shell "Start " & target
End Sub

This fails with "file not found"

Given that I have successfully opened the file to read the text from
excel
and also that it runs fine from cmd.exe, I know the path is right. Is
there
some security setting or reference that is missing? This problem occurs
regardless of the file I attempt to target. I am running XP in Classic
mode.

TIA

Andrew






Andrew Hall NZ

Problem with Shell command
 
Thanks for the replies Chip, very useful

Andrew

Chip Pearson

Problem with Shell command
 
You're most welcome. It took me a few tries to get it right, but I'm glad it
works.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"Andrew Hall NZ" wrote in message
...
Thanks for the replies Chip, very useful

Andrew




PapaDos

Problem with Shell command
 
Shell "cmd /cStart " & target

--
Regards,
Luc.

"Festina Lente"


"Andrew Hall NZ" wrote:

I want to be able to open some pdf and htm files from Outlook and from xcel.
I am using

Sub testsh()
Dim target As String
target = "C:/vba/copy7.htm"
Shell "Start " & target
End Sub

This fails with "file not found"

Given that I have successfully opened the file to read the text from excel
and also that it runs fine from cmd.exe, I know the path is right. Is there
some security setting or reference that is missing? This problem occurs
regardless of the file I attempt to target. I am running XP in Classic mode.

TIA

Andrew


Andrew Hall NZ

Problem with Shell command
 
That's very neat PapaDos but how do I deal with a situation where the file
name (or parent folder name) contains spaces eg:

VB & VBA in a Nutshell (VB6).1998.OReilly.pdf

PapaDos

Problem with Shell command
 
For names with spaces, the start command is a bit tricky, it interprets the
first string within quotes as the new window title !
We need to give it a dummy a (it could be empty) string before the command
we need to execute.

Then we need to put target in quotes, either at the definition:

target = """C:/vba/copy7.htm"""
Shell "cmd /cStart """" " & target

or at the shell call:

Shell "cmd /cStart """" """ & target & """"




Alternatively, on most Windows XP setups, the call command is not necessary
at all:

target = """C:/vba/copy7.htm"""
Shell "cmd /c" & target

or

target = "C:/vba/copy7.htm"
Shell "cmd /c""" & target & """"

--
Regards,
Luc.

"Festina Lente"


"Andrew Hall NZ" wrote:

That's very neat PapaDos but how do I deal with a situation where the file
name (or parent folder name) contains spaces eg:

VB & VBA in a Nutshell (VB6).1998.OReilly.pdf


Andrew Hall NZ

Problem with Shell command
 
That is great Luc, thanks. I now have two solutions both of which work with
spaces in file and folder names, the shorter one being:

target = """C:\vba\test this\VB & VBA in a Nutshell (VB6).1998.OReilly.pdf"""
Shell "cmd /cStart """" " & target

The longer one is given in my second post

Andrew


PapaDos

Problem with Shell command
 
Getting rid of the "Start" altogether is even shorter, see my previous post...

--
Regards,
Luc.

"Festina Lente"


"Andrew Hall NZ" wrote:

That is great Luc, thanks. I now have two solutions both of which work with
spaces in file and folder names, the shorter one being:

target = """C:\vba\test this\VB & VBA in a Nutshell (VB6).1998.OReilly.pdf"""
Shell "cmd /cStart """" " & target

The longer one is given in my second post

Andrew


Jay J

Problem with Shell command
 
I;m having the same problem with an .msi file from vb.net 2005 in a
smart client, error file not found.:

Dim fiInstall As IO.FileInfo = New
IO.FileInfo("Cargo\BriltechCRM_701.msi")

If fiInstall.Exists Then
Shell(fiInstall.FullName.Trim,
AppWinStyle.NormalFocus, True)
End if

File name has spaces, but
"""" & fiInstall.FullName.Trim & """"
did not work...












*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 02:59 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com