Hello,
this forum is typically for end user questions and may include the odd answer to basic VBA topics. For more advanced VBA topics you may want to try the Excel Developer Forum forum, where the VBA specialists hang out.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
My code stopped after execution of line marked "yellow" with error msg:
Sub AddReferenceToProject()
'===============================================================================
'adding reference Microsoft Visual Basic for Applications Extensibility 5.3 using GUID:
'Without this reference the code below will not work
'===============================================================================
ThisWorkbook.VBProject.References.AddFromGuid _
GUID:="{0002E157-0000-0000-C000-000000000046}", _
Major:=5, Minor:=3
End Sub
__________________________________________________________________________________________
Sub AddModuleToProject()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "NewModule"
End Sub
______________________________________________________________________________________________
Sub AddProcedureToModule()
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """"
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents("NewModule")
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Public Sub SayHello()"
LineNum = LineNum + 1
.InsertLines LineNum, " MsgBox " & DQUOTE & "Hello World" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
End Sub
Note: My machine Windows-7.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Hello,
this forum is typically for end user questions and may include the odd answer to basic VBA topics. For more advanced VBA topics you may want to try the Excel Developer Forum forum, where the VBA specialists hang out.
Your code runs fine for me. I first tested by running each sub separately and then added the following sub to call each of your subs and it still ran fine.
Sub RunCode()
Call AddReferenceToProject
Call AddModuleToProject
Call AddProcedureToModule
End Sub
Try deleting the entire line of code and then insert a line and re-enter the code. It is possible that you have a non printable character in there somewhere. (After deleting, re-type the line; don't try to copy and paste it back after deleting because if any spurious characters are there then it will put them back again.)
Hello,
this forum is typically for end user questions and may include the odd answer to basic VBA topics. For more advanced VBA topics you may want to try the Excel Developer Forum forum, where the VBA specialists hang out.
Hello teylyn,
Thanks for your advice.
I'm a new in this web.
Best Regards,
Your code runs fine for me. I first tested by running each sub separately and then added the following sub to call each of your subs and it still ran fine.
Sub RunCode()
Call AddReferenceToProject
Call AddModuleToProject
Call AddProcedureToModule
End Sub
Try deleting the entire line of code and then insert a line and re-enter the code. It is possible that you have a non printable character in there somewhere. (After deleting, re-type the line; don't try to copy and paste it back after deleting because if any spurious characters are there then it will put them back again.)
Hello OssieMac,
I did as you suggested, but it still getting the same Error. It is become run when I used Call method.
However, I recognized the way how to avoid this again.
So, when I Run macro by button F5 - No errors, macro finished its job well.
Than I tried run step by step by clicking F8 - the Error appeared again after execution the same line.
I can't understand why stepping way to run procedure is generating error.
Anyway it is not a harm to me procedure if I use F5 or intiate by Call method or any other active-x control.
Thanks for you help!
Best regards,
Valery