locked
When trying to load a macro dynamically, encountering a 50289 project is protected error even though my project is NOT protected RRS feed

  • Question

  • I'm writing an application in C++ that dynamically creates VBA macros for me.  What I need to do now is write a simple macro in VBA that dynamically loads the macro that is being written and updated by my C++ app inside of MS Word 2007.

    This article (http://support.microsoft.com/kb/219905/en-us) describes exactly how to do that for excel, and I've managed to successfully do this on excel.  When I try to create a similar macro for Microsoft Word, however, I run into this: run-time error '50289' Can't perform operation since the project is protected.

    I've made sure to follow every step in this article (http://support.microsoft.com/kb/282830/en-us), describing how to instruct your office application to trust access to the VBA project object model.  This seemed to work in Excel, but not in Word.

    I've also made sure to right-click the "Normal" project in VB and verify under the "Normal - Project Properties" -> "Protection" tab that the project is not protected.

    Here are my system specs:

    • I'm running Windows 7 Home Premium  (64-bit) with Service Pack 1.
    • Visual Basic 6.5 version 1054
    • Microsoft Word / Excel / Office 2007

    This macro, modified from the above article and written for Excel, works correctly on my setup: 

    Sub Macro1()
          Dim oXL As Excel.Application
          Dim oBook As Excel.Workbook
          Dim oSheet As Excel.Worksheet
          
        ' Create a new instance of Excel and make it visible.
          Set oXL = CreateObject("Excel.Application")
          oXL.Visible = True
       
        ' Add a new workbook and set a reference to Sheet1.
          Set oBook = oXL.Workbooks.Add
          Set oSheet = oBook.Sheets(1)
          oSheet.Activate
                
        ' The Import method lets you add modules to VBA at
        ' run time. Change the file path to match the location
        ' of the text file you created in step 3.
          oXL.VBE.ActiveVBProject.VBComponents.Import "C:\VBATest2.bas"
       
        ' Now run the macro, passing oSheet as the first parameter
          oXL.Run "MacroTest"
                
        ' Turn instance of Excel over to end user and release
        ' any outstanding object references.
          oXL.UserControl = True
          Set oSheet = Nothing
          Set oBook = Nothing
          Set oXL = Nothing
          
    End Sub

    This Word macro, modeled off the above macro written for excel, results in run-time error '50289' Can't perform operation since the project is protected:

    Sub CompareTesting2()
            
          Dim oXL As Word.Application
          Dim oBook As Word.Document
        ' Dim oSheet As Excel.Worksheet
          
        ' Create a new instance of Excel and make it visible.
          Set oXL = CreateObject("Word.Application")
          oXL.Visible = True
       
        ' Add a new workbook and set a reference to Sheet1.
          Set oBook = oXL.Documents.Add
        ' Set oSheet = oBook.Sheets(1)
          oBook.Activate
                
        ' The Import method lets you add modules to VBA at
        ' run time. Change the file path to match the location
        ' of the text file you created in step 3.
          oXL.VBE.ActiveVBProject.VBComponents.Import "C:\VBATest.bas"
       
        ' Now run the macro, passing oSheet as the first parameter
          oXL.Run "VBATest"
                
        ' Turn instance of Excel over to end user and release
        ' any outstanding object references.
        ' oXL.UserControl = True
        ' Set oSheet = Nothing
          Set oBook = Nothing
          Set oXL = Nothing
    End Sub

    After many hours of reading through forums with no success, any help would be very much appreciated!

    Sunday, October 20, 2013 4:40 PM