CorelDRAW:Application
From OberonPlace Development Portal
Contents |
Application Class
The Application is the entry point to CorelDRAW/Corel DESIGNER object model. It's a top-level class that is used to get access to other objects and methods of the object model. The Application class itself represents the application instance itself. The methods and properties of the Application class are used to perform certain application-wide commands such opening documents, accessing configuration settings such as current install location, location of user data folders, getting a list of all open documents, windows, installed printers and so on.
Remarks
In VBA, the Application object is global, meaning that all of its methods and properties can be accessed without explicitly mentioning the Application. That is, the following two commands have exactly the same effect:
MsgBox Application.Documents.Count MsgBox Documents.Count
Both of the above commands will show a message box displaying the number of documents currently open in the application.
However this is not the case for other languages, including Visual Basic, C++, C#, and others. In environments other than VBA, the Application object is not immediately available. It must be created first. For example, here is how it is done in Visual Basic:
Dim app As New Application MsgBox app.Documents.Count
The New keyword in the declaration of the app variable means that the variable of type Application is being declared and a new instance of the object is created. In fact the above code is equivalent to the following:
Dim app As Application Set app = New Application MsgBox app.Documents.Count
Many languages require the full name of the object to be specified when declaring variables, including the name space to which the object belongs to. VBA and VB does not require this yet still allows. For example, CorelDRAW's object model resides in CorelDRAW namespace. Corel DESIGNER's object model is from CorelDESIGNER namespace.
If your macro/program needs to interact with more than one application (e.g. CorelDRAW and Corel PHOTO-PAINT) at the same time, you will have to specify explicitly which application you refer to when you type Application class name. This is done by supplying the namespace in front of the class name:
Dim app As New CorelDRAW.Application MsgBox app.Documents.Count
The same applies to C# and C++:
C#:
CorelDRAW.Application app = new CorelDRAW.Application(); System.Forms.MessageBox.Show(app.Documents.Count.ToString());
C++:
CorelDRAW::IDrawApplicationPtr app(__uuidof(CorelDRAW::Application) ); CString strMsg; strMsg.Format(_T("%d"), app->Documents->Count ); MessageBox( NULL, strMsg, _T("Message"), MB_OK);
Examples
The following example creates a new document, creates a rectangle in the document, then saves the document to C:\Temp\Graphic1.cdr and closes the document:
Sub CreateDocumentWithRectange() Dim doc As Document Dim sRectangle As Shape Set doc = Application.CreateDocument() Set sRectangle = doc.ActiveLayer.CreateRectangle(2, 10, 4, 8) sRectangle.Fill.UniformColor.RGBAssign 255, 0, 0 doc.SaveAs "C:\Temp\Graphic1.cdr" doc.Close End Sub
Since as explained above, all the properties and method of the Application class are global in VBA, there is no need to explicitly specify the Application class when calling CreateDocument method for example, so the above code can be written as follows:
Sub CreateDocumentWithRectange() Dim doc As Document Dim sRectangle As Shape Set doc = CreateDocument() Set sRectangle = doc.ActiveLayer.CreateRectangle(2, 10, 4, 8) sRectangle.Fill.UniformColor.RGBAssign 255, 0, 0 doc.SaveAs "C:\Temp\Graphic1.cdr" doc.Close End Sub
Version Information
- CorelDRAW: Available since version 9
- Corel DESIGNER: Available since version 10
References
