Error when Execute New Procedure Created Using VBE

R

Resant

Hi all, I have a problem.
I try to replace a procedure (named Dynamic_Macro) at run-time, by
delete the procedure and then add it again using VBE. That's work
succesfully...
But when I try to execute that new procedure, the Excel always close
and restart again. I have add timer between create the procedure and
execute process, but still cause Excel restart. I'm sure nothing wrong
with the new procedure, 'cause when I separate the executing coding,
it's executed succesfully.

My coding is :
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long
Dim LineNum As Long

strScript = ""
Set VBCodeMod =
ThisWorkbook.VBProject.VBComponents("Module2").CodeModule

With VBCodeMod
'Delete proc Dynamic_Macro
StartLine = .ProcStartLine("Dynamic_Macro", vbext_pk_Proc)
HowManyLines = .ProcCountLines("Dynamic_Macro",
vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines

'Add again proc Dynamic_Macro
strScript = ""
ReadText "Macro.txt", strScript
LineNum = .CountOfLines + 1
.InsertLines LineNum, strScript
Application.Wait (Now() + TimeValue("0:00:50"))
Application.Run "Dynamic_Macro"
End With

ReadText is a procedure that read Dynamic_Macro content from a file
text "Macro.txt" and pasing to strScript variable.

Please would should I do in order to I can excute it immediately after
created in one procedure?



Thanks for your help,

Resant
 
B

Bob Phillips

Resant,

I haven't tested this, but maybe if you force a re-compile in the code it
might work

Application.VBE.CommandBars.FindControl(ID:=578).Execute

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
R

Resant

Thanks for your reply, Bob
But it's still doesn't work. The Excel still restart.
Any other idea please?
 
B

Bob Phillips

Glad you are sorted.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top