+ Reply to Thread
Results 1 to 4 of 4

How to enter %PC1driveD% into .vbs? System environment variable PC1DriveD defined as d:

Hybrid View

  1. #1
    Registered User
    Join Date
    08-30-2014
    Location
    Lake Worth, Florida
    MS-Off Ver
    Office 2010
    Posts
    2

    How to enter %PC1driveD% into .vbs? System environment variable PC1DriveD defined as d:

    PC2 needs to access the files on PC1,
    thus sysvar on PC2 defines PC1DriveD as a networked drive letter.

    Same sharing concept needed if use USB with fname and attach to laptop.

    My .bat code worked flawlessly using %PC1DriveD%\fname.xlsm

    Now, using .vbs, this works:


    Set oxl = CreateObject("excel.application")
                        Set owb = oxl.Workbooks.Open("d:\fname.xlsm")   
         
           THIS DOES NOT WORK:
                    Set owb = oxl.Workbooks.Open("%PC1d%\fname.xlsm")  
    
           FYI, this is accepted, but then the file is NOT FOUND
                    MyPath = "%PC1d%\fname.xlsm"
    
                    Set owb = oxl.Workbooks.Open("MyPath")
    I've actually spent weeks unsuccessfully trying to resolve this puzzle

    THANKS!
    Last edited by Sometimes; 08-31-2014 at 10:48 AM. Reason: Code entry guidelines ... fixed properly

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How to enter %PC1driveD% into .vbs? System environment variable PC1DriveD defined as

    Hi Sometimes,

    The following may help you resolve your problem:
    Sub GetEnvironmentVariableValue()
      Dim sUserName As String
      sUserName = Environ("UserName")
      MsgBox "Environ(""UserName"")=" & sUserName
    End Sub
    Lewis

  3. #3
    Registered User
    Join Date
    08-30-2014
    Location
    Lake Worth, Florida
    MS-Off Ver
    Office 2010
    Posts
    2

    Re: How to enter %PC1driveD% into .vbs? System environment variable PC1DriveD defined as

    Lewis, thank you so much for the quick response.

    I had tried various drafts of GetEnvironmentVariableValue,
    and I learned about all kinds of possible error combinations
    on my part!

    I must be daft, but here's what I entered, and I got
    "Expected end of statement" for " Dim sDriveLetter As String"


    Exact code entered is
    ~~~~~~~~~~~~~~~~~~~~~

    ' Excel to open in own new window with variable drive letter from SysVar: PC1d is defined with value d:
    
    On Error Resume Next
    Set oxl = GetObject(, "excel.application")
    Set oxl = CreateObject("excel.application")
    
    If oxl Is Nothing Then
    MsgBox "Cannot start Excel"
    
    Else
       Set PRSNL = oxl.Workbooks.Open("C:\Program Files (x86)\Microsoft Office\Office14\XLSTART\Personal.xlsb")
    
    Sub GetEnvironmentVariableValue()
      Dim sDriveLetter As String
    '                    >>>>>>>> Err Msg for above line says: Expected end of statement
      sDriveLetter  = Environ("PC1d")
    '               MsgBox "Environ(""UserName"")=" & sUserName
    End Sub
    
    oxl.Visible = True
    Set owb = oxl.Workbooks.Open("sDriveLetter \subdir\FileName.xlsm")
    
    If owb Is Nothing Then
    MsgBox "Cannot open file"
    Else
    owb.Visible = True
    oxl.Windows(1).WindowState = -4137 'xlMaximized
    End If
    
    End If
    
    ' end vbs script
    Last edited by Sometimes; 08-31-2014 at 10:50 AM. Reason: Corectly following CODE instructions! Sorry!

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: How to enter %PC1driveD% into .vbs? System environment variable PC1DriveD defined as

    I have to apologize, because I gave the right answer to the wrong question. I thought you wanted to read the environment variable in Excel VBA.

    Your question seems to be a VBS question. The following thread may help you read Environment variables in VBS: http://ss64.com/vb/env.html

    The Forum Rules require you to add CODE TAGS when you post code, and require no answers until the CODE TAGS are in place. See http://www.excelforum.com/forum-rule...rum-rules.html

    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. open textfile from path defined by environment variable
    By proepert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-14-2010, 11:25 PM
  2. Access system-environment variables
    By Perry in forum Excel General
    Replies: 3
    Last Post: 03-31-2006, 07:25 AM
  3. Can you set a Window's Environment variable in VB
    By Ken Soenen in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2006, 02:25 PM
  4. Environment Variable
    By Andibevan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-03-2005, 11:05 AM
  5. [SOLVED] Using an Array in Environment Variable
    By Jack Gillis in forum Excel General
    Replies: 6
    Last Post: 06-14-2005, 03:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1