Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
79 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 58

Accepted Solution

by:
Bill Prew earned 2000 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 25

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 58

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 58

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

610 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