Environ use (blackslash vs. forward slash)

RawlinsCross

Board Regular
Joined
Sep 9, 2016
Messages
215
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:

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,092
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:

RawlinsCross

Board Regular
Joined
Sep 9, 2016
Messages
215
Ken, can you elaborate on the need for four quotation marks in a row ("""") - I've never come across that.
 

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,092
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:

RawlinsCross

Board Regular
Joined
Sep 9, 2016
Messages
215
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
 

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,092
That is because there is no operating system command nor application called: C:\Program Files\Windows Media Player\wmplayer.exe sPath
 

RawlinsCross

Board Regular
Joined
Sep 9, 2016
Messages
215
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)
 

Kenneth Hobson

Well-known Member
Joined
Feb 6, 2007
Messages
3,092
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.
 

Forum statistics

Threads
1,082,017
Messages
5,362,697
Members
400,686
Latest member
Aakash

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top