Avatar of Culwatrnca11
Culwatrnca11Flag for United States of America

asked on 

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

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
VB ScriptWindows BatchMicrosoft DOS

Avatar of undefined
Last Comment
Culwatrnca11
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of NVIT
NVIT
Flag of United States of America image

.Workbooks.Open Environ("LatestDir") & "\New_ End-Dated_Revised.xlsm", False
Avatar of Culwatrnca11
Culwatrnca11
Flag of United States of America image

ASKER

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%.
Avatar of Bill Prew
Bill Prew

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
Avatar of Culwatrnca11
Culwatrnca11
Flag of United States of America image

ASKER

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.
Avatar of Bill Prew
Bill Prew

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
Avatar of Culwatrnca11
Culwatrnca11
Flag of United States of America image

ASKER

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.
VB Script
VB Script

VBScript (Visual Basic Scripting Edition) is an interpreted scripting language developed by Microsoft that is modeled on Visual Basic, but with some important differences. VBScript is commonly used for automating administrative and other tasks in Windows operating systems (by means of the Windows Script Host) and for server-side scripting in ASP web applications. It is also used for client-side scripting in Internet Explorer, specifically in intranet web applications.

39K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo