By example:
Option Explicit
Sub example()
Const vbext_ct_StdModule = 1 'Standard module
Const vbext_ct_ClassModule = 2 'Class module
Const vbext_ct_MSForm = 3 'Microsoft Form
Const vbext_ct_ActiveXDesigner = 11 'ActiveX Designer
Const vbext_ct_Document = 100 'Document Module
Dim WB As Workbook
Dim VBComp As Object ' VBComponent
Dim VBCompThisWorkbook As Object ' VBComponent
'Create new WB and copy whatever sheet(s) to it.
Set WB = Workbooks.Add(xlWBATWorksheet)
Sheet1.Copy After:=WB.Worksheets(1)
Application.DisplayAlerts = False
'Delete the sheet initially created in the new WB
WB.Worksheets(1).Delete
Application.DisplayAlerts = True
For Each VBComp In WB.VBProject.VBComponents
If VBComp.Type = vbext_ct_Document Then
'Pick a component property that is different between the ThisWorkbook module
'and a worksheet's module.
If LCase$(VBComp.Properties(7).Name) = "author" Then
Set VBCompThisWorkbook = VBComp
Exit For
End If
End If
Next
If Not VBCompThisWorkbook Is Nothing Then
'Since we might not know whether the user has Option Explicit required, delete it if it's there.
VBCompThisWorkbook.CodeModule.DeleteLines 1, VBCompThisWorkbook.CodeModule.CountOfDeclarationLines
'Add our code
VBCompThisWorkbook.CodeModule.AddFromString _
"Option Explicit" & vbLf & vbLf & _
"Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)" & vbLf & vbLf & _
" If Not MsgBox(""Are you sure you want to save?"", vbYesNo Or vbQuestion, vbNullString) = vbYes Then" & vbLf & _
" Cancel = True" & vbLf & _
" MsgBox ""<Save> cancelled..."", vbInformation, vbNullString" & vbLf & _
" End If" & vbLf & vbLf & _
"End Sub"
End If
End Sub