Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 24

Thread: Solved: How to autodelete a module

  1. #1
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location

    Solved: How to autodelete a module

    Is there any way to auto-delete a module? or an entire add-in?
    I have tried several methods shown in other forums but none of them seem to work properly, Im using Office 2003

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    What other methods did you try, and what happened?

  3. #3

  4. #4
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    [VBA]
    Option Explicit

    Private Sub Workbook_Open()
    Dim ThisModule As Object

    On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromGuid _
    "{0002E157-0000-0000-C000-000000000046}", 5, 3

    MsgBox "This project has been registered using code in this module" & vbLf & _
    "(Demo: the registration code module will now be deleted)"

    Set ThisModule = Application.VBE.ActiveVBProject.VBComponents
    ThisModule.Remove VBComponent:=ThisModule.Item ("RunOnceModule")
    End Sub
    [/VBA]

    Is this the poper way?m is not working for me

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by makako
    [VBA]
    Option Explicit

    Private Sub Workbook_Open()
    Dim ThisModule As Object

    On Error Resume Next
    ThisWorkbook.VBProject.References.AddFromGuid _
    "{0002E157-0000-0000-C000-000000000046}", 5, 3

    MsgBox "This project has been registered using code in this module" & vbLf & _
    "(Demo: the registration code module will now be deleted)"

    Set ThisModule = Application.VBE.ActiveVBProject.VBComponents
    ThisModule.Remove VBComponent:=ThisModule.Item ("RunOnceModule")
    End Sub
    [/VBA]

    Is this the poper way?m is not working for me
    [vba]Item ("RunOnceModule")[/vba] must contain the name of the module between the quotation marks. Is the module you're trying to delete named 'RunOnceModule'?
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    Yes, here's the zip file

    Pd: will the workbook_open sentence will also delete or should it insert the delete argument between "on error resume next" etc?

  7. #7
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Well, I opened your zip file, got a message box saying "Hi" and there was no code modules, so it must have worked (Office 2k) And if I had saved there would be a workbook minus a module saved.

    In Excel 2002 or above the "Trust Access To Visual Basic Project" box must be selected in Tools|Options|Security|Macro Security or the macro won't run, has this been done?

    If you also want to delete all code referring to this from the Workbook_Open event I suggest you have a read of this article to see how to go about it
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Same here, messagebox saying Hi but no modules only the workbook open code is left.
    2003
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    I can still see it. the RunOnceModule, I skipped the on error resume next sentence and
    [VBA]
    ThisWorkbook.VBProject.References.AddFromGuid _
    "{0002E157-0000-0000-C000-000000000046}", 5, 3
    [/VBA]
    shows an error 1004, is there anything concerning my regional config?
    and yes, the Trust Access To Visual Basic Project is selected, i get the Hi message

    [VBA]
    Sub SelfDeleteProcedure()
    Dim FirstLine As Long, NumberOfLines As Long, N As Long

    On Error Resume Next
    With ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
    FirstLine = .ProcStartLine("SelfDeleteProcedure ", vbext_pk_Proc)
    NumberOfLines = .ProcCountLines("SelfDeleteProcedure ", vbext_pk_Proc)

    .DeleteLines FirstLine, NumberOfLines
    End With
    End Sub
    [/VBA]

    I also tried this code and added the mentioned reference to Visual Basic 5.3 stuff (Great article by the way Johnske) but it is still not working.
    Last edited by makako; 08-03-2006 at 07:21 AM.

  10. #10
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    The On Error statement is required for the case where the reference already exists...

    Apart from that, if you've checked "Trust Access" and your VB project is not locked for viewing... sorry, can't help you. It should be working OK but someone else may have some ideas.


    EDIT: In the last example you showed above... I just discovered a typo, both instances of ("SelfDeleteProcedure ", contain an unwanted space before the last quotation mark, delete those spaces and then try it again (it's usually something quite simple like that) - sorry
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  11. #11
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    I edited the space after SelfDeletePrecedure but still not working. Can anybody send me a functional archive or anwer me if my regional setting may affect the result? thanks

  12. #12
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    maybe this code I've picked up somehere will help ? It runs once then self destroys.

    Sub DeleteThisModule()
    Dim vbCom As Object

    MsgBox "Hi, I will delete myself "

    Set vbCom = Application.VBE.ActiveVBProject.VBComponents

    vbCom.Remove VBComponent:= _
    vbCom.Item("Module1")

    End Sub

  13. #13
    VBAX Contributor
    Joined
    Aug 2006
    Posts
    120
    Location
    No, it shows an error (1004). Is there any other reference i can use or am i missing?

  14. #14
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    It works for me.....do you have a Module1
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  15. #15
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    hi lucas,

    can you post an example please.

    thanks

  16. #16
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    attached
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #17
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    Quote Originally Posted by makako
    No, it shows an error (1004). Is there any other reference i can use or am i missing?

    thanks lucas,

    i got the same error message, but found the following solution

    To turn on trusted access to Visual Basic Projects:
    1. On the Tools menu, point to Macro, and then click Security.
    2. On the Trusted Sources tab, select the Trust access to Visual Basic Project check box
    works ok for me now with the above solution.

  18. #18
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That was discussed in post 7 of this thread. I thought you would understand from previous posts in this thread that the trust access to vb project must be checked in order to make this work. Sorry for the omission.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  19. #19
    Hi Guys,

    You can switch this access on and off with code using the Wscript "Shell.Application" and using wsh.RegWrite to alter the key from 0 for off to 1 for on, for access the VB Object Module. I would effectively have the registry written to switch access on, followed by a application.quit to force the user to reboot Excel and to ensure access to the object model was set to on.

    The registry key can be found at:-

    HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel\Security\AccessVBOM


    Hope this adds some flavour to your thoughts about the subject?? :-)

  20. #20
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    Quote Originally Posted by lucas
    That was discussed in post 7 of this thread. I thought you would understand from previous posts in this thread that the trust access to vb project must be checked in order to make this work. Sorry for the omission.

    sorry must have missed it....

Posting Permissions

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