Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Application error when attempting to write code to a workbook

I'm attempting to use a Macro to create a workbook and add code to that
workbook.
This is the point in my code where I'm having trouble:

code = "private sub commandbutton1_click()" & vbCrLf & _
"Ans=MsgBox(" & Chr(34) & "Are you sure that you are ready to
send?" & Chr(34) & ", vbYesNo)" & vbCrLf & _
"if Ans=vbYes Then Thisworkbook.Route" & vbCrLf & _
"end sub"
With ActiveWorkbook.VBProject.VBComponents("Sheet1").Co deModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, code
End With

After this code runs, I get an application error stating that the memorry
cannot be "read", and Excel crashes. If I change "Sheet1" to "ThisWorkbook",
it runs fine. (I'm sure sheet1 exists.) Also, if, after Nextline, if I simply
type text (in quotes) and don't attempt to reference a variable containing
code, it runs fine. Is this an Excel bug? I'm using Excel XP on a Windows
2000 computer.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Application error when attempting to write code to a workbook

After examining things further, I noticed that if the code that I'm trying to
pass contains spaces, I get the error. However, if it is just something like
"ThisWorkbook.Route", I don't get the error.

"Jared" wrote:

I'm attempting to use a Macro to create a workbook and add code to that
workbook.
This is the point in my code where I'm having trouble:

code = "private sub commandbutton1_click()" & vbCrLf & _
"Ans=MsgBox(" & Chr(34) & "Are you sure that you are ready to
send?" & Chr(34) & ", vbYesNo)" & vbCrLf & _
"if Ans=vbYes Then Thisworkbook.Route" & vbCrLf & _
"end sub"
With ActiveWorkbook.VBProject.VBComponents("Sheet1").Co deModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, code
End With

After this code runs, I get an application error stating that the memorry
cannot be "read", and Excel crashes. If I change "Sheet1" to "ThisWorkbook",
it runs fine. (I'm sure sheet1 exists.) Also, if, after Nextline, if I simply
type text (in quotes) and don't attempt to reference a variable containing
code, it runs fine. Is this an Excel bug? I'm using Excel XP on a Windows
2000 computer.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible to write VBA code to send out an EMAIL everytime a workbook is opened? Marcello do Guzman[_2_] Excel Programming 4 September 2nd 04 07:59 AM
Application error with VBA code jd Excel Programming 1 May 6th 04 09:42 AM
Write to closed workbook code Todd Huttenstine[_3_] Excel Programming 4 May 4th 04 09:59 AM
Application Error when running Macro on Workbook open TM[_2_] Excel Programming 1 October 2nd 03 10:53 AM
Application Error when running Macro on Workbook open TM[_2_] Excel Programming 0 September 15th 03 12:33 PM


All times are GMT +1. The time now is 03:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"