+ Reply to Thread
Results 1 to 5 of 5

Removing Code from ThisWorkbook and error msg

  1. #1
    Registered User
    Join Date
    01-13-2016
    Location
    Atlanta, GA
    MS-Off Ver
    2013
    Posts
    5

    Removing Code from ThisWorkbook and error msg

    Hey everyone. I just joined to post this question.

    I have a line of code in my routine to delete some lines from ThisWorkbook as follows:

    ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule.DeleteLines 8,4

    When I run the macro, I get an error message:
    Run time error '1004':
    Programmatic access to Visual Basic Project is not trusted.
    It won't give me the option to Continue, only to End or Debug. Is there any way around this error? Setting DisplayAlerts to false doesn't do it, I'm sure because it's a system thing.

    Do I have any options here?

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Removing Code from ThisWorkbook and error msg

    Sounds like a Security setting issue. Did you check your security level?

    FYI.

    https://www.ablebits.com/office-addi...-macros-excel/
    Last edited by JOHN H. DAVIS; 01-13-2016 at 03:49 PM.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Removing Code from ThisWorkbook and error msg

    Hi karacooks

    Welcome to the Forum!!!

    From Chip Pearson
    http://www.cpearson.com/excel/vbe.aspx

    Introduction

    You can write code in VBA that reads or modifies other VBA projects, modules, or procedures. This is called extensibility because extends the editor -- you can use VBA code to create new VBA code. You can use these features to write custom procedures that create, change, or delete VBA modules and code procedures.

    In order to use the code on this page in your projects, you must change two settings.
    •First, you need to set an reference to the VBA Extensibility library. The library contains the definitions of the objects that make up the VBProject. In the VBA editor, go the the Tools menu and choose References. In that dialog, scroll down to and check the entry for Microsoft Visual Basic For Applications Extensibility 5.3. If you do not set this reference, you will receive a User-defined type not defined compiler error.


    •Next, you need to enable programmatic access to the VBA Project. In Excel 2003 and earlier, go the Tools menu (in Excel, not in the VBA editor), choose Macros and then the Security item. In that dialog, click on the Trusted Publishers tab and check the Trust access to the Visual Basic Project setting.

    In Excel 2007, click the Developer item on the main Ribbon and then click the Macro Security item in the Code panel. In that dialog, choose Macro Settings and check the Trust access to the VBA project object model.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  4. #4
    Registered User
    Join Date
    01-13-2016
    Location
    Atlanta, GA
    MS-Off Ver
    2013
    Posts
    5

    Re: Removing Code from ThisWorkbook and error msg

    OH gosh thank you both for super quick responses.

    I went and made both those changes and everything works fine. My concern now is that if I hand this worksheet over to an end user, they're probably going to have the same problem, aren't they? And asking my end users to go in and change security settings and so forth is a no-go.

    I might be back to square one, but you did solve the problem for my own use anyway. Thank you!!

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Removing Code from ThisWorkbook and error msg

    Hi karacooks

    You're welcome...glad I could help. Thanks for the Rep.

    You can perform Step 1 (reference to the VBA Extensibility library) with Code...it get's a bit messy but can be done.

    As far as I'm aware, there is no way to do Step 2 (enable programmatic access to the VBA Project) with Code...has to be done manually...but only once.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. removing Workbook_Open Code causes Run-Time 1004 error
    By BEEJAY in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-06-2012, 09:53 AM
  2. Removing error code #VALUE! after using FIND
    By djchapple in forum Excel General
    Replies: 2
    Last Post: 09-04-2011, 10:48 AM
  3. code from 'sheet1' to 'ThisWorkbook'
    By losmi8 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-11-2010, 03:03 AM
  4. Stop Code for ThisWorkbook
    By stevewilde in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-05-2009, 09:37 AM
  5. Code in 'ThisWorkbook' module in add-in?
    By karan in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-04-2009, 03:36 AM
  6. Using ThisWorkbook for code efficiency
    By Wyvern in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2007, 11:01 PM
  7. Remove ThisWorkbook code via VBA
    By PCLIVE in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2005, 05:05 PM
  8. [SOLVED] Deleting Code in 'ThisWorkbook'
    By Ken Loomis in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-25-2005, 10:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1