Cannot use shell command if there are spaces in path

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
Am trying to launch Notepad++ and have it open a specific file. I see elsewhere that spaces in the path to the file to open causes grief. Yep. I can confirm it.

Tried adding double double quotes, replacing spaces with %20 but have not found the right combo. Here is version seven of the code, trying combinations of content.

Can get program to open but it keeps trying to open a file whose name is PART of a folder name in the path that has spaces. Example ...\Data Tools Dev\myfile.xml
might look like ...\Data Tools Dev\Data

VBA Code:
Function FileExists(psFileSpec As String) As Boolean

    On Error Resume Next
    If Dir(psFileSpec) <> "" Then FileExists = True

End Function

'
 Sub OpenFileWithNotepadPP()
 
    Dim sPath As String
    Dim sFileName As String
    
    sPath = "C:\Users\Jim\Documents\Primary\Excel Tools and Development\Database Related\"
    sFileName = "PrimaryData.XML"
    
    If Not FileExists(sPath & sFileName) _
     Then
        MsgBox "That file does not exist."
        Exit Sub
    End If
    
Debug.Print sPath & sFileName

    Call Shell(Chr(34) & "C:\Program Files (x86)\Notepad++\notepad++.exe " & Chr(34) & sPath & sFileName & Chr(34) & Chr(34))
 
 End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this
VBA Code:
Function FileExists(psFileSpec As String) As Boolean

    On Error Resume Next
    If Dir(psFileSpec) <> "" Then FileExists = True

End Function

'
 Sub OpenFileWithNotepadPP()
 
    Dim sPath As String
    Dim sFileName As String
    
    sPath = "C:\Users\Jim\Documents\Primary\Excel Tools and Development\Database Related\"
    sFileName = "PrimaryData.XML"
    
    If Not FileExists(sPath & sFileName) _
     Then
        MsgBox "That file does not exist."
        Exit Sub
    End If
    
    Debug.Print sPath & sFileName
    Debug.Print """C:\Program Files (x86)\Notepad++\notepad++.exe"" """ & sPath & sFileName & """"

    Call Shell("""C:\Program Files (x86)\Notepad++\notepad++.exe"" """ & sPath & sFileName & """")
 
 End Sub
 
Upvote 0
Fantastic assistance. Thanx. I could try to understand or I could be lazy and take what you gave me and move on. For now, the latter for sure.

But, bonus question...can I get that other app to take or get focus so it comes to the top of the of windows open in Windows 10?
 
Upvote 0
Fantastic assistance. Thanx. I could try to understand or I could be lazy and take what you gave me and move on. For now, the latter for sure.

But, bonus question...can I get that other app to take or get focus so it comes to the top of the of windows open in Windows 10?
This should do what you want
VBA Code:
Function FileExists(psFileSpec As String) As Boolean

    On Error Resume Next
    If Dir(psFileSpec) <> "" Then FileExists = True

End Function

'
Sub OpenFileWithNotepadPP()

    Dim sPath As String
    Dim sFileName As String

    sPath = "C:\Users\Jim\Documents\Primary\Excel Tools and Development\Database Related\"
    sFileName = "PrimaryData.XML"

    If Not FileExists(sPath & sFileName) _
     Then
        MsgBox "That file does not exist."
        Exit Sub
    End If

    Debug.Print sPath & sFileName
    Debug.Print """C:\Program Files (x86)\Notepad++\notepad++.exe"" """ & sPath & sFileName & """"

   Shell """C:\Program Files (x86)\Notepad++\notepad++.exe"" """ & sPath & sFileName & """", vbNormalFocus

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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