Hello,
This sub completes correctly when run manually from within Module1 of C:\Invoices\InvF1.xlsm
Sub ReplaceThisWorkbook()
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
.DeleteLines StartLine:=1, Count:=.CountOfLines ' Delete all lines
.AddFromFile "c:\Invoices\mods\excel\ThisWorkbook2.cls" ' and replace with ThisWorkbook2
End With
End Sub
The same sub does NOT run correctly from within this vbs script...
...file 'Fx.vbs'
Option Explicit
On Error Resume Next
F1
Sub F1()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Invoices\InvF1.xlsm", 0, True)
xlApp.DisplayAlerts = False
xlApp.Run "ReplaceThisWorkbook"
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
The whole purpose of the above is to make multiple copies of Workbook-A with different 'ThisWorkbook' code.
I would first make an exact copy of Workbook-A (with a different file name) and then run the vbs script 'Fx.vbs'.
Maybe someone can suggest a workaround to make copies of a workbook with different 'ThisWorkbook' code.
Thank you.
-Nico
This sub completes correctly when run manually from within Module1 of C:\Invoices\InvF1.xlsm
Sub ReplaceThisWorkbook()
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
.DeleteLines StartLine:=1, Count:=.CountOfLines ' Delete all lines
.AddFromFile "c:\Invoices\mods\excel\ThisWorkbook2.cls" ' and replace with ThisWorkbook2
End With
End Sub
The same sub does NOT run correctly from within this vbs script...
...file 'Fx.vbs'
Option Explicit
On Error Resume Next
F1
Sub F1()
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Invoices\InvF1.xlsm", 0, True)
xlApp.DisplayAlerts = False
xlApp.Run "ReplaceThisWorkbook"
xlApp.ActiveWorkbook.Save
xlApp.ActiveWorkbook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
The whole purpose of the above is to make multiple copies of Workbook-A with different 'ThisWorkbook' code.
I would first make an exact copy of Workbook-A (with a different file name) and then run the vbs script 'Fx.vbs'.
Maybe someone can suggest a workaround to make copies of a workbook with different 'ThisWorkbook' code.
Thank you.
-Nico