+ Reply to Thread
Results 1 to 11 of 11

Please help - VBA isn't reading my Environment variable

  1. #1
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Please help - VBA isn't reading my Environment variable

    Hi, sorry if this is a simple fix. I have created a user environment variable and named it OLIMARO.
    This environment variable is supposed to point to the following folder: %userprofile%\documents\olimaro-2.3\
    in the file path referenced above, I want to run a batch file named "run_olimaro.bat" via vba.

    I have created this macro but it doesn't seem to be working
    Sub RunOlimaro()
    Dim folderPath As String
    folderPath = Environ("OLIMARO")
    Shell folderPath & "\run_olimaro"
    End Sub


    However placing the full path without specifying the environment variable seems to run.

    Shell c:\Users\Chinoxl\documents\olimaro-2.3\

    but I dont want to run the full path as I need it to run on a different computer where the folder path might be different.
    What am I doing wrong? Please help.


    EDIT: The solution was: Excel needs to be restarted each time the path in an existing environment variable is changed before the new path can be considered in the code. Using MsgBox in reading the value from the Environment variable path was helpful in recognizing what path was referenced in VBA.
    Attached Files Attached Files
    Last edited by Chinoxl; 09-17-2020 at 04:50 AM.

  2. #2
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Please help - VBA isn't reading my Environment variable

    what are you attempting to accomplish with this?
    Please Login or Register  to view this content.
    that function can return literally anything when you put almost anything in the argument.


    https://docs.microsoft.com/en-us/off...viron-function

  3. #3
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Re: Please help - VBA isn't reading my Environment variable

    Hi, thanks, I am new to vba so I am not sure about everything yet but I know I just want to use vba to run a file based on the environment variable assigned to its file's path.

    The file program I want to run is stored on my computer and in several others but each has a different folder path location per computer. As I wouldn't know the folder path/location of each, I have tried to point vba to the file-to-run using the environment variable but it just wouldn't.

  4. #4
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Please help - VBA isn't reading my Environment variable

    maybe this would help as an explanation of how pointers in windows can/should be used?

    https://superuser.com/questions/2843...et-or-use-them

    and if the program you want to run is in different locales on different peoples' computers at your workplace, perhaps you should not be using windows pointers or deep level resources to do this? the environ variable in windows, per the article I just gave you, can do many different things. and to your point of using %userprofile% as part of a dir path, of course you can use that whenever you want. by default, just referencing the profile like that points directly to the directory of the name that has been assigned to the owner of the workstation.

    for instance, if I want to use that on my local machine to run any give program, I could write this:
    Please Login or Register  to view this content.
    to open a text file.

    does any of this help you gain the knowledge you need to do this?

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,003

    Re: Please help - VBA isn't reading my Environment variable

    If you add a MsgBox FolderPath statement, does it show the correct value? Also, you didn't actually say what happens when you run your macro? I'm guessing an error message, but if so, what?
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Re: Please help - VBA isn't reading my Environment variable

    Hi vba_php

    Thanks for your help. I still cant get it to run when I break them like this..
    Shell folderpath & "\run_olimaro"

    I have tried to put the entire file path in a new environment variable and this works for some awkward reason, but when I break them using the above, it doesn't seem to work.

  7. #7
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Please help - VBA isn't reading my Environment variable

    Quote Originally Posted by Chinoxl View Post
    I have tried to put the entire file path in a new environment variable and this works for some awkward reason, but when I break them using the above, it doesn't seem to work.
    I'm not sure you followed everything I said. but...you know what the SHELL command is used for, right? it is meant to run actual programs:

    https://docs.microsoft.com/en-us/off...shell-function

    so, this:
    Please Login or Register  to view this content.
    does not point to a program. obviously that would not work. I hope we're on the same page here in terms of what I understand you're doing and what you *want* to do? also, did you look at Rorya's post?
    Last edited by vba_php; 09-16-2020 at 12:10 PM.

  8. #8
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Re: Please help - VBA isn't reading my Environment variable

    Hi rorya,

    Thanks for your suggestion, while battling with this yesterday, I tried to use Run instead of Shell (in the vba code I entered in my previous reply to @vba_php). I did this so the error message which shows "cant recognise macro" and then lists the path in the variable in the error message (my newbie attempt to replicate MsgBox ).
    As you would expect, I discovered my vba was reading the wrong file path for the OLIMARO environment variable (I still dont know why). So I created a new environment variable with a different name, but pointing to the path I wanted and tried to check it with the run error message and it looked fine then I changed it to Shell. It ran fine, but then the problem is, It would'nt work unless I put the file path in the environment variable. If I split them into two so that I get the folder path and add the file to it in vba like this: Shell folderpath & "\run_olimaro" it doesn't run.

    So why don't I want to use the full file path? The reason is because, I need to provide a form for the other users to specify the folder path on their computers, there are several files in the folder, if they have to specify the actual file to run (and there are several files at different path locations I have to create this for), it could work but then it would be a hassle for them to begin selecting each file and filling the file paths in the form. In essence, what I need is a way to launch the run_olimaro.bat file from the click of a button in vba (and several others like it).

    The files were not installed, they were extracted from a zip folder which they have received via email. Therefore, getting the file path where they have chosen to extract the files to on their own computers seems impossible to me without some way for them to tell me the path (so I can write the vba to launch the file from each path separately). That's why I am using environment variables.

    If there is some other way to do it without environment variables, that would make my day. Perhaps if there was a code for them to select the file in a dialog box and then this appears as a full file path somewhere, I can use this to create the environment variable for them.


    EDIT: To answer your question the error code when I use shell in the vba is:

    Run-time error '5'
    invalid procedure call or argument
    Last edited by Chinoxl; 09-16-2020 at 12:37 PM.

  9. #9
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Re: Please help - VBA isn't reading my Environment variable

    Hi vba_php

    Yes I think you know exactly what I want to do.

    Please can you help me with the command for running *.bat files? There is an *.exe file in the folder as well as the *.bat. I tried running the *.exe using the
    Shell folderpath & "\run_olimaro.exe" and it still didnt work

    Shell folderpath alone works if I set the entire path including the *.bat file itself as the OLIMARO environment variable.

  10. #10
    Registered User
    Join Date
    05-14-2020
    Location
    England
    MS-Off Ver
    2010
    Posts
    13

    Re: Please help - VBA isn't reading my Environment variable

    Oh shoot I just tried again. It works, it works thanks alot both of you.

    I tried using the MsgBox this time, I realised the problem was excel kept reading the previous path that I set and doesnt realise I had changed it while excel was still running. So it seems I have to shut down excel each time I change the environment variable path and start it again. That seemed to fix it.

    Thanks a lot. Now I just have to figure out a way to get their folder paths or simply ask them to register their folder paths as the OLIMARO environment variable and that would do just fine. THANKS AGAIN. @rorya, @vba_php you guys are AWESOME!!

  11. #11
    Banned User!
    Join Date
    02-06-2020
    Location
    Iowa City, IA, USA
    MS-Off Ver
    2016 - 365 / 2007
    Posts
    2,014

    Re: Please help - VBA isn't reading my Environment variable

    i am unsubscribing. good luck

+ 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. [SOLVED] Finding temp folder. Environment variable?
    By Don Wiss in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-15-2006, 01:10 PM
  3. [SOLVED] Environment Variable Names
    By Chaplain Doug in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2006, 10:35 AM
  4. 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
  5. Environment Variable
    By Andibevan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-03-2005, 11:05 AM
  6. Using an Array in Environment Variable
    By Jack Gillis in forum Excel General
    Replies: 6
    Last Post: 06-14-2005, 03:05 PM
  7. Changing an Environment Variable.
    By Jack Gillis in forum Excel General
    Replies: 5
    Last Post: 03-08-2005, 04:06 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