copy worksheets into new workbook preserving code and objects

fspino

New Member
Joined
May 20, 2010
Messages
21
Hallo,
I need to copy almost all of the worksheets enclosed in addin workbook in a new workbook as a copy of addin workbook, without the modules and classes, but preserving the code and the object (Image1) included into the originals worksheets.

The code enclosed into the worksheet modules of my addin is very simple:
Code:
Option Explicit
Public Dove As String
Private Sub Image1_Click()
    Dim temp
    If Dove = "Close" Then
        temp = Application.Run("Vecacs_2.11.xla!cancellaLavagna", ActiveSheet.Name)
        'Image1.Visible = False
    'Else
    '    If Dove <> "Bar" Then
    '        MsgBox Dove
    '    End If
    End If
End Sub
Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If Y <= 17 Then
        If X >= 404 Then
            Dove = "Close"
        Else
            Dove = "Bar"
        End If
    Else
        Dove = "X=" + Trim(Str(X)) + ", Y=" + Trim(Str(Y))
    End If
End Sub

The code as shown in the code is linked to an Image enclosed into each worksheet.

My VBA code works exacly as I want: preserves both images and code but when I save the new workbook and then I reopen it the code into the worksheet modules... vanished!

I saw, that's no problem! When the new workbook is re-opened I check if exists the code into the worksheet modules...if doesn't I re-create it...simply!

But, of course, it doesn't work!

I tried two different ways: first one crashes Excel (none explain), the second returns: run-time error 57017 (something like "Routine management of events is not valid") at the row: LineNum = .CreateEventProc(Evento, Oggetto) (see below).

The code to create the aforementioned subs is the following.

Addin module RepairSheet:
Code:
Option Explicit
Public Sub AddEventProcedure(Progetto As String, Modulo As String, Oggetto As String, Evento As String, MacroCode As String)
    Dim VBCodeMod As CodeModule
    Dim LineNum As Long
    Set VBCodeMod = Workbooks(Progetto).VBProject.VBComponents(Modulo).CodeModule
    With VBCodeMod
        LineNum = .CreateEventProc(Evento, Oggetto)
        LineNum = LineNum + 1
        .InsertLines LineNum, MacroCode
    End With
End Sub
Public Sub AddProcedure(Progetto As String, Modulo As String, Macro As String, MacroCode As String)
' Use Microsoft Visual Basic For Applications Extensibility 5.3
    Dim VBCodeMod As CodeModule
    Dim LineNum As Long
    Set VBCodeMod = Workbooks(Progetto).VBProject.VBComponents(Modulo).CodeModule
    With VBCodeMod
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, MacroCode
    End With
    'Dopo che la macro è stata creata la si può lanciare con:
    'Application.Run Macro
End Sub
Public Sub DeleteProcedure(Progetto As String, Modulo As String, Macro As String)
' Use Microsoft Visual Basic For Applications Extensibility 5.3
    Dim VBCodeMod As CodeModule
    Dim StartLine As Long
    Dim HowManyLines As Long
    Set VBCodeMod = Workbooks(Progetto).VBProject.VBComponents(Modulo).CodeModule
    With VBCodeMod
        StartLine = .ProcStartLine(Macro, vbext_pk_Proc)
        HowManyLines = .ProcCountLines(Macro, vbext_pk_Proc)
        .DeleteLines StartLine, HowManyLines
    End With
End Sub
Public Function ComponentName(Progetto As String, Foglio As String) As String
' Use Microsoft Visual Basic For Applications Extensibility 5.3
    Dim comp As VBComponent
    ComponentName = ""
    For Each comp In Workbooks(Progetto).VBProject.VBComponents
        If comp.Properties.Item("Name") = Foglio Then
            ComponentName = comp.Name
            Exit For
        End If
    Next comp
End Function
Public Function CheckProcedure(Progetto As String, Modulo As String, Macro As String) As Boolean
' Use Microsoft Visual Basic For Applications Extensibility 5.3
    Dim VBCodeMod As CodeModule
    Dim StartLine As Long
    Dim HowManyLines As Long
    HowManyLines = 0
    StartLine = 0
    CheckProcedure = False
    On Error Resume Next
    Set VBCodeMod = Workbooks(Progetto).VBProject.VBComponents(Modulo).CodeModule
    If VBCodeMod Is Nothing Then
        CheckProcedure = False
    Else
        With VBCodeMod
            StartLine = .ProcStartLine(Macro, vbext_pk_Proc)
            HowManyLines = .ProcCountLines(Macro, vbext_pk_Proc)
        End With
        If StartLine = 0 And HowManyLines = 0 Then CheckProcedure = False Else CheckProcedure = True
    End If
End Function
Public Sub AddBlackboardCode(Progetto As String, Modulo As String)
    Dim MacroCode As String
    Dim Macro As String
    Macro = "Image1_Click"
    MacroCode = _
    "Option Explicit" & vbCrLf & _
    "Public Dove As String"
    AddProcedure Progetto, Modulo, Macro, MacroCode
    
    '"Private Sub Image1_Click()" & vbCrLf &
    MacroCode = _
    "    Dim temp" & vbCrLf & _
    "    If Dove = ""Close"" Then" & vbCrLf & _
    "        temp = Application.Run(""Vecacs_2.11.xla!cancellaLavagna"", ActiveSheet.Name)" & vbCrLf & _
    "        'Image1.Visible = False" & vbCrLf & _
    "    'Else" & vbCrLf & _
    "    '    If Dove <> ""Bar"" Then" & vbCrLf & _
    "    '        MsgBox Dove" & vbCrLf & _
    "    '    End If" & vbCrLf & _
    "    End If" & vbCrLf
    '"End Sub" & vbCrLf
    AddEventProcedure Progetto, Modulo, "Image1", "Click", MacroCode
    
    '"Private Sub Image1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal Y As Single)" & vbCrLf &
    MacroCode = _
    "    If Y <= 17 Then" & vbCrLf & _
    "        If x >= 404 Then" & vbCrLf & _
    "            Dove = ""Close""" & vbCrLf & _
    "        Else" & vbCrLf & _
    "            Dove = ""Bar""" & vbCrLf & _
    "        End If" & vbCrLf & _
    "    Else" & vbCrLf & _
    "        Dove = ""X="" + Trim(Str(x)) + "", Y="" + Trim(Str(Y))" & vbCrLf & _
    "    End If" & vbCrLf
    '"End Sub" & vbCrLf
    AddEventProcedure Progetto, Modulo, "Image1", "MouseMove", MacroCode
End Sub

Addin module CmdExec:
Code:
      ...
      ...
      ActWsName = ActWsName = ActiveSheet.Name
      ...
      ...
      actModName = ComponentName(ActiveWorkbook.Name, ActWsName)
      If actModName <> "" Then
        If Not CheckProcedure(ActiveWorkbook.Name, actModName, "Image1_Click") Then
            AddBlackboardCode ActiveWorkbook.Name, actModName
        End If
      End If
      ...
      ...

Not all!
If I create, manually, the code into the worksheets into the reopened workbook the code is not linked to "Image1")!

Then...now I have now two chance:
1. I find a system to create new workbook with the object and code that remain the same after saved.
2. I find a system to generate code into the worksheet of the new workbook as it reopened.

Please, can somebody helps me?

Many thanks in advance.

Francesco
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Your post is very confusing, for me anyway.

I can't see how the code would suddenly disappear when you save the new workbook.

Perhaps it's some sort of macro security issue, what version of Excel are you using?

By the way why do you want/need to save a copy of an add-in?

I thought one of the ideas of add-ins was that they could be used in all your workbooks.
 
Upvote 0
Your post is very confusing, for me anyway.

I can't see how the code would suddenly disappear when you save the new workbook.

Perhaps it's some sort of macro security issue, what version of Excel are you using?

By the way why do you want/need to save a copy of an add-in?

I thought one of the ideas of add-ins was that they could be used in all your workbooks.

I use Excel 2003 (Office Professional),
I need to copy just some sheets from add-in to workbook, because the application (add-in precisely) needs very complex sheets to work. Same of theese sheets also contains an image that I use like a blackboard to draw the plans that the data rapresent.

None security level setted.

Any idea?
Thanx
 
Upvote 0
I use Excel 2003 (Office Professional),
I need to copy just some sheets from add-in to workbook, because the application (add-in precisely) needs very complex sheets to work. Same of theese sheets also contains an image that I use like a blackboard to draw the plans that the data rapresent.

None security level setted.

Any idea?
Thanx

I found this note in http://msdn.microsoft.com/en-us/library/x80526fk.aspx:
When you create a workbook programmatically, it is a native Microsoft.Office.Interop.Excel.Workbook object, not a Microsoft.Office.Tools.Excel.Workbook host item.

You can generate a Microsoft.Office.Tools.Excel.Workbook host item for a Microsoft.Office.Interop.Excel.Workbook object in an application-level project. For more information, see Extending Word Documents and Excel Workbooks in Application-Level Add-ins at Run Time.


May be my case!

But I can't understand how I can generate an Microsoft.Office.Tools.Excel.Workbook, if I need it! :confused:
 
Upvote 0
Where does Interop come into this?

What programming language are you actually using?

By the way I still don't get the addin part - why don't you use a template?
 
Upvote 0
Where does Interop come into this?

What programming language are you actually using?

By the way I still don't get the addin part - why don't you use a template?

I don't know how Interop come into, but it seems come into this (microsoft say that).

I use VBA (Microsoft Visual Basic 6.3).

I don't know how to use a template. Is it a external file o inside at the addin?
Because I don't want to use a setup to install the addin, but I'd like to distribuite it like a file (.xla)
 
Upvote 0
I don't know how Interop come into, but it seems come into this (microsoft say that).

I use VBA (Microsoft Visual Basic 6.3).

I don't know how to use a template. Is it a external file o inside at the addin?
Because I don't want to use a setup to install the addin, but I'd like to distribuite it like a file (.xla)

To see how I copythe worksheets enclosed in addin workbook in a new workbook as a copy of addin workbook, without the modules and classes, but preserving the code and the object (Image1) included into the originals worksheets, see http://www.mrexcel.com/forum/showthread.php?t=469518. :oops:

Thanks
 
Upvote 0

Forum statistics

Threads
1,215,087
Messages
6,123,046
Members
449,092
Latest member
ikke

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