Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject and execute Excel VBA code into spreadsheet received from external source

I would like to programatically (in vbscript or command line or whatever) inject VBA code into a workbook I get from an FTP site and then execute the macro on the workbook. All automated. No user interaction.

I have the VBA code defined in a text file.

like image 640
Joe Avatar asked Nov 16 '25 12:11

Joe


1 Answers

Haven't had a chance to try this yet but i think this will do what i need.

Set objExcel = CreateObject("Excel.Application") 
objExcel.Visible = True 
objExcel.DisplayAlerts = False 
Set  objWorkbook = objExcel.Workbooks.Open("C:\scripts\test.xls") 
   Set xlmodule = objworkbook.VBProject.VBComponents.Add(1)  
   strCode = _ 
   "sub test()" & vbCr & _ 
   "   msgbox ""Inside the macro"" " & vbCr & _ 
   "end sub" 
   xlmodule.CodeModule.AddFromString strCode 
objWorkbook.SaveAs "c:\scripts\test.xls" 
objExcel.Quit 
like image 187
Joe Avatar answered Nov 18 '25 20:11

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!