VB Script that copies a VBA function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • irGed
    New Member
    • Jul 2011
    • 18

    VB Script that copies a VBA function

    Hi,

    I searched google to see if there's something that can help me achieve what I wanted to do, but couldn't find any. What I want to achieve is to write a VB script that can copy the code written on the "beforeSave " functionality. And then, writes a macro based on the content of the copied code.

    Is this implementation possible?

    Thanks.
  • irGed
    New Member
    • Jul 2011
    • 18

    #2
    just found out that creating macro on excel using vbscript is doable, just the copying of the "beforeSave " function left.

    Comment

    • irGed
      New Member
      • Jul 2011
      • 18

      #3
      I have solved the problem but not sure if this is the best way to achieve this, if anyone has a better solution, please help me.

      as for the code, I have exported the VB Component that contains the desired function and then I used a third-party tool to modify the generated export file and then I imported it with another excel.

      for the export code:

      Code:
      Dim ArgObj, inputExcel, outputFile
      Set ArgObj = WScript.Arguments 
      
      'First parameter input file
      inputExcel = ArgObj(0) 
      'Second parameter output file
      outputFile = ArgObj(1) 
      
      Set objExcel1 = CreateObject("Excel.Application")
      	objExcel1.DisplayAlerts = 0
      Set objWorkbook1 = objExcel1.Workbooks.Open(inputExcel)
      	objWorkbook1.VBProject.VBComponents("ThisWorkbook").Export (outputFile)
      	objWorkbook1.Save
      	objExcel1.Quit
      	'objExcel2.Quit
      Set	objExcel1 = Nothing
      and for the import code:

      Code:
      Dim ArgObj, inputExcel, outputFile
      Set ArgObj = WScript.Arguments 
      
      'First parameter VALIDATION file
      inputExcel = ArgObj(0) 
      'Second parameter input file
      inputFile = ArgObj(1) 
      
      Set objExcel1 = CreateObject("Excel.Application")
      	objExcel1.DisplayAlerts = 0
      Set objWorkbook1 = objExcel1.Workbooks.Open(inputExcel)
      Set VBComp = objWorkbook1.VBProject.VBComponents("Module1")
      objWorkbook1.VBProject.VBComponents.Remove VBComp
      objWorkbook1.VBProject.VBComponents.Import (inputFile)
      	objWorkbook1.Save
      	objExcel1.Quit
      Set	objExcel1 = Nothing

      Thanks.

      Comment

      Working...