+ Reply to Thread
Results 1 to 5 of 5

Find if workbook_open exists

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-26-2005
    Posts
    108

    Find if workbook_open exists

    How can I (using VBA) detect whether or not a workbook_open macro exists in a workbook?

    I've "guessed" at ...

    ThisWorkbook.VBProject.VBComponents("Workbook_Open").Name
    ... but that doesn't seem to work (error 9 - subscript out of range).

    Can anyone help?
    Thanks in advance.
    Last edited by VBA Noob; 12-10-2007 at 09:07 AM.
    Tony

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon tonywig

    Something like this should do the trick :
    Sub test()
    If ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule _
     .Find("Private Sub Workbook_Open()", 1, 1, 100, 1000) Then
    MsgBox "Workbook module found"
    End If
    End Sub
    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Forum Contributor
    Join Date
    01-26-2005
    Posts
    108
    Worked an absolute treat.
    Thanks Dominic.

    Is there an "easy" way to delete it if it's found?

    Thanks again.
    T.
    Last edited by tonywig; 12-10-2007 at 09:09 AM.

  4. #4
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Hi tonywig

    Is there an "easy" way to delete it if it's found?
    There's two easy ways
    (1) Chip Pearson describes how to delete the module as an entity using the VBIDE object in his pages, but you have to set a refrence to the extensibilty library.

    (2) Using the code below will delete the lines one at a time without the need to set any references
    Sub test()
    On Error Resume Next
    Set codemod = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule
    NumLines = codemod.ProcCountLines("Workbook_Open", 0)
    For n = 1 To NumLines
    codemod.DeleteLines 1
    Next n
    End Sub
    HTH

    DominicB

  5. #5
    Forum Contributor
    Join Date
    01-26-2005
    Posts
    108
    Thankyou so much.
    Excellent.
    Knew it must be possible but was missing the technical knowledge to achieve it.
    Luckily for me, you have that a-plenty.
    Thanks again.

+ 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