Problem to call function

Hi,

Once I call this function
Public Function CreateButton(xDate As Date)
    
    ''UnhideSheets
    Dim NName As String
    NName = "btn1"

    Dim myCmdObj As OLEObject
    Set myCmdObj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=470, Top:=5, Width:=60, Height:=25)
     ' Define buttons name
    myCmdObj.Name = NName
     ' Define buttons caption
    myCmdObj.Object.Caption = "refresh"
    myCmdObj.PrintObject = False
    
     ' Inserts code for the button
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub " & NName & "_Click()"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, vbTab & "refresh"
        .InsertLines N + 4, vbNewLine
        .InsertLines N + 5, "End Sub"
    End With
    
    Dim myCmdObj1 As OLEObject
    NName = "btnNextMonth"
    Set myCmdObj1 = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=560, Top:=5, Width:=60, Height:=25)
    myCmdObj1.Name = NName
    myCmdObj1.Object.Caption = "Next Month"
    myCmdObj1.PrintObject = False
    
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub " & NName & "_Click()"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, vbTab & "goNextMonth"
        .InsertLines N + 4, vbNewLine
        .InsertLines N + 5, "End Sub"
    End With
    
    Dim myCmdObj2 As OLEObject
    NName = "btnPrevMonth"
    Set myCmdObj2 = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=620, Top:=5, Width:=60, Height:=25)
    myCmdObj2.Name = NName
    myCmdObj2.Object.Caption = "Prev Month"
    myCmdObj2.PrintObject = False
    
    With ThisWorkbook.VBProject.VBComponents(ActiveSheet.CodeName).CodeModule
        N = .CountOfLines
        .InsertLines N + 1, "Private Sub " & NName & "_Click()"
        .InsertLines N + 2, vbNewLine
        .InsertLines N + 3, vbTab & "goPrevMonth"
        .InsertLines N + 4, vbNewLine
        .InsertLines N + 5, "End Sub"
    End With
        
End Function

Open in new window

within Excel 2003 VBA codes, I get this error

tt303.jpg
why?
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Instead of trying to create Subs in the workbook you could create and distribute a template workbook  (.xlt) that already contains the subs.
HuaMin ChenProblem resolverAuthor Commented:
Can I have more details to your way? But I'm using Excel 2003, while the codes do work, and only such error happens.
Martin LissOlder than dirtCommented:
I ran your code in Excel 2010 and created the attached Excel 2003 template file. By the way your CreateButton sub requires that it be passed a date but that date is never used.
28714201.xlt
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

HuaMin ChenProblem resolverAuthor Commented:
Thanks. I see your Excel file.
But why did the error come out, due to the same codes, within my Excel workbook?
Martin LissOlder than dirtCommented:
I'm sorry but I don't have Excel 2003.
byundtMechanical EngineerCommented:
When I run the code in Excel 2003, there is a runtime error on this statement, with an error message that the operation is not permitted.
Set myCmdObj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Left:=470, Top:=5, Width:=60, Height:=25)

Open in new window


I don't know whether I have a faulty Excel 2003 install, am missing a reference, or if this is a problem repeatable elsewhere.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
In Excel 2003, record a macro when adding a button. That recored macro should show you how to add one using your VBA code.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Applications

From novice to tech pro — start learning today.