Results 1 to 10 of 30

Thread: Class Stuff: VBA Custom Classes & Objects, Class Modules

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #26
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,389
    Rep Power
    10
    https://www.excelfox.com/forum/showt...ll=1#post24194
    https://www.excelfox.com/forum/showt...age3#post24194



    Solution without Class module using a worksheet object code module

    This post describes what to do. Justification is given a couple of posts back

    We need to
    _(i) Put the coding (which is given by either the text from the class module , or the coding from the perceptions suggestion ), in any worksheet code module
    _(ii) Remove the two line instanciation coding.
    ( _(iii) We still need to assign the WithEvents of the Excel application object variable to the current running Excel )


    Lets do the last two (ii) and (iii), first
    We had this next codi9ng below, in the ThisWorkbook code module, where we conveniently had the instanciation added to the available Workbook_Open() coding, to get, in effect, perceptionally speaking, the VBA source code which we don’t see


    Object module, MeWatcher
    Code:
    Dim MeWatcher As FileOpenWatcher ' This must go here, not in the coding below, or else the variable will not exist after the coding below ends
    Private Sub Workbook_Open()      ' This is done when this file opens
     Set MeWatcher = New FileOpenWatcher    '   Make the object  MeWatcher  which is something like a  running Excel
     Set MeWatcher.ExApp = Excel.Application '  I think this makes the object,  MeWatcher,  the actual running Excel
    End Sub
    
    ( The object variable, MeWatcher, needs to be outside the routine, so that it does not "vanish" when the routine ends )


    We are proposing/ suggesting that we don’t need the two instantiation lines. So that reduces to
    Code:
    Private Sub Workbook_Open()           '  This is done when this file opens
     Set SheetX.ExApp = Excel.Application '  I think this makes the object,  ExApp,  the actual running Excel
    End Sub
    ( The SheetX. will become clear as we go on now to tackle the …._(i) Put the coding (which is given by either the text from the class module , or the coding from the perceptions suggestion ), in any worksheet code module ….

    For (i) it is very simple. We are assuming that the a wiorksheet code module is of a similar source/ origin etc. as our perceived instanciated thing holding VBA source code which we don’t see , ( which is identical to the "stamp" of text of the class module ). The only difference is that it does not have the name of the instanciated object variable, MeWatcher, but rather a reference to / name of a worksheet
    So what we have seen now many times as VBA source coding, or the text from the class "stamp", is all we need , this -
    Code:
    Public WithEvents ExApp As Excel.Application ' This makes ExApp a variable/ object that some how is like a running Excel, and also the   WithEvents  means it has access to all those  codings that it has that kick off when something happens
    
    ' This and the next macro is the text of coding we need to tell us if we open the file "test.csv"
    Public Sub ExApp_WorkbookOpen(ByVal Wb As Workbook) ' This will become a property in a final Instanciated object, and I intend to use it
    Dim s As String
     Let s = Wb.Name
        If s = "test.csv" Then Call MyMacro
    End Sub
    Sub MyMacro() ' This is available also in intellisense to a final instanciated object, although in this example I will not use it, - its calls from the routine above. Splitting the two is not done for any particular reason
    MsgBox Prompt:="You just opened  test.csv"
    End Sub
    



    So finally, for the solution using a worksheets code module , we have coding in the ThisWorkbook module, and a worksheets code module


    Object module, ThisWorkbook
    Code:
    Option Explicit
    Private Sub Workbook_Open()           '  This is done when this file opens
    ' Set Sheet1.ExApp = Excel.Application '  I think this makes the object,  ExApp,  the actual running Excel
    'or
     Set Worksheets("Sheet1").ExApp = Excel.Application
    End Sub
    






    Object module, Sheet1
    Code:
    Option Explicit
    Public WithEvents ExApp As Excel.Application ' This makes ExApp a variable/ object that some how is like a running Excel, and also the   WithEvents  means it has access to all those  codings that it has that kick off when something happens
    
    ' This and the next macro is the text of coding we need to tell us if we open the file "test.csv"
    Public Sub ExApp_WorkbookOpen(ByVal Wb As Workbook) ' This will become a property in a final Instanciated object, and I intend to use it
    Dim s As String
     Let s = Wb.Name
        If s = "test.csv" Then Call MyMacro
    End Sub
    Sub MyMacro() 
    MsgBox Prompt:="You just opened  test.csv"
    End Sub
    

    Attached Files Attached Files
    Last edited by DocAElstein; 06-19-2024 at 10:51 PM.

Similar Threads

  1. PQ - IP C class generator
    By sandy666 in forum ETL PQ Tips and Tricks
    Replies: 0
    Last Post: 10-22-2020, 05:16 AM
  2. Backup all modules, class modules and userforms to a selectable folder
    By MrBlackd in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 04-06-2014, 08:33 AM
  3. Manipulate VBA Array Object Using Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 06-06-2013, 07:53 PM
  4. Array Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 3
    Last Post: 12-20-2012, 11:22 AM
  5. Class Objects Created Using the CreateObject Method That Employs Late Binding
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 08-16-2011, 12:38 PM

Posting Permissions

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