View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default CreateEventProc causes crash

OK, event proc's in multiple modules. Have a go with the following, quite a
few changes -

Sub test()
Dim i As Long
Dim sty As String
Dim sProc As String
Dim arr
Dim arrCodeName() As String
Const Q As String = """"

sty = "My Custom Chart Type"
arr = Array("Chart1", "Chart2") ' << CHANGE

ReDim arrCdNm(LBound(arr) To UBound(arr))

For i = LBound(arr) To UBound(arr)

arrCdNm(i) = ActiveWorkbook.Charts(arr(i)).CodeName
If Len(arrCdNm(i)) = 0 Then
' flash the VBIDE to update codename of a new sheet
With Application.VBE.MainWindow
.Visible = True
.Visible = False
End With
arrCdNm(i) = ActiveWorkbook.Charts(arr(i)).CodeName
End If
Next

For i = 0 To UBound(arrCdNm)
sProc = " 'CreateEventProcedure3 " & _
Q & arrCdNm(i) & Q & _
", " & _
Q & sty & Q & " ' "
Application.OnTime Now, sProc
Next

End Sub

Sub CreateEventProcedure3(sCodeName As String, sStyle As String)
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long, i As Long
Dim arrLines(0 To 1) As String
Const DQUOTE = """" ' double quote

arrLines(0) = " Me.ApplyCustomType ChartType:=xlUserDefined, _"
arrLines(1) = " TypeName:=" & DQUOTE & sStyle & DQUOTE

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

With CodeMod
LineNum = .CreateEventProc("Calculate", "Chart")
For i = 0 To 1
LineNum = LineNum + 1
.InsertLines LineNum, arrLines(i)
Next
End With

End Sub

If you need to do other suff 'after' the OnTime macros have run, note OnTime
macros all called at the same time get called in reverse order. So code the
'last' Ontime macro 'first', if that makes sense.

surely you don't want to update the style of the chart every
time it calculates !

I need to because pivot charts loose there style each time they are
changed (KB215904)


Ah, pivot charts, understood.

Regards,
Peter T


"Thomas" wrote in message
...
Hi Peter,

thanks for cleaning up my mess a bit ;)
I followed all your hints line by line, and still I could not solve
the problem.
The script runs fine as long as CreateEventProcedure3 is only called
once and the script is finished afterwards. As soon as I ad more code
after the call of that Sub, Excel will crash.

Sub test()
CreateEventProcedure3 "chart 1", "My Custom Chart Type"
'The next line would crash excel:
'CreateEventProcedure3 "chart 2", "My Custom Chart Type"
'this one would crash also:
'Application.Wait(Now + TimeValue("0:00:5"))
'these two lines work fine:
Application.ScreenUpdating = True
Application.VBE.MainWindow.Visible = False
End Sub

What confuses me most is the fact that if I run the script only with
the first call of CreateEventProcedure and afterwards modify the
script (edit Sheet Name) to run it again for the next sheet, it runs
smoothly. What does this mean?

surely you don't want to update the style of the chart every

time it calculates !
I need to because pivot charts loose there style each time they are
changed (KB215904)

Can anybody reproduce my issue? Any hints welcome!

Thanks and best regards,
Thomas