Open run Macro and Close Excel file plus delete another file

Posted on 2014-04-10
Medium Priority
Last Modified: 2014-04-12
Hello Experts,

I have bespoke application that exports a CSV file as 'EXPORT.csv' to my desktop, this file is used to update an Excel spreadsheet called 'SPECIFICATION.xlsx' which has a Macro within called  'CSVSETUP'  that will load the 'EXPORT.csv' file to update 'SPECIFICATION.xlsx' - and my application can call a program that should enable me to run this in the background when 'EXPORT.csv' is sent to the desktop.  

What i wish to do is to simply call 'SPECIFICATION.xlsx' and open it to run the Macro, Save 'SPECIFICATION.xlsx'  and Close it when the Macro has finished running  plus Delete 'EXPORT.csv' from the Desktop when this process is completed.  

What would be the easiest method to do this task and how should any code required to do this be written?

Thank you, in advance of any reply?
Question by:phoenix55
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
LVL 65

Accepted Solution

RobSampson earned 2000 total points
ID: 39993286
Hi, this is untested, but I think it should do the job.



strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Specification.xlsm"
strWBName = Mid(strExcelFile, InStrRev(strExcelFile, "\") + 1)
strMacroName = "MacroName"
strMacroPath = strWBName & "!" & strMacroName

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWB = objExcel.Workbooks.Open(strExcelFile, False, False)
objWB.Application.Run strMacroPath
objWB.Close False

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile objShell.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\EXPORT.csv", True

Open in new window

LVL 65

Expert Comment

ID: 39993289
That's VBScript code, by the way, so place the .VBS file in the same folder as the Specifications.xlsm file, and it should work.



Author Comment

ID: 39993333
Okay, thanks for the prompt reply Rob, will give it a try today.
Independent Software Vendors: 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!


Author Comment

ID: 39996498
Hi Rob,

Gave the code a try out but it needs a line inserted to enable the Macro's, could you advise please?

Author Closing Comment

ID: 39996639
Right, sorted enabled macro's.  Code works brilliantly Rob, much appreciated, thank you.
LVL 65

Expert Comment

ID: 39996754
Thanks for the grade. Typically you need to manually lower the macro security or add the file to Trusted Locations. You probably could do that with a script but it's easier to do it manually.


Featured Post

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.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

765 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