Results 1 to 7 of 7

Custom Workbook Events

Threaded View

mc84excel Custom Workbook Events 08-13-2019, 10:53 PM
MarvinP Re: Custom Workbook Events 08-13-2019, 11:06 PM
mc84excel Re: Custom Workbook Events 08-14-2019, 10:00 PM
MarvinP Re: Custom Workbook Events 08-15-2019, 01:06 AM
Kyle123 Re: Custom Workbook Events 08-15-2019, 01:14 AM
mc84excel Re: Custom Workbook Events 08-23-2019, 12:11 AM
Audamus Re: Custom Workbook Events 06-22-2021, 12:04 AM
  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Custom Workbook Events

    Goal = I'm trying to create reusable code to handle custom events at a workbook level.

    Ideally I would like to make this so that I can add it to existing Excel macro projects/tools (be it XLSM or XLAM) and only minimal changes are required to make the code work with that project.

    (Optional background info - I have several projects that use custom events. Most of these created by me but their custom events were unique to each project. I am now trying to consolidate their code to create a reusable/template class or module that they can each use.)


    At this stage, I am using three objects:
    1. ThisWorkbook
    2. A module to contain all the custom events called from Item 1
    3. A module to contain the functions & global variables that are used by Item 2 but their values are unique to that project

    The point of keeping Item 2 separate to Item 1 is to reduce clutter in ThisWorkbook class. Also to store all the subprocedures/functions that the custom events require.
    The point of keeping Item 3 separate to Item 2 is that whenever I update Item 2, I can copy/paste it to any other project already using this code without needing to make additional edits.

    So starting with Item 1 (i.e. the code inside ThisWorkbook class). Below is what I am currently using: (I will show/look at Items 2 & 3 after Item 1 is sorted out)


    '/ Last updated 20190819 1343
    '/  20190814 1438
    
    Option Explicit
    
    Private mblnThisAddInJustInstalled As Boolean
    
    ''/ Comment out if not using Custom Ribbon
    'Private pRibbonUI As IRibbonUI
    '
    'Public Property Let ribbonUI(ByRef iRib As IRibbonUI)
    ''/ Set RibbonUI to property for later use
    ''/ Comment out if not using Custom Ribbon
    '    Set pRibbonUI = iRib
    'End Property
    '
    'Public Property Get ribbonUI() As IRibbonUI
    ''/ Retrieve RibbonUI from property for use
    ''/ Comment out if not using Custom Ribbon
    '    Set ribbonUI = pRibbonUI
    'End Property
    
    Private Sub Workbook_AddinInstall()
    '/ This event followed by Workbook_Open
        mblnThisAddInJustInstalled = True
    End Sub
    
    Private Sub Workbook_Open()
        Call CustomEvent_TWB_Open(mblnThisAddInJustInstalled)
    End Sub
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        ' cancel the inbuilt save
        Cancel = True
        ' replace with custom save
        Call CustomEvent_TWB_BeforeSave(SaveAsUI)
    End Sub
    
    Private Sub Workbook_AddinUninstall()
    '/ This event followed by Workbook_BeforeClose
        mblnThisAddInJustInstalled = False
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim blnRequireCustomSave As Boolean
    
        If ThisWorkbook.Saved Then
            GoTo ExitProcedure
        End If
    
        If Not ThisWorkbook.IsAddin Then
            ' if using 'Force Enable Macros' code in XLSM do custom SaveAndClose
            If LenB(gstrcWS_ENABLE_MACROS) Then
                blnRequireCustomSave = True
            End If
        Else
            ' if closing XLAM, remind developer to save
            If LenB(gstrcDEV_MATCH_NAME) Then
                blnRequireCustomSave = InStr(1, Application.UserName, gstrcDEV_MATCH_NAME, vbTextCompare)
            End If
        End If
    
        If blnRequireCustomSave Then
            Do
                ' custom Save&Close event
                Select Case MsgBox("Do you want to save the changes you made to '" & ThisWorkbook.Name & "'?", vbQuestion + vbYesNoCancel)
                Case vbYes
                    Call CustomEvent_TWB_BeforeSave
                Case vbNo
                    ThisWorkbook.Saved = True
                Case vbCancel
                    ' user chose Cancel so the workbook close is also cancelled
                    Cancel = True
                    Exit Sub
                End Select
            Loop Until ThisWorkbook.Saved = True
        End If
    
    ExitProcedure:
        Call CustomEvent_TWB_BeforeClose
    End Sub

    1. COMMENT: This code is not 100% reusable as 1 variable and 2 properties need to be commented out if the project isn't using custom ribbon code. (Most of my projects already are. I see no need to change this code - it already notes the need to comment out that part of the code if that particular project is not using)
    2. COMMENT: The code is calling procedures that are not shown e.g. CustomEvent_TWB_BeforeSave. These are kept in Item 2. I will show these once Item 1 concepts are sorted out.
    3. COMMENT: Workbook_BeforeClose - The code references global constants that are not shown. These are kept in Item 3. I will show these once Item 1 concepts are sorted out.
    4. QUESTION: Workbook_BeforeClose - I have doubts as the Custom Save event code I have inside this function. Shouldn't the Workbook_BeforeSave trigger automatically whenever Workbook_BeforeClose is triggered and the XLSM/XLAM has property
      ThisWorkbook.Saved = False
      ?
    5. QUESTION: Workbook_AddinUninstall - Is there any point setting this variable back to false? Would it not be impossible to fire Workbook_Open event for that add-in after it has been uninstalled UNLESS the user reinstalls the add-in in the same Excel session? (In which case the Workbook_AddInInstall would set the variable back to True anyway)
    6. QUESTION: Is there anything you have noticed that you think should be changed/added?
    Last edited by mc84excel; 08-23-2019 at 12:16 AM. Reason: update code to latest version
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Workbook events and Worksheets events
    By mp3909 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-21-2018, 08:31 AM
  2. [SOLVED] Hearing custom events
    By smpita in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-05-2018, 05:35 PM
  3. [SOLVED] Custom Object ?Resursive? Events
    By mikerickson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-27-2015, 08:03 PM
  4. Custom Object ?Resursive? Events
    By mikerickson in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2015, 05:33 PM
  5. Q: OLEObject in custom class, events not firing
    By miguel3d in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-03-2015, 08:45 PM
  6. custom events
    By SuitedAces in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-18-2007, 08:46 AM
  7. [SOLVED] Shapes and Events and Custom Menue
    By Tom G. in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2005, 03:06 PM

Tags for this Thread

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