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
54 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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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 23

Expert Comment

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

Author Comment

by:Culwatrnca11
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now