+ Reply to Thread
Results 1 to 10 of 10

Remove all code and modules from the active workbook

  1. #1
    Registered User
    Join Date
    02-04-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    5

    Remove all code and modules from the active workbook

    Hello,

    I am trying to use vba to remove the modules behind the active workbook. I modified code I found from an existing source http://www.cpearson.com. (The full code had additional parts I didn't need so I removed any parts that had nothing to do with stripping the VBA)

    Please Login or Register  to view this content.


    When stepping through the code, it reaches the following line and I get an error message that says "Object Variable or With block variable not set.":

    Please Login or Register  to view this content.
    Thank you in advance for your help.

    Best,
    Matt

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Remove all code and modules from the active workbook

    Hello Matt,

    Give this a try.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Remove all code and modules from the active workbook

    In Excel 2007, you need only save the workbook as a *.xlsx document, since no macros are allowed in that format they will be removed for you.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    02-04-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    5

    Re: Remove all code and modules from the active workbook

    I would prefer the programatic approach. Leith, your code works if the project isn't protected. To give you some context. My spreadsheet is a form with a submit button. When the user clicks the submit button, it sends an email to an address through the user's Microsoft Outlook with a time stamped copy of the excel form saved as an attachment. Since the original form is protected, so is the saved copy. The reason I want to remove the code in the saved copy is so that the submit button in the attachment becomes dead. In otherwords, they wont be able to execute the Submit button from the copy.

    Long story short, is there was way to modify what you have sent so that it can first unprotect the vba project to be deleted? I am getting a Run-time error message '50289' that says "Can't perform operation since the project is protected".

    My sincere apologies if this is not clear.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Remove all code and modules from the active workbook

    Hello dasvas,

    It would help me to answer your question if you posted the macro code.

  6. #6
    Registered User
    Join Date
    02-04-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    5

    Re: Remove all code and modules from the active workbook

    Below is the code for the main macro. When it opens up a copy of the new excel under a time stamped name. When that workbook is active, I want to remove the macros right before I send it in the email. I execute the code you supplied at that point. The problem that I stated before is that I protect the code in the original workbook so the copied workbook prevents me from deleting the code due to that protection.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Remove all code and modules from the active workbook

    Why not just save a copy of the workbook as an xlsx?
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Remove all code and modules from the active workbook

    Hello dasvas,

    I modified the macro to unlock the VBA Project. You will need to supply the password. It is marked in bold.
    Please Login or Register  to view this content.
    .
    Last edited by Leith Ross; 02-07-2011 at 01:37 PM. Reason: Changed = to <> for VBproj.Protection

  9. #9
    Registered User
    Join Date
    02-04-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    5

    Re: Remove all code and modules from the active workbook

    It is still giving me the same Run time '50289' error. I am prompted for the password as if the send key function cannot get to the part where it passes the password I supplied. I have attached a simplified version of my excel workbook that replicates my problem. It opens up the time stamped excel and tries to remove the module but it gets hung up on the password protection part. You can execute the code using the button in the main sheet of the workbook, just as I would with my own excel form.

    I hope this attachment makes it clear what i am trying to do. The password for everything in this book is "pass123".
    Attached Files Attached Files

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Remove all code and modules from the active workbook

    Hello dasvas,

    Sorry for the delay. This took some heavy coding to make it work. I crashed my system a lot yesterday working on this. Good news it is working and stable. It is necessary to use the Windows API to to do a variety of tasks that can not be done in VBA or are not reliable using VBA code, like SendKeys.

    There are 3 separate VBA modules needed to make this work. There is the API_SendKeys to replace the VBA SendKeys. The Timer_API which allows the code to detect the password dialog and send the password to it, and finally the main module to open the workbook, and call the other macros to unlock the project.

    Since all systems are different, you may need to adjust the delay on the Timer_API macro. For my system 200 milliseconds works well. If you have problems unlocking the project, try increasing the delay. Unfortunately, there is will always be a quick flash of the password dialog. It is just the nature of the operating system's design. The password is conveniently hidden in the Timer_API code so as not to be immediately obvious.

    Due to the character limit for a posting, I am attaching an example workbook. Here is the code for the main module which includes the file path and name of the workbook with a protected VBA project. Change these to what you are using.

    Main Module Code
    Please Login or Register  to view this content.

    I have marked the password in bold in the Timer_API procedure below.
    Password Location
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Leith Ross; 02-09-2011 at 11:28 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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