Excel automation (inserting macros programmatically)

Posted on 2006-04-12
Last Modified: 2010-04-07

I wrote an application a while ago that creates reports in excel programatically using the Excel Object Library. At the time of development, I had Windows 2000 with Office 2000. I tested this on a machine with Windows XP and Office 2000 and it worked.

Now I have a machine with Windows XP and Office 2003. The application now generates an error: "Error 1004: Programmatic access to Visual Basic is not trusted". This, of course, happens on the line that tries to add a VBA macro as follows:

   Dim str As String
   Dim eCM As VBIDE.CodeModule
   Dim eVBC As VBIDE.VBComponent
   Set eVBC = eWB.VBProject.VBComponents.Add(vbext_ct_StdModule) ' Error here
   eVBC.Name = "mdlAudioBee"
   str = "Some Macro Text"
   eVBC.CodeModule.InsertLines 1, str

I have already set the lowest possible value for Excel > Tools > Macros > Security > Low and have also disabled my AntiVirus program (Norton).

What else do I need to do to allow the above operation?


Question by:raheelasadkhan
    LVL 26

    Accepted Solution


    Author Comment

    Worked nicely. Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now