简体   繁体   中英

Remove Workbook_Open Code In the copy saved with Save As from VBA

I'm trying to force the user to save the newly filled template (created with VBA and the macros inside it) to avoid data loss when it reopens (so users don't have to delete the data after creating executing the program each time), and I read something about enabling and disabling events, and I tried it but no luck. It saves the same book but if I open it, it seems to be executing anyway. Is there anyway to strip the macros from that copy?

The only Macro I'd be interested in disabling and/or deleting will be the code inside the Workbook_Open because that's the one that calls the modules code and such... and I'm only interested in removing it from the copy that I'm saving with another name. Not the file the user opened in the first place.

Further explaination:

I required the code on Workbook_Open. Let me try to explain better, I have a sheet with all the code in it, when it opens among other things it asks you to select some input files in order to fill the workbook, after it's done, it asks the user to save the filled workbook as a copy, but when I open that saved copy, it's like i'm opening the first sheet. And I'd like to just show the filled template instead of acting like an exact copy. That's why I thought of removing the Workbook

Code:

Application.ScreenUpdating = True
ThisWorkbook.Activate
MsgBox "Site Configuration List" & vbNewLine & "Generated Sucessfully", vbOKOnly, "SCL Generated Sucessfully"
MsgBox "Remember to save the file with a different filename" & vbNewLine & "The next time the program executes it'll erase all information contained in it", vbInformation
SaveWorkbookAsNewFile (Project & "_" & ProjectName)

Private Sub SaveWorkbookAsNewFile(NewFileName As String)
Dim ActSheet As Worksheet
Dim ActBook As Workbook
Dim CurrentFile As String
Dim NewFileType As String
Dim NewFile As String

Application.ScreenUpdating = False ' Prevents screen refreshing.
Application.EnableEvents = False
CurrentFile = ThisWorkbook.FullName

NewFileType = "Excel Files 1997-2003 (*.xls), *.xls"

NewFile = Application.GetSaveAsFilename( _
InitialFileName:=NewFileName, _
fileFilter:=NewFileType)

If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveCopyAs Filename:=NewFile, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=True, _
CreateBackup:=False
End If

Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub

I found something similar here before but that really didn't helped me.

I'm using excel 2007. In the new file (Filled sheet) I don't require any code. I'd like just to show the information and that's it – Splendonia just now

In such a case all you need to do is save the file as an .xlsx file. You don't have to do the dirty work of removing the code. This file will automatically be stripped of all the code.

Change the line

ActiveWorkbook.SaveCopyAs Filename:=NewFile, _
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=True, _
CreateBackup:=False

to

ActiveWorkbook.SaveAs Filename:=NewFile, _
FileFormat:= xlOpenXMLWorkbook , _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=True, _
CreateBackup:=False

Also remember to change the NewFileType accordingly.

EDIT

Oops, I forgot to say I might need to save it as a .xls file, the program in which the users need to upload this later does not support >excel2007 files – Splendonia 5 mins ago

To delete the Workbook_Open , use this code (TRIED AND TESTED). Courtesy Chip Pearson . Please do not forget to read the Introduction mentioned in the link before you try this code.

Option Explicit

Sub DeleteProcedureFromModule()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule

    Dim StartLine As Long
    Dim NumLines As Long
    Dim ProcName As String

    Set VBProj = ActiveWorkbook.VBProject
    Set VBComp = VBProj.VBComponents("ThisWorkbook")
    Set CodeMod = VBComp.CodeModule

    ProcName = "Workbook_Open"
    With CodeMod
        StartLine = .ProcStartLine(ProcName, vbext_pk_Proc)
        NumLines = .ProcCountLines(ProcName, vbext_pk_Proc)
        .DeleteLines StartLine:=StartLine, Count:=NumLines
    End With
End Sub

I'm not sure this is possible in code... (watch Siddharth prove me wrong)..

One way I could think of doing this is to check for existance of a value in a dedicated cell before running the code..

Private Sub Workbook_Open()

   Dim rng As Range

   Set rng = Sheet1.Range("A1")

    If rng.Value = "run" Then

    'put code in here

    rng.Value = ""

    End If

End Sub

Can you explain why the link you provide did not work for you? I was going to suggest something similar... it seems as though if they're saving a new copy, then you could intercept this on the BeforeSave event and just remove the code from the module using the standard API. Take a look at this:

http://msdn.microsoft.com/en-us/library/aa443970(v=vs.60).aspx

This object is available to you in VBA and something like this should work fine for what you're explaining... but if it isn't then we'd help you better if you could explain where and why this isn't doing what you're expecting it to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM