Workbook_Open event Created via VBA doesnt persist after closing and reopening file

AswathiS

New Member
Joined
Dec 14, 2017
Messages
9
I am creating an .xls file via VBA and making it protected
I need to add workbook_Open event for this workbook.
I have added it using the below sample code

Code:
Sub CreateEventProcedure()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim LineNum As Long
        Const DQUOTE = """" ' one " character


        Set VBProj = ActiveWorkbook.VBProject
        Set VBComp = VBProj.VBComponents("ThisWorkbook")
        Set CodeMod = VBComp.CodeModule
        
        With CodeMod
            LineNum = .CreateEventProc("Open", "Workbook")
            LineNum = LineNum + 1
            .InsertLines LineNum, "    MsgBox " & DQUOTE & "Hello World" & DQUOTE
        End With
    End Sub

Throiugh code , i created the file, protected it , added the above code and saved.
But when i closed and re-open, this workbook_open event got vanished.
Please help
 
Last edited by a moderator:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Welcome to the forum.

Are you sure that:
1. The correct workbook was active when you ran that code?
2. You saved it in the correct format?
 
Upvote 0
@RoryA Thanks for your Reply
For answering your questions
1. Yes the Proper workbook was active when i ran this code
2.I saved the workbook as follows

With wNewBook
.Sheets(1).Activate
.SaveLinkValues = False
.Save
.Close
End With
 
Upvote 0
You must have done a SaveAs at some point if it's a new workbook - what format did you specify?
 
Upvote 0
Yes
I have done a save as when ever i create the workbook
Set wNewBook = Workbooks.Add
With wNewBook
.Title = Abc
.Subject =cde
.Application.SheetsInNewWorkbook = pqr
.SaveAs gt_AllFiles.sApprovalFile, FileFormat:=51
.Close
End With
 
Upvote 0
@RoryA

Yes
I have done a save as when ever i create the workbook
Set wNewBook = Workbooks.Add
With wNewBook
.Title = Abc
.Subject =cde
.Application.SheetsInNewWorkbook = pqr
.SaveAs gt_AllFiles.sApprovalFile, FileFormat:=51
.Close
End With
 
Upvote 0
Fileformat 51 is xlOpenXMLWorkbook, which is not a macro-enabled format.
 
Upvote 0
What format are you trying to save it as? Your question mentioned .xls but it looks like you perhaps wanted .xlsm? If so, use 52 rather than 51.
 
Upvote 0
I know .xlsm is a macro enabled workbook.
So If I want to add workbook_open event in this file, should I save it as .xlsm
.xls can't add a workbook_open event?
Sorry if I am asking something wrong.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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