View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Excel Crash in procedure.

Way back I vaguely recall Rob Bovey's highly respected Code Cleaner causing
similar problems while writing (stripping code) to Object modules in
'another' project. Seemed to be a relatively rare and intermittent bug. I
think Rob may have introduced various pauses and DoEvents at appropriate
times to slow things down, which I think solved a significant proportion of
the problems. I don't think even that prevented 100% of problems why not but
not give it a shot.

Private Declare Sub Sleep _
Lib "KERNEL32.DLL" (ByVal dwMilliseconds As Long)

to pause say half a second
Sleep 500

perhaps also add a DoEvents

Add a sleep (or maybe an application.wait) and DoEvents before & after
writing the lines.

In passing, I don't see any reason to use FSO, why not read directly, eg

Sub GetTextByLines()
Dim LineNo As Long
Dim iFF As Integer
Dim Source As String, EntryLine As String
iFF = FreeFile

Close iFF ' just in case not previously closed

Source = "Y:\Quality\ovlSetup\" & module & ".txt"

On Error GoTo errH
Open Source For Input Access Read As #iFF
On Error GoTo 0

Do While Not EOF(iFF)
LineNo = LineNo + 1
Line Input #iFF, EntryLine
Debug.Print LineNo; EntryLine
Loop

errH:
Close iFF
If LineNo = 0 Then MsgBox Source & " not found"
End Sub


Regards,
Peter T


"Tim Rush" wrote in message
...
The activeworkbook and the workbook with the running code are separate. I
have a hidden workbook with the procedures in it that are controlling

this.
I then open the workbook(s) needing the update, and run the procedure

while
they are active.

"Peter T" wrote:

Set VBProj = activeworkbook.VBProject


Is the Activeworkbook same as Thisworkbook. Trying to write code to
worksheet and thisworkbook modules of the same project that's running

the
code is fraught with problems.

Regards,
Peter T

"Tim Rush" wrote in message
...
The procedure below works sometimes and sometimes not.
This is basically Chip Pearson's vbProject routines.
Everything works fine initializing all vb objects and beginning the

add
procdure. Apparently I get the entire text file entered, then

(stepping
through procdure) when the End With is highlighted just above the

LOOP,
Excel does a complete dump and error recovery.
Happens 7 out of 10 times, the other times, it works fine, so that

makes
it
hard to figure out.
I'm using Excel2003, winXP
I do have the extendability library on per Chip
If somebody would like the whole file, I can zip and upload a

declassified
version.

Private Sub AddProcedureToModule()
Dim module As String 'These lines added to test procedure only
module = "OVL"
'
'
On Error GoTo ErrorTrap
'Private Sub AddProcedureToModule(module As String) 'Procedure from

Chip
Pearson
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim objFSO As Object
Dim objFile As Object
Dim LineNum As Long
Dim Source As String
Dim EntryLine As String
Const DQUOTE = """" ' one " character
Source = "Y:\Quality\ovlSetup\" & module & ".txt" 'location of

code
to enter
Set VBProj = activeworkbook.VBProject
'Set VBComp = VBProj.VBComponents(module)
Set VBComp = VBProj.VBComponents(Worksheets(module).CodeName)
'modification by Dave Peterson
Set CodeMod = VBComp.CodeModule
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Source, 1)
Do Until objFile.AtEndOfStream
EntryLine = objFile.readline
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, EntryLine
End With
Loop
ExitHe
objFile.Close
Set objFSO = Nothing
Set objFile = Nothing
Exit Sub
ErrorTrap: 'added brief error handling
MsgBox ("Error in AddProcedure routine.") 'informs which

procedure
to debug
'useful with on

error
resume next
GoTo ExitHe
End Sub