Each open document, or Document object, is a member of the Application.Documents collection. The documents in that collection appear in the order in which they were created or opened.
Corel DESIGNER and CorelDRAW provide a number of properties, methods, and events for working with documents, the most useful of which are listed in the following table.
For detailed information on any property, method, or event, see API documentation.
For more information on document-related activities, see the following subtopics:
Files of all supported formats can be imported. Imported files are placed on document layers, so information on importing files is provided in the section on working with layers (see Importing files into layers) rather than in this section on working with documents.
The Application object has two methods for creating documents: CreateDocument and CreateDocumentFromTemplate.
The Application.CreateDocument method creates an empty document based on the default page size, orientation, and styles:
Application.CreateDocument() As Document
The Application.CreateDocumentFromTemplate method creates an untitled document from a specified template (CDT) file:
Application.CreateDocumentFromTemplate(Template As String, _ [IncludeGraphics As Boolean = True])As Document
Both of these functions return a reference to the new document, so they are typically used in the following manner:
Dim newDoc as Document Set newDoc = CreateDocument
The new document becomes active immediately and can be referenced by using the Application.ActiveDocument property. For more information on this property, see Activating documents.
If you want, you can use event handlers to respond to events that are triggered by creating a document:
To open a document, you can use the Application.OpenDocument method.
VBA example for Corel DESIGNER:
Dim doc As Document Set doc = OpenDocument("C:\graphic1.des")
VBA example for CorelDRAW:
Dim doc As Document Set doc = OpenDocument("C:\graphic1.cdr")
If you want, you can use event handlers to respond to events that are triggered by opening a document:
Each open document is a member of the Application.Documents collection. The documents in that collection appear in the order in which they were created or opened.
To reflect the actual stacking order of the documents, you must use the Application.Windows collection.
The Application.ActiveDocument property provides direct access to the active document — that is, the document that is in front of all the other documents in the application window. ActiveDocument is an object of type Document and, therefore, has the same members — properties, methods, and objects — as the Document class.
If no documents are open, ActiveDocument returns nothing. You can check for open documents by using the following VBA code:
If Documents.Count = 0 Then MsgBox "There aren't any open documents.", vbOK, "No Docs" Exit Sub End If
The Document.Activate method activates a document so that it can be referenced by ActiveDocument. The following VBA code activates the third open document (if three or more documents are open):
Documents(3).Activate
Using the Document.Activate method on the Application.ActiveDocument property has no effect.
If you want, you can specify which open document to activate by referencing the one of the following properties:
You can check the filename of each open document by using the following VBA code:
Public Function findDocument(filename As String) As Document Dim doc As Document For Each doc In Documents If doc.FileName = filename Then Exit For Set doc = Nothing Next doc Set findDocument = doc End Function
You can then activate the returned document by using the Document.Activate method.
You can specify the reference point, unit of measurement, and drawing scale for a document by using the corresponding properties of the Document class.
The Document.ReferencePoint property specifies the reference point for a document. This point is referenced when positioning the objects in that document.
The Document.Unit property specifies the unit of measurement for a document. This unit is used to position and size the objects in that document.
The Document.WorldScale property specifies the drawing scale for a document. The drawing scale lets you make the distances in a drawing proportionate to real-world distances; for example, you can specify that 1 inch in the drawing corresponds to 1 meter in the physical world.
These properties affect all elements in your document, such as the objects that you draw. For optimal results, choose the settings that best fit your macro solution.
You can simultaneously display multiple windows for a single document. For example, a large document can be displayed with one window zoomed in to the upper-right corner of the document and another zoomed in to the lower-right corner. Although the individual windows can be zoomed and panned independently, turning the page in one window affects all windows.
By using the View Manager, you can create views that have individual display settings. Choosing a saved view displays the page according to the settings for that view.
In VBA, the Window object provides access to the windows that contain each View object for (or view of) a given document. The Window object represents a frame, while the View object displays the document inside that frame.
Besides letting you work with windows and views, the application lets you display documents by zooming and panning.
Working with windows
Each Document object has a Windows collection for displaying that document. To switch between windows, use the Window.Activate method:
ActiveDocument.Windows(2).Activate
The Document.ActiveWindow property provides direct access to the active window — that is, the document window that is in front of all other document windows.
The next window and previous window for the active document are referenced in the Window.Next and Window.Previous properties:
ActiveWindow.Next.Activate
To create a new window, use the Window.NewWindow method:
ActiveWindow.NewWindow
To close a window (and the document, if it has only one open window), use the Window.Close method:
ActiveWindow.Close
If you want, you can use event handlers to respond to events that are triggered by activating a window:
You can also use event handlers to respond to events that are triggered by deactivating a window:
Working with views
The Window.ActiveView property and the Document.Views property both represent document views. Each Window object has one ActiveView object, which represents the current view of the document; saving the display settings for an ActiveView object creates a view. In contrast, each Document object has a collection of View objects in its Views property; choosing a View object activates the corresponding saved view, which contains the display settings for the corresponding ActiveView object.
The only way to access an ActiveView object is from the Window.ActiveView property.
You can create a View object and add it to a Document.Views collection. The following VBA code adds the current ActiveView settings to the Views collection:
ActiveDocument.Views.AddActiveView "New View"
You can also create a view with specific settings by using the Document.CreateView method. The following VBA code creates a new View object that accesses the position (3, 4) in inches, uses a zoom factor of 95%, and displays page 6:
ActiveDocument.Unit = cdrInch ActiveDocument.CreateView "New View 2", 3, 4, 95, 6
To apply a saved view to the active window, call the View.Activate method:
ActiveDocument.Views("New View").Activate
Zooming
To zoom an ActiveView object by a set amount, set the ActiveView.Zoom property by specifying a double value in percent. For example, the following VBA code sets the zoom factor to 200%:
ActiveWindow.ActiveView.Zoom = 200.0
You can also zoom by using the following methods of the ActiveView class:
Panning
To pan an ActiveView object, you can move its origin by modifying the ActiveView.OriginX and ActiveView.OriginY properties. The following VBA code pans the document 5 inches to the left and 3 inches up:
Dim av As ActiveView ActiveDocument.Unit = cdrInch Set av = ActiveWindow.ActiveView av.OriginX = av.OriginX - 5 av.OriginY = av.OriginY + 3
Alternatively, you can use the ActiveView.SetViewPoint method:
Dim av As ActiveView ActiveDocument.Unit = cdrInch Set av = ActiveWindow.ActiveView av.SetViewPoint av.OriginX - 5, av.OriginY + 3
You can modify a document regardless of whether it is active.
Modifying an inactive document does not activate that document. To activate a document, you must use its Activate method (see Activating documents).
The following VBA code adds a layer named "fooLayer" to the third open document:
Dim doc As Document Set doc = Documents(3) doc.ActivePage.CreateLayer "fooLayer"
The following VBA code uses the findDocument() function to add a layer named "fooLayer" to the inactive document named barDoc.cdr:
Dim doc As Document Set doc = findDocument("barDoc.cdr") If Not doc Is Nothing Then doc.ActivePage.CreateLayer "fooLayer"
Two very useful methods of the Document class combine to create a "command group," which can reduce a series of programmed, document-related actions to a single, undoable step. These methods — BeginCommandGroup and EndCommandGroup — are demonstrated in the following VBA example:
Dim sh As Shape ActiveDocument.BeginCommandGroup "CreateCurveEllipse" Set sh = ActiveLayer.CreateEllipse(0, 1, 1, 0) sh.ConvertToCurves ActiveDocument.EndCommandGroup
The preceding code sets the Undo string in the Edit menu as Undo CreateCurveEllipse. Clicking this command undoes not only the ConvertToCurves operation but also the CreateEllipse operation.
A command group can contain many hundreds of commands, if required. Creating command groups can make your macros appear to be fully integrated into the application.
Two methods can be used for saving documents: Document.SaveAs and Document.Save.
The Document.SaveAs method saves a document by using the specified file path and filename. You can use this method to save a previously unsaved document or to save an existing document to a different file.
The Document.SaveAs method provides an optional parameter that lets you access the StructSaveAsOptions class to specify additional settings.
The Document.Save method saves over an existing document file — that is, by using the existing file path and filename for the document.
If you want, you can use event handlers to respond to events that are triggered by opening the Save dialog box:
You can also use event handlers to respond to events that are triggered by saving a document and closing the Save dialog box:
Finally, you can also use event handlers to respond to events that are triggered when the user responds to a request to save a document:
Files of all supported formats can be exported.
Files are exported at the Document level because the range of exported objects can extend over multiple layers and multiple pages. However, files are imported at the Layer level because each imported object is assigned to a specified layer on a specified page (see Importing files into layers).
The Document class has three file-export methods — Export, ExportEx, and ExportBitmap — all of which can export to the bitmap and vector formats.
The wide selection of supported file formats is due to the vast number of filters that are available to the application. Each filter lets you work with the files from another graphics application. To learn more about working with these filters, see Working with import filters and export filters.
If you want, you can use event handlers to respond to events that are triggered by opening the Export dialog box:
You can also use event handlers to respond to events that are triggered by exporting a document and closing the Export dialog box:
Finally, you can also use event handlers to respond to events that are triggered when the user responds to a request to export a document:
Understanding the Document.Export method
To export a page, you require only the Document.Export method, a filename, and a filter type. The following VBA code exports the current page to a TIFF bitmap file:
ActiveDocument.Export "C:\ThisPage.eps", cdrTIFF
However, the preceding code gives little control over the output of the image. More control is obtained by including a StructExportOptions object, as in the following VBA code:
Dim expOpts As New StructExportOptions expOpts.ImageType = cdrCMYKColorImage expOpts.AntiAliasingType = cdrNormalAntiAliasing expOpts.ResolutionX = 72 expOpts.ResolutionY = 72 expOpts.SizeX = 210 expOpts.SizeY = 297 ActiveDocument.Export "C:\ThisPage.eps", cdrTIFF, cdrCurrentPage, expOpts
A StructPaletteOptions object can also be included in the function call for palette-based image formats, if you want to provide the settings for automatically generating the palette.
Understanding the Document.ExportEx method
The Document.ExportEx method is the same as the Document.Export method, except that ExportEx can retreive the dialog-box settings for a filter and apply those settings to the export:
Dim eFilt As ExportFilter Set eFilt = ActiveDocument.ExportEx("C:\ThisPage.eps", cdrEPS) If eFilt.HasDialog = True Then If eFilt.ShowDialog = True Then eFilt.Finish End If Else eFilt.Finish End If
Understanding the Document.ExportBitmap method
The Document.ExportBitmap method is similar to the Document.ExportEx method in that it returns an ExportFilter object that can be used to display the Export dialog box. However, the ExportBitmap method simplifies the coding by taking the individual members of the StructExportOptions object as parameters:
Dim eFilt As ExportFilter Set eFilt = ActiveDocument.ExportBitmap("C:\Selection.eps", _ cdrTIFF, cdrSelection, cdrCMYKColorImage, _ 210, 297, 72, 72, cdrNormalAntiAliasing, _ False, True, False, cdrCompressionLZW) eFilt.Finish
Publishing documents to PDF is a two-step process. The first step is to specify the PDF settings (although this step can be skipped by specifying those settings from the application or by using the default settings). The second step is to export the file.
To specify the PDF settings, you can use the Document.PDFSettings property. This property is an object of type PDFVBASettings and contains properties for all PDF settings that can be set in the Publish To PDF dialog box.
The following VBA code for CorelDRAW exports pages 2, 3, and 5 to a PDF file named MyPDF.pdf:
Dim doc As Document Set doc = ActiveDocument With doc.PDFSettings .Author = "Corel Corporation" .Bookmarks = True .ColorMode = pdfRGB .ComplexFillsAsBitmaps = False .CompressText = True .DownsampleGray = True .EmbedBaseFonts = True .EmbedFonts = True .Hyperlinks = True .Keywords = "Test, Example, Corel, CorelDRAW, PublishToPDF" .Linearize = True .PageRange = "2-3, 5" .pdfVersion = pdfVersion13 .PublishRange = pdfPageRange .TrueTypeToType1 = True End With doc.PublishToPDF "C:\MyPDF.pdf"
The following VBA example gives more control to the user by displaying the Publish to PDF Settings dialog box:
Dim doc As Document Set doc = ActiveDocument If doc.PDFSettings.ShowDialog = True Then doc.PublishToPDF "C:\MyPDF.pdf" End If
Profiles for PDF settings can be saved and loaded by using the PDFVBASettings.Save method and PDFVBASettings.Load method (respectively).
Using VBA to print documents is straightforward: almost all settings that are available in the Print dialog box are available to the Document.PrintSettings property. When these properties are set, printing the document is simply a matter of calling the Document.PrintOut method.
For example, the following VBA code prints three copies of pages 1, 3, and 4 to a level-3 PostScript printer:
With ActiveDocument.PrintSettings .Copies = 3 .PrintRange = prnPageRange .PageRange = "1, 3-4" .Options.PrintJobInfo = True With .PostScript .DownloadType1 = True .Level = prnPSLevel3 End With End With ActiveDocument.PrintOut
Each page in the Print dialog box has a corresponding object-model class that contains all settings for that page. The following table lists these classes.
You cannot set layout options in VBA. However, if necessary, you can open the Print dialog box by using the PrintSettings.ShowDialog method.
You can print only the selected objects in a document by setting the PrintSettings.PrintRange property to prnSelection.
You can use a specific printer in the Application.Printers collection by specifying it in the PrintSettings.Printer property.
You can save a printing profile by using the PrintSettings.Save method.
You can access a saved printing profile by using the PrintSettings.Load method, but be sure to specify the full path to the profile.
You can reset the print settings by using the PrintSettings.Reset method.
If you want, you can use event handlers to respond to events that are triggered by opening the Print dialog box:
You can also use event handlers to respond to events that are triggered by printing a document and closing the Print dialog box:
Finally, you can also use event handlers to respond to events that are triggered when the user responds to a request to print a document:
You can close a document by calling the Document.Close method.
The following VBA code closes the active document and activates the document behind it:
ActiveDocument.Close
If the code closes a document that is not active, the document referenced by the Application.ActiveDocument property does not change.
You must explicitly test the Dirty property for a document and take appropriate action if that document has been modified.
You can also close a document by using the Close method of the Document object itself (as in doc.Close).
If you want, you can use event handlers to respond to events that are triggered by closing a document:
You can also use event handlers to respond to events that are triggered when the user responds to a request to close a document: