Solved

path Variable captured using a batch file needs to also be utilized in Excel VBS to open excel file.

Posted on 2016-07-18
7
60 Views
Last Modified: 2016-07-19
Here is a snipit of the batch file code which captures the directory and file name. I had help on this before but wish to take it a little further.

set LatestDir=%LatestDir%
Set Dir=c:\Temp
Set Varname=Name of file.xlsx

for /f "delims=" %%a in ('dir /b /a:d /o:n "%Dir%\WR*"') do set LatestDir=%Dir%\%%a
set LatestFile=%LatestDir%\%varname%
dir "%LatestFile%"
------
The above works great..  I now have a xlsm file which runs a macro when a 2nd xlsm file is opened, code below. My question is how can I pull in the above variable for %LatestDir% and apply it to this script below for the path since it changes each week?
------
Public Function TestExcelMacro()
Dim objXLApp As Object
Dim XLApp As Object
Set XLApp = CreateObject("Excel.Application")
With XLApp
.Application.Visible = True
.UserControl = True
.Workbooks.Open "c:\temp\RAMCOMacos.xlsm", True
.Workbooks.Open "c:\temp\wr20160718\New_ End-Dated_Revised.xlsm", False

.Run "RAMCOmacos.xlsm!MacroRAMS_CO_Sort"
End With

Set XLApp = Nothing
End Function

-----------
SO this line below would look something like this in excel.. Pulling in the "LatestDir".  Reason for this is because each week this directory name changes so the code in my XLSM file needs to also reflect what is being capture by the Batch file.  



.Workbooks.Open "%LatestDir%\New_ End-Dated_Revised.xlsm", False
0
Comment
Question by:Culwatrnca11
  • 3
  • 3
7 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 41717996
Will the environment variable be set when the Excel file is opened and macros are run?  If so then you can use the Environ("LatestDir") to get its value.

https://msdn.microsoft.com/en-us/library/office/gg264486.aspx

~bp
0
 
LVL 24

Expert Comment

by:NVIT
ID: 41718004
.Workbooks.Open Environ("LatestDir") & "\New_ End-Dated_Revised.xlsm", False
0
 

Author Comment

by:Culwatrnca11
ID: 41718158
Bill, yes. they are being set by the batch file

Set LastestDir=%LatestDir%. my batch file is running a few other processes and also calls a couple other batch files which also reference %LatestDir%.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 53

Expert Comment

by:Bill Prew
ID: 41719031
Okay, then you should be able to reference environment variables using the Environ() function as I mentioned, let us know if you run into problems.

If you have trouble with your variable you might try a test with a standard system variable like "COMPUTERNAME" and display it in a MsgBox or to the immediate window for debugging.

One other note, make sure the BAT file that sets the variable does not have SETLOCAL in effect with your variable is set, otherwise it won't be visible outside the BAT script.

As long as the Excel sheet with the macro is opened from the same command processor where the BAT script set the variable you should be okay.

~bp
0
 

Author Comment

by:Culwatrnca11
ID: 41719531
Ok, Using Environ () function is working it pulls in the stored path but now I get Visual Basic Error

Run-time error '440':
Automation error

when I debug the line that is highlighted is when it runs the macro

.Run "RAMCOMacos.xlsm!MacroRAMS_CO_Sort"

The macro appears to run but not sure why I am getting an error at this line now.

Here is the code:

Public Function TestExcelMacro()
Dim objXLApp As Object
Dim XLApp As Object
Set XLApp = CreateObject("Excel.Application")
With XLApp
.Application.Visible = True
.UserControl = True
.Workbooks.Open Environ("LatestDir") & "\RAMCOMacos.xlsm", True
.Workbooks.Open Environ("LatestDir") & "\New_ End-Dated_Revised.xlsm", False
.Run "RAMCOMacos.xlsm!MacroRAMS_CO_Sort"
End With

Set XLApp = Nothing
End Function

Both Files are in the same directory when opened then macro is called.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 41719619
Okay, so seems like we solved the question you originally asked, but now have a different question.  I'd suggest closing this question and opening a new one for the new problem.

Naturally if I knew the problem I would just tell you here, but I don't.  I would suggest line by line debugging and stepping into the RUN call to see if you can isolate the error.  Also if you are doing ON ERROR NEXT in any of the code you might want to turn that off for debugging, in case the actual cause of the problem is being masked by that.

Also, you might store the Environ("LatestDir") result into a string variable once, and then use that string inside the code you shared above.  Just to rule out that interfering with something, but I think that's unlikely.  However since you retrieve the environment variable value a couple of times, slightly more efficient anyway to fetch it once, and store locally in a local program variable.

~bp
0
 

Author Comment

by:Culwatrnca11
ID: 41719754
Bill,

Thank you for your help, very much appreciated. Will see if i can figure out what is happening now and if not reach out again if I need assistance.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question