Environ use (blackslash vs. forward slash)

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
So I have a question on the use of Environ function in VBA. If I want to open a file from my desktop, it appears I need to use forward slashes instead of backslashes? Is this correct? Is there a forward slash version on Environ?

Code:
Private Sub PortConvey_Click()

Dim sPath As String

'MTS files are video files from a Sony digital camera

sPath = Environ("USERPROFILE") & "\Desktop\TrainingVideos\ConConvey.MTS"
Debug.Print sPath  'Debug.Print prints out C:\Users\jhalfyard\Desktop\TrainingVideos\ConConvey.MTS


'This does not play the video but throws no error
Shell "C:\Program Files\Windows Media Player\wmplayer.exe sPath"


'This does play the video
Shell "C:\Program Files\Windows Media Player\wmplayer.exe c:/Users/jhalfyard/Desktop/TrainingVideos/ConConvey.MTS"
    
End Sub
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Either or is ok.

If that file type is associated with the application that you want to open with it in Windows:
Code:
Shell "cmd /c " &"""" & _
"c:\Users\jhalfyard\Desktop\TrainingVideos\ConConvey.MTS" &  """", vbNormal
 
Last edited:
Upvote 0
Think of Shell() as if it were the old DOS command shell. It did not allow space characters. So, if a path had it, it would fail. e.g. Dir Program Files. To work, do, Dir "Program Files"

You can try this too. Ctrl+R, cmd, enter key, and then try what I did. See what older than dirt knows?

The reason it did not allow it was that it delimited input by the space character. Some programs or commands allow command line inputs. It has to have a way to parse those inputs. So, if you delimit each with double quotes, you are gold.

Some add the quote delimiters by 2 double quotes in the string. Besides the way that I showed, you can do it like this:
Code:
Dim s as string, q as string
q = """"
s = q & "c:\Users\jhalfyard\Desktop\TrainingVideos\ConConvey.MTS" & q
Shell "cmd /c " &  s, vbNormal

I will leave the double double quote method as an exercise. The advantage to that above is that it is easy to read and a Debug.Print s, makes it easy to see results of string concatenation in Immediate Window after a run.
 
Last edited:
Upvote 0
Okay, played around with it. Wonder if you can comment as to why the final command below does not work?

Code:
Private Sub PortConvey_Click()
Dim sPath As String
Dim sNewPath As Variant

sPath = Environ("USERPROFILE") & "\Desktop\TrainingVideos\ConConvey.MTS"
Debug.Print sPath  'Debug.Print prints out C:\Users\jhalfyard\Desktop\TrainingVideos\ConConvey.MTS

'This works!!
Shell ("C:\Program Files\Windows Media Player\wmplayer.exe C:\Users\jhalfyard\Desktop\TrainingVideos\ConConvey.MTS")

'This also works!!!!
Shell "cmd /c" & "" & sPath & "", vbNormal

'This doesn't work. (but throws no error)
Shell ("C:\Program Files\Windows Media Player\wmplayer.exe sPath")
    
End Sub
 
Upvote 0
That is because there is no operating system command nor application called: C:\Program Files\Windows Media Player\wmplayer.exe sPath
 
Upvote 0
But isn’t it identical to the command given above under the comment ‘This Works!
?
sorry, I don’t see how they are different (although clearly they are)
 
Upvote 0
You passed a hard coded string. The others returned the value of sPath, not the string of sPath itself.

You can put this in a module to compare:
Code:
Sub Test()
    Dim sPath As String
    sPath = Environ("USERPROFILE") & "\Desktop\TrainingVideos\ConConvey.MTS"
    Debug.Print "C:\Program Files\Windows Media Player\wmplayer.exe sPath"
    Debug.Print "C:\Program Files\Windows Media Player\wmplayer.exe " & sPath
End Sub

Put both strings from the Immediate Window after a run into Win+R. Hopefully, you can see why the first would error.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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