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