PC Review


Reply
Thread Tools Rate Thread

Application Crash Whie adding Code lines using VBA

 
 
Amit Kumar
Guest
Posts: n/a
 
      13th Mar 2008
Hi,

Executing the code given below throws an error:
"Microsoft Office Excel has encountered a problem and needs to close. We
are sorry for the inconvenience."

But If I remove first insertLine statement that is ".InsertLines LineNum,
"Private Sub.........." the program is excuting fine.

Please help me on this.

Thanks!!

Sub WriteEventHandler(LabelName As String)
Dim LineNum As Long
Const DQUOTE = """"
Set RenameCodeMod =
ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
With RenameCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Private Sub " + LabelName + "_DblClick(ByVal
Cancel As MSForms.ReturnBoolean)"
LineNum = LineNum + 1
.InsertLines LineNum, " CurName = " + Trim(LabelName) + ".Caption"
LineNum = LineNum + 1
.InsertLines LineNum, " newname = InputBox(" + DQUOTE + "Enter
new name for " + DQUOTE + " + CurName, " + DQUOTE + "Rename" + DQUOTE + ",
CurName)"
LineNum = LineNum + 1
.InsertLines LineNum, " If Len(Trim(newname)) > 0 Then " +
Trim(LabelName) + ".Caption = Trim(newname)"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub

 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      13th Mar 2008
I've amended your routine slightly and with the test all seems to work OK.
Having said that, there are scenarios where adding event code to class
modules can cause the project to recompile and crash Excel (eg under certain
circumstances when adding new code to the project that's running the insert
new code).

If the following doesn't work it's probably related to what you are doing
overall.


Sub WriteEventHandler(LabelName As String, sModName As String)
Dim LineNum As Long
Dim RenameCodeMod As Object
Const DQUOTE = """"

Set RenameCodeMod = _
ActiveWorkbook.VBProject.VBComponents(sModName).CodeModule
With RenameCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Private Sub " & LabelName _
& "_DblClick(ByVal Cancel As MSForms.ReturnBoolean)"
LineNum = LineNum + 1
.InsertLines LineNum, " CurName = " & Trim(LabelName) &
".Caption"
LineNum = LineNum + 1
.InsertLines LineNum, _
" newname = InputBox(" & DQUOTE & _
"Enter new name for " & DQUOTE & " & CurName, " _
& DQUOTE & "Rename" & DQUOTE & ",CurName)"
LineNum = LineNum + 1
.InsertLines LineNum, _
" If Len(Trim(newname)) > 0 Then " _
& Trim(LabelName) & ".Caption = Trim(newname)"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub

Sub AddLabel()
Dim sName As String, sCodeName As String
Dim ws As Worksheet
Dim ole As OLEObject
Set ws = ActiveSheet
Set ole = ws.OLEObjects(2)
sCodeName = ws.CodeName
' note newly added sheet won't return codename until saved or
' unless the VBE is open or unless other trick done to re-compile

Set ole = ActiveSheet.OLEObjects(1)
With Range("B34")
Set ole = ActiveSheet.OLEObjects.Add( _
ClassType:="Forms.Label.1", _
Left:=.Left, Top:=.Top, _
Width:=.Width, Height:=.Height)
End With
ole.Object.Caption = "Double-click me to change caption"
ole.Object.BackColor = RGB(210, 210, 250)
sName = ole.Name
WriteEventHandler sName, sCodeName
End Sub


Regards,
Peter T


"Amit Kumar" <Amit (E-Mail Removed)> wrote in message
news:243678F9-166F-425F-B2ED-(E-Mail Removed)...
> Hi,
>
> Executing the code given below throws an error:
> "Microsoft Office Excel has encountered a problem and needs to close. We
> are sorry for the inconvenience."
>
> But If I remove first insertLine statement that is ".InsertLines LineNum,
> "Private Sub.........." the program is excuting fine.
>
> Please help me on this.
>
> Thanks!!
>
> Sub WriteEventHandler(LabelName As String)
> Dim LineNum As Long
> Const DQUOTE = """"
> Set RenameCodeMod =
> ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
> With RenameCodeMod
> LineNum = .CountOfLines + 1
> .InsertLines LineNum, "Private Sub " + LabelName +

"_DblClick(ByVal
> Cancel As MSForms.ReturnBoolean)"
> LineNum = LineNum + 1
> .InsertLines LineNum, " CurName = " + Trim(LabelName) +

".Caption"
> LineNum = LineNum + 1
> .InsertLines LineNum, " newname = InputBox(" + DQUOTE + "Enter
> new name for " + DQUOTE + " + CurName, " + DQUOTE + "Rename" + DQUOTE + ",
> CurName)"
> LineNum = LineNum + 1
> .InsertLines LineNum, " If Len(Trim(newname)) > 0 Then " +
> Trim(LabelName) + ".Caption = Trim(newname)"
> LineNum = LineNum + 1
> .InsertLines LineNum, "End Sub"
> End With
> End Sub
>



 
Reply With Quote
 
Robert Bruce
Guest
Posts: n/a
 
      13th Mar 2008
Yn newyddion: %(E-Mail Removed),
Roedd Peter T <peter_t@discussions> wedi ysgrifennu:

> I've amended your routine slightly and with the test all seems to
> work OK. Having said that, there are scenarios where adding event
> code to class modules can cause the project to recompile and crash
> Excel (eg under certain circumstances when adding new code to the
> project that's running the insert new code).
>
> If the following doesn't work it's probably related to what you are
> doing overall.


See also the CreateEventProc method.

Rb
 
Reply With Quote
 
Amit Kumar
Guest
Posts: n/a
 
      13th Mar 2008
I already tried using CreateEventProc... But getting same error.

"Robert Bruce" wrote:

> Yn newyddion: %(E-Mail Removed),
> Roedd Peter T <peter_t@discussions> wedi ysgrifennu:
>
> > I've amended your routine slightly and with the test all seems to
> > work OK. Having said that, there are scenarios where adding event
> > code to class modules can cause the project to recompile and crash
> > Excel (eg under certain circumstances when adding new code to the
> > project that's running the insert new code).
> >
> > If the following doesn't work it's probably related to what you are
> > doing overall.

>
> See also the CreateEventProc method.
>
> Rb
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      13th Mar 2008
Although CreateEventProc ensures the event is correctly written, providing
the control already exists, it has the possible disadvantage of opening the
VBE to display the new event. Also I very much doubt it would prevent the
crash problem, which I'd bet is related to an untimely recompile of the
project while the code is running. That depends of course on what you are
doing overall as I mentioned previously.

In the AddLabel routine I posted, forgot to remove a couple of lines
accidentally left in for my own testing.

Set ole = ws.OLEObjects(2)
and a bit further down -
Set ole = ws.OLEObjects(1).

Regards,
Peter T


"Amit Kumar" <(E-Mail Removed)> wrote in message
news:9E4125E1-BE41-46C8-8F84-(E-Mail Removed)...
> I already tried using CreateEventProc... But getting same error.
>
> "Robert Bruce" wrote:
>
> > Yn newyddion: %(E-Mail Removed),
> > Roedd Peter T <peter_t@discussions> wedi ysgrifennu:
> >
> > > I've amended your routine slightly and with the test all seems to
> > > work OK. Having said that, there are scenarios where adding event
> > > code to class modules can cause the project to recompile and crash
> > > Excel (eg under certain circumstances when adding new code to the
> > > project that's running the insert new code).
> > >
> > > If the following doesn't work it's probably related to what you are
> > > doing overall.

> >
> > See also the CreateEventProc method.
> >
> > Rb
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding lines of code to a macro to delete rows childofthe1980s Microsoft Excel Programming 11 27th Dec 2009 07:23 AM
LONG CODE LINES => CRASH-STARTER PACALA_BA Microsoft Access 0 4th Dec 2008 06:19 PM
adding code lines with vba code thread Microsoft Excel Programming 4 6th Feb 2008 01:31 PM
Adding code to a control on a form causes excel to crash =?Utf-8?B?U2t1emFwbw==?= Microsoft Excel Programming 1 21st Sep 2005 02:13 PM
How to crash the IDE/Compiler in 5 lines of code (BUG) Rarlfth Nadar Microsoft VB .NET 5 28th Sep 2004 11:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:46 AM.