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

前端 未结 1 1454
自闭症患者
自闭症患者 2020-12-11 10:01

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.

相关标签:
1条回答
  • 2020-12-11 10:42

    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 
    
    0 讨论(0)
提交回复
热议问题