Adding text to macro via macro

wmtsub

Active Member
Joined
Jun 20, 2018
Messages
322
So I am using this code:

Sub insert()
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString ("Private Sub Workbook_BeforeSave ()")

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString ("Cancel = True")

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString ("Application.ThisWorkbook.Saved = True")

ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.AddFromString ("End Sub")
End Sub

And I am getting this result:

Cancel = True
Application.ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_BeforeSave()

I can not get the private sub to print a the top of the list. I can change the order of all the other but this always goes to the bottom. And so it will not work.

Any idea why?
or a different way to do this?

-Eds
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
Sub insert()
Dim LineNum%
With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    LineNum = .CreateEventProc("BeforeSave", "Workbook")
    LineNum = LineNum + 1
    .InsertLines LineNum, "Cancel=true"
    LineNum = LineNum + 1
    .InsertLines LineNum, "Application.ThisWorkbook.Saved = True"
    LineNum = LineNum + 1
    .InsertLines LineNum, "end sub"
End With
End Sub
 
Upvote 0
You can use the CreateEventProc method to create any event procedure, such as BeforeSave...

Code:
Sub CreateBeforeSaveProcedure()

    Dim text As String
    Dim textLine As Long
    
    text = "ThisWorkbook.Saved = True"


    With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
        textLine = .CreateEventProc("BeforeSave", "Workbook")
        .InsertLines textLine + 1, text
    End With
    
End Sub

Hope this helps!
 
Upvote 0
Sorry, for my solution, the text for the body of the event procedure should have been...

Code:
    text = "Cancel = True"
    text = text & vbCrLf & "ThisWorkbook.Saved = True"
 
Upvote 0
Over the week end I also found this to work.


Sheets("sheet1").Select
Sheets("sheet1").Copy
Application.WindowState = xlNormal
awn = ActiveWorkbook.Name
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 1, "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 2, "Cancel = True"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 3, "Application.ThisWorkbook.Saved = True"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 4, "End Sub"
 
Upvote 0
So to add purpose to this macro. I need to copy a worksheet to a new workbook and protect it so it can not be saved and also close the original workbook. I am hung up on the macro which only prints to the original workbook. I have tried to activated the new workbook but it does not seem to work. Any suggestions would be appreciated.

Sheets("sheets 1").Select
Sheets("sheets 1 ").Copy
Application.WindowState = xlNormal
awn = ActiveWorkbook.Name
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 1, "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 2, "Cancel = True"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 3, "Application.ThisWorkbook.Saved = True"
ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.InsertLines 4, "End Sub"
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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