GlobalMacroStorage: How to close active document after open?

I need to automate some process. I wrote some code to publish cdr files as PDF:

Sub GlobalMacroStorage_DocumentOpen(ByVal doc As Document, ByVal FileName As String)    
Set doc = ActiveDocument
With doc.PDFSettings
...{some code here}
.Linearize = True
.PageRange = "1,2"
.pdfVersion = pdfVersion13
.PublishRange = pdfPageRange
.TrueTypeToType1 = True
.TextAsCurves = True
.OverprintBlackLimit = True
End With

If Len(Dir(doc.FilePath + "\coreltmp", vbDirectory)) = 0 Then
MkDir doc.FilePath + "coreltmp"
End If
name2 = Dir(FileName) + "TEST.pdf"
 strName = doc.FilePath & "coreltmp" & "\" & name2
doc.PublishToPDF strName
ActiveDocument.Close  <<=====ERROR LINE
End Sub

Marked line makes error: the document cannot be closed from the document event handler . So the question is: how close this document without close whole application?

Parents
  • Mario,

    You cannot close a document from a document event handler that is why you are getting the error. If you could then every document you opened would then just close. ;-) Not very good on the workflow for working with the document.

    I will also mention, when working with document events there is no need to Set doc = ActiveDocument because the document is being passed to you by the open event. That is what the ByVal doc as Document is doing. So you can just start using doc and it will work.

    If you want to open and close a document you just need to move your code outside a document even handler and you will be good to go.

    -Shelby
Reply Children
No Data