Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Call a sub from a class module

    How can I run a sub, located in class module, from a "normal" module? Is this even technically possible? I keep getting compile errors...

    I have tried moving the sub to the "normal" module and then calling it from the class module. This also won't compile.

    The formulations I have tried are: call modulename.subname and also application.run modulename.subname. (The latter syntax works between "normal" modules.)

    I'm grateful for any ideas.

    Maybe this should be in the VBA forum...
    Stylus

  2. #2
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    233
    Thanks
    0
    Thanked 22 Times in 21 Posts
    Quote Originally Posted by Stylus View Post
    How can I run a sub, located in class module, from a "normal" module? Is this even technically possible? I keep getting compile errors...

    I have tried moving the sub to the "normal" module and then calling it from the class module. This also won't compile.

    The formulations I have tried are: call modulename.subname and also application.run modulename.subname. (The latter syntax works between "normal" modules.)

    I'm grateful for any ideas.

    Maybe this should be in the VBA forum...
    I'm not sure on the "possible" side of this question...but why would you put a sub in a Class Module? Isn't the idea of a class module that it contains methods and properties of a particular class, so in order to invoke one of those methods you first have to instantiate the class as in set myobj = new classname, then you can do things like myobj.methodname??

  3. #3
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    John

    Thanks for your reply. Let me give some details so you will understand what I'm doing.

    I need certain functions performed when a new or existing doc is opened, even if Word is already running. I found an example of an event handler that responds to doc openings while word is running. This structure uses an autoexec that creates the class "EventHandler" when Word is started, and the eventhandler then contains the subs "NewDocument" and "DocumentOpen" which do the work.

    One of the functions is to copy Word styles from the Global template to whatever document is opened. (Everybody tells me to have the styles in each WorkGroup template. I DID that once and spent a huge amount of time maintaining these styles - in 30 templates... The Global template as a 'Single Source' saves me a lot of grief!)

    I have had the 'copy styles' sub inside the class module, and this has worked well. Now I want to give the user the option of manually triggering the copy-styles function. I also want to avoid having two copies of this 'copy styles' sub in the global template: simpler debugging and maintenance.

    I haven't been able access the 'copy styles' in the class module from any other module, that is, link this sub to any ribbon button. If I move the sub to a normal module that I can connect to the ribbon, then I can't get the class to see it.

    So: is this a technical impossibility, or is it a question of a syntax or structure that I need to use? If all else fails, then I will go ahead and use the messy 'two-sub' solution.

    I appreciate your time and any suggestions you have.
    Stylus

  4. #4
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Got it to work, though in a puzzling way:

    The sub I was calling from a sub in the class module was "Private". When I made private sub into a normal sub, the class module was able to call it.

    The thing is, while the sub was "Private" it was being called by a normal module without any problems.

    Strange.

    Edit to make this more understandable:

    In a template I have a module "A" containing sub "Aa", and a module "B" containing sub "Bb", and finally a class module "C" containing sub "Cc".

    Aa can call Bb, regardless of whether Bb is "Private" or not. Cc can not call Bb if Bb is "private".
    Last edited by Stylus; 2012-10-12 at 07:35. Reason: Clarity
    Stylus

Posting Permissions

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