How to change 'ThisWorlbook' code.

Nico1000

New Member
Joined
Sep 22, 2016
Messages
4
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
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
True for the 3rd argument in Workbooks.Open means 'read only', which means any changes to the workbook won't be saved, therefore try changing it to False. The 2nd argument means 'Update links' and should also be a Boolean.

Code:
Set xlBook = xlApp.Workbooks.Open("C:\Invoices\InvF1.xlsm", False, False)
Please use CODE tags - the # icon in the reply box.
 
Upvote 0

Forum statistics

Threads
1,215,016
Messages
6,122,700
Members
449,092
Latest member
snoom82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top