公告
本專欄的相關(guān)的文章已不在簡書同步更新,請直接訪問 VBA探秘 官方網(wǎng)站或關(guān)注微信公眾號獲取最新文章動態(tài)。

目的
這篇教程將教會你使用 CorelDRAW VBA代碼來創(chuàng)建和打開文檔,演示 Document 對象的使用方法。
創(chuàng)建文檔
Application 對象有兩個方法用來創(chuàng)建文檔:CreateDocument 和 CreateDocumentFromTemplate。下面做簡單說明:
Application.CreateDocument 方法基于默認(rèn)的頁面大小、方向和樣式創(chuàng)建了一個空文檔,該函數(shù)的定義如下:
Application.CreateDocument() As Document
Application.CreateDocumentFromTemplate 方法從一個特定的模板文件來創(chuàng)建一個無標(biāo)題文檔,該函數(shù)的定義如下:
Application.CreateDocumentFromTemplate(Template As String, [IncludeGraphics As Boolean = True]) As Document
其中,第一個必選參數(shù)Template是模板文件的位置,模板文件的后綴名一般是.cdt;第二個參數(shù)是可選參數(shù),用來指定是否包含圖像,默認(rèn)值為True。
這兩個創(chuàng)建文檔的函數(shù)都返回了一個新文檔對象的引用,因此,它們通常以以下方式使用:
Dim newDoc as Document
Set newDoc = CreateDocument
新創(chuàng)建的文檔可以通過使用 Application.ActiveDocument 來立即激活,要查看激活文檔的相關(guān)信息,請繼續(xù)往下瀏覽。
如果您愿意,可以使用事件處理程序?qū)?chuàng)建文檔觸發(fā)的事件作出響應(yīng),也就是說在某個文檔創(chuàng)建后,你可以做你自己想做的事情:
- Application.DocumentNew
- GlobalMacroStorage.DocumentNew
以用戶創(chuàng)建的全局宏(GlobalMacroStorage)為例,詳細請看代碼:
' 運行入口(請在宏面板中雙擊運行此方法)
Sub main()
Dim newDoc As Document
Set newDoc = CreateDocument
End Sub
'*********************************************************************************************
' 文檔回調(diào)事件
' 每當(dāng)新的文檔被創(chuàng)建時,此過程被觸發(fā)
' @author Zebe
'*********************************************************************************************
Private Sub GlobalMacroStorage_DocumentNew(ByVal doc As Document, ByVal FromTemplate As Boolean, _
ByVal Template As String, ByVal IncludeGraphics As Boolean)
MsgBox "檢測到新文檔被創(chuàng)建,文檔名稱:" & doc.title
End Sub
小結(jié)
本篇教程到此結(jié)束,更多關(guān)于文檔對象(Document)的使用,請瀏覽博客其他文章。
原創(chuàng)聲明:本文首發(fā)于個人CorelDRAW VBA博客,請尊重文章版權(quán)。
轉(zhuǎn)載請注明原文鏈接:http://www.cdrvba.com/coreldraw-vba-use-document/