Problem with .xlsm file adding code via VBA

Kanji

New Member
Joined
Jul 14, 2011
Messages
3
Hi everyone!

I'm working with a subroutine that creates a new workbook. This workbook is a report that I want to customize by adding a combo box with an event 'OnChange' associated.

In order to add the event procedure from my subroutine to the new workbook, I use the VBIDE object model and, in special, the following code:

Code:
Sub Process()
 
On Error GoTo Error_Handler
 
Dim wbkReport As Workbook
Dim vbpProject As Object
Dim vbcComponent As Object
Dim codModule As Object
Dim lngLine As Long
 
 Workbooks.Add
 
  Set wbkReport = ActiveWorkbook
    
  With wbkReport.Worksheets(1)
    
      .OLEObjects.Add "Forms.ComboBox.1"                'Add a combo box
        
        .OLEObjects(1).Name = "cbxMonth"                'Its inner name
     
            .Range("A5").Value = "January"
            
            .Range("A6").Value = "February"
        
      .OLEObjects(1).ListFillRange = .Name & "!A5:A6"   'Data source
     
    End With
 
 'Now, let's create the event procedure for this combo box
  
  Set vbpProject = wbkReport.VBProject
  
  Set vbcComponent = vbpProject.VBComponents(wbkReport.Worksheets(1).CodeName)
  
  Set codModule = vbcComponent.CodeModule
  
    lngLine = codModule.CreateEventProc("Change", "cbxMonth")   'On change event
    
     codModule.InsertLines lngLine + 1, " VBA.Msgbox ""You choose "" & cbxMonth.Value"
  
  'Finally, save the changes
  
  wbkReport.SaveAs ThisWorkbook.Path & "\Report.xlsm", xlOpenXMLWorkbookMacroEnabled
 
GoTo Final
 
Exit Sub
 
Error_Handler:
  VBA.MsgBox "An error has ocurred"
 
Final:
  Set wbkReport = Nothing
  Set codModule = Nothing
  Set vbcComponent = Nothing
  Set vbpProject = Nothing
 
End Sub

Problem: if the users doesn't fire the event procedure changing the combo value and then closes this file -saving or not changes, however- when they want to open it back, the event procedure will not work: the code was not saved although I used the .SaveAs method in the subroutine that generates the new workbook.

It seems that it's not enough to save as .xlsm and use the right constant for the FileFormat argument; you must fire the event (!!).

Thank you in advance!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Why not just create a workbook/worksheet template with the combobox, and it's event code?

When you need to create a new file just copy that, code and all.

So you wouldn't need to use code to create the combobox and it's code.
 
Upvote 0
Thanks Norie for your answer.

It's clear that the way you told me is easier but is not the right way in my case cause I must change the properties of the destiny worksheet also the combo box -events included- dinamically on deppends of certain data.

However, it's a very strange matter: although saving changes -.xlsm-, the code is not saved.

Thank you again!
 
Upvote 0
Why couldn't you change the porperties of the destination sheet?

Also what changes to you need to make to the code?
 
Upvote 0
[SOLVED] Problem with .xlsm file adding code via VBA

Solved.

I'm playing with the code and find that the problem only appears with the event procedures -object modules-: if you add a 'dummy' standard module, class module, etc. -procedures inside are not needed- instead only event procedures, Excel will save correctly all the code generated via VBIDE in your '.xlsm'.

So it's a bizarre behaviour with the '.xlsm' but with an easy fix.

Thank you all.
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,822
Members
452,946
Latest member
JoseDavid

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