OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 26-02-2005, 19:32
zlatev
Guest
 
Posts: n/a
Default Create an object from class defined in another GMS

I looked through the documentation about GMSManager.RunMacro, which I suposed that may do the job.

However I can not think out at the moment, how can I create an object from a specified class, if the function parameter is string with class name.
I tried some "shots in the dark" by using CreateObject and GetObject functions, but as my class is not ActiveX - they did not suceeded.

I want this to be as much generic, as possible. So if there is way not to use hardcoded constructor calls in the code, I would like to know that way.

Kind Regards,
Zlatin
Reply With Quote
  #2  
Old 27-02-2005, 11:31
zlatev
Guest
 
Posts: n/a
Default Update: 27 Feb 2005

I also tried using MSScriptControl.ScriptControl ActiveX control, Eval method.

Another thing I tried is adding a referrence to the GMS that contains the class module. However I cannot instantiate Private classes outside the GMS. When I change the Instancing property of Class module to PublicNotCreatable I can define a variable of type of the class in question, but I cannot actually instantiate the class.
Reply With Quote
  #3  
Old 27-02-2005, 11:56
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default Re: Create an object from class defined in another GMS

Zlatin,

I suppose you will ensure that the two GMS modules are installed at the same time because you are trying to use some specific information (class) provided by the other GMS. In this case, the best bet is to add a reference from one GMS to the other.

Here is an example. I created two new GMS modules and called them Project1.gms and Project2.gms. Then I started CorelDRAW and renamed the project names from GlobalMacros to Project1 and Project2 respectively.

I added the following class module to Project2 and named it clsMyModule:

Code:
Option Explicit

Public strName As String
Public nAge As Integer

Public Sub ShowMessage()
    MsgBox "Name = " & strName & vbCr & "Age = " & nAge
End Sub
Make sure you set the Instancing of this class to "PublicNotCreatable" (see the attachment #3).

Since VBA doesn't allow to create public creatable classes (unlike VB), you need to provide a function that can create an instance of this class. So, just add a module to Project2 and paste the following code in it:

Code:
Option Explicit

Public Function CreateMyClass() As clsMyClass
    Set CreateMyClass = New clsMyClass
End Function
Now in Project1 I added a new module (Module1). Now go to Tools>References while Project1 is selected in Project Explorer window and find "Project2" in the list of projects. Add a checkmark to it (see the attached image 1 for illustration). Now you should see a reference in Project1 pointing to Project2 (see the second attached image).

Code:
Option Explicit

Sub Test()
    Dim c As Project2.clsMyClass
    Set c = Project2.CreateMyClass
    c.strName = "Some Name"
    c.nAge = 20
    c.ShowMessage
End Sub
Now you can run the Test macro and it should show a message box.
Attached Images
   
Reply With Quote
  #4  
Old 27-02-2005, 11:59
Alex's Avatar
Alex Alex is offline
Administrator
 
Join Date: Nov 2002
Posts: 1,940
Blog Entries: 4
Default Re: Create an object from class defined in another GMS

I see that you just almost answered your own question while I was writing my reply.

However after some further testing I discovered that CorelDRAW doesn't shutdown properly if GMS cross-reference each other. Which is apparently a bug and should be looked at by Corel.

Maybe you should create a real COM object using VB or C++ and then just instantiate it in your macro?
Reply With Quote
  #5  
Old 27-02-2005, 12:03
zlatev
Guest
 
Posts: n/a
Default

Ok, thank you, will try it right now.

Btw. I tried creating a function that will create a private class instance and return reference to it as an Object.
However as soon as I try to call that function through GMSManager.RunMacro it fails on the moment that the reference should be passed accross boundaries of GMS module.
And after it fails the Application Recovery Manager Wizard pops-up.

I add this info, just in case someone else tries to do such things.

Edit: Nope I do not like to create COM object. Also I am sure there would be no cross references between GMS modules. So there will be only one common GMS module that is referenced by other GMS modules.
Reply With Quote
  #6  
Old 27-02-2005, 12:51
zlatev
Guest
 
Posts: n/a
Default

Thanks Alex, works like charm.
I added a module named Constructors to my "common" GMS, that would contain all such functions for creating class instances.

So I create a constructor function like
Code:
Public Function ClassName() As ClassName
    Set ClassName = New ClassName
End Function
And use it in another GMS module like
Code:
   Dim oClassName As CommonGMSName.ClassName
   Set oClassName = CommonGMSName.Constructors.ClassName()
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Palette (or Colors array) property in VBA class in CDR12 zlatev CorelDRAW/Corel DESIGNER VBA 7 22-02-2005 12:28
Can I create CustomShape with VBA for CD? Can I create tool? Jab CorelDRAW/Corel DESIGNER VBA 0 01-02-2005 06:02
Where does COrelPhotopaint 12 keeps its Globalmacros file? cyrilgupta Corel Photo-Paint VBA 1 08-11-2004 16:02
How Do I set the PP object? andyb Corel Photo-Paint VBA 2 30-05-2004 00:04
Font Substitution Upon Import RobC CorelDRAW/Corel DESIGNER VBA 4 11-03-2003 00:06


All times are GMT -5. The time now is 02:40.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com