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
62 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 54

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

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 54

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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