Macro stop run with Errror msg "Can't execute code in break mode"

Anonymous
2016-11-19T17:03:35+00:00

My code stopped after execution of line marked "yellow" with error msg:

See full code below:

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.

Microsoft 365 and Office | Excel | For home | Windows

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.

0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Anonymous
    2016-11-19T21:27:08+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments
  2. OssieMac 47,981 Reputation points Volunteer Moderator
    2016-11-19T22:37:05+00:00

    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.)

    0 comments No comments
  3. Anonymous
    2016-11-20T09:08:41+00:00

    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,

    0 comments No comments
  4. Anonymous
    2016-11-20T11:14:34+00:00

    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

    0 comments No comments