Shell Command

Private Sub Form_Activate()
Dim openexcel
        openexcel = Shell("c:\Program Files\Microsoft Office\Office\excel c:\my documents\the worksheet.XLS", 1)
End Sub

This command will not work because long filenames and spaces between the folder "my documents" and "The worksheet.xls"  I know that if I type in the dos name "Mydocu~1" it will work, but I need to have this set to a variable, instead of hard code.  Is there a function that will convert these so the computer will know how to open?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

a111a111a111Connect With a Mentor Commented:
Here is a working code to get a short file name from a long file name.

The program and code are working fine.

If you need the exe I can email to you.

============  start here =============
' ***  in module1 ***
Option Explicit

Declare Function GetShortPathName Lib "kernel32" _
      Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _
      ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

   Public Function GetShortName(ByVal sLongFileName As String) As String
       Dim lRetVal As Long, sShortPathName As String, iLen As Integer
       'Set up buffer area for API function call return
       sShortPathName = Space(255)
       iLen = Len(sShortPathName)

       'Call the function
       lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen)
       'Strip away unwanted characters.
       GetShortName = Left(sShortPathName, lRetVal)
   End Function


' THe code in form1 ********

Option Explicit

Private Sub Command1_Click()
Dim msg As String
     CommonDialog1.filename = "*.*"
     msg = "Long File Name: " & CommonDialog1.filename & vbCrLf
     msg = msg & "Short File Name: " & GetShortName(CommonDialog1.filename)
     msg = msg & vbCrLf & "________________________________________________________"
     msg = msg & vbCrLf & vbCrLf & "   E-Mail:"
     MsgBox msg
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Form_Load()

End Sub

Try phrasing it as:
quote = chr(34)
path = "c:\" & quote & "Program Files\Microsoft Office\Office\excel" & quote
file = "c:\" & quote & "my documents\the worksheet.XLS" & quote
openexcel = Shell(path & " " & file, 1)

In this way the quotes will be passed out to the SHELL command.

thienAuthor Commented:
This does not work.  Tells me file is not found.  

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Hi David,
I dont think there is a problem with the command u have given because it works fine. Could u specify the exact error that it gives at u'r place.
Check out that excel works fine and also that the xls is present. I tried with other exe's is working fine. check what ver of excel u r using and if it cannot accept filename as spaced in between.

thienAuthor Commented:

Again it does not work,error 53, File not found.  If I use the command the regular way,

openexcel = Shell("c:\Program Files\Microsoft Office\Office\excel c:\worksheet.XLS", 1)

The example above works, but if I change the path to C:\my documents, or to a worksheet that has a space in the name, then I get errors.  Your example does not work. Even though I know the file exists, it tells me it does not.

Also you can download the exe from
You can modify the code as you wish.

thienAuthor Commented:
Thank you This is Exactly What I need!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.