Results 1 to 8 of 8

Thread: Environ use (blackslash vs. forward slash)
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Sep 2016
    Location
    St. John's, Newfoundland, Canada
    Posts
    175
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Environ use (blackslash vs. forward slash)

    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 by RawlinsCross; Sep 21st, 2019 at 03:02 PM.

  2. #2
    Board Regular Kenneth Hobson's Avatar
    Join Date
    Feb 2007
    Location
    Tecumseh, OK
    Posts
    3,052
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    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 by Kenneth Hobson; Sep 21st, 2019 at 06:13 PM.

  3. #3
    Board Regular
    Join Date
    Sep 2016
    Location
    St. John's, Newfoundland, Canada
    Posts
    175
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    Ken, can you elaborate on the need for four quotation marks in a row ("""") - I've never come across that.

  4. #4
    Board Regular Kenneth Hobson's Avatar
    Join Date
    Feb 2007
    Location
    Tecumseh, OK
    Posts
    3,052
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    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 by Kenneth Hobson; Sep 21st, 2019 at 09:07 PM.

  5. #5
    Board Regular
    Join Date
    Sep 2016
    Location
    St. John's, Newfoundland, Canada
    Posts
    175
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    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

  6. #6
    Board Regular Kenneth Hobson's Avatar
    Join Date
    Feb 2007
    Location
    Tecumseh, OK
    Posts
    3,052
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    That is because there is no operating system command nor application called: C:\Program Files\Windows Media Player\wmplayer.exe sPath

  7. #7
    Board Regular
    Join Date
    Sep 2016
    Location
    St. John's, Newfoundland, Canada
    Posts
    175
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    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)

  8. #8
    Board Regular Kenneth Hobson's Avatar
    Join Date
    Feb 2007
    Location
    Tecumseh, OK
    Posts
    3,052
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Environ use (blackslash vs. forward slash)

    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.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •