OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Developer Forums > VBA > CorelDRAW/Corel DESIGNER VBA

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 07-06-2009, 23:43
xxxslowjoe
Guest
 
Posts: n/a
Default Long Macro Kills CorelDRAW

Hi,

I wrote my first VBA macro for CorelDRAW X4. Basically, it loops thru many MasterPage Layers making them visible and printable, then loops thru all the regular pages and publishes each one to PDF. It works well but after awhile I get an error message something like "An error has caused CorelDRAW to stop working" and I have to kill CorelDRAW then restart it. This is a wild guess but I think either there are too many things to "undo" or auto save is kicking in.

Is there a way to either tell CorelDRAW not to keep track of changes or disable autosave?

Thanks for any ideas,
Joe
Reply With Quote
  #2  
Old 08-06-2009, 07:20
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
 
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe
Default

The best way to do this is...

Code:
Sub MySub()
     EventsEnabled = False
     ... Do some code
     EventsEnabled = True
End Sub
This will tell Corel not to record for undo, any of the things between False and True. Be very careful with this. Things can get broken in your file and become corrupt when you are doing this. If you could share your code and maybe a sample file, we could see about "trimming the fat" from your macro and getting it streamlined.
__________________
Sean
Waiting for a ride in the T.A.R.D.I.S.
Reply With Quote
  #3  
Old 08-06-2009, 10:42
xxxslowjoe
Guest
 
Posts: n/a
Default

Hi Sean,

Thanks for the input. I will give it a try tonight. What do you mean when you say things can get corrupted in my file? I'm not actually changing anything like text or drawing anything - all I am doing is making different layers visible or not and printable or not and then publishing to pdf.

I would appreciate it if someone could look over my code - I'll post it tonight (I'm not at my Corel computer).

Thanks again,
Joe
Reply With Quote
  #4  
Old 08-06-2009, 11:21
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
 
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe
Default

Actually, it is less likely that the file can be corrupted by turning off events. Turning off Optimization or setting Begin and End Command Groups can be dangerous. VBA help (F1 when a command is highlighted in your code) can show you the warnings. The things to search for are:
  • BeginCommandGroup
  • EndCommandGroup
(these two will allow you to use the undo (CTRL-Z) key to undo a set of commands sent through VBA.)
  • Optimization = False
  • Optimization = True
(this turns screen redraw on and off. keeps from annoying flicker while code is running)

Check help documentation for correct usage on these.
__________________
Sean
Waiting for a ride in the T.A.R.D.I.S.
Reply With Quote
  #5  
Old 09-06-2009, 23:16
xxxslowjoe
Guest
 
Posts: n/a
Default

Hi,

I tried turning off/on events at the start and end of my processing. At first it seemed to work but after a few tries, Windows killed CorelDRAW again. I have created a watered down version of my .cdr file that mimics my real one (see attached).

Here is the code - I was able to find boostStart/boostFinish to help me:

Code:
Public Sub PrintFlyers()

    boostStart
    PrintBranch("Branch2") ''''''''' start here
    boostFinish

End Sub

Sub boostStart(Optional unDo$)
   On Error Resume Next
   If Len(unDo) Then ActiveDocument.BeginCommandGroup unDo
   Optimization = True
   EventsEnabled = False
   ActiveDocument.SaveSettings
   ActiveDocument.PreserveSelection = False
End Sub

Sub boostFinish(Optional ByVal endUndoGroup As Boolean = False, Optional ByVal doRedraw As Boolean = True)
   On Error Resume Next
   ActiveDocument.PreserveSelection = True
   ActiveDocument.RestoreSettings
   EventsEnabled = True
   Optimization = False
   If endUndoGroup Then ActiveDocument.EndCommandGroup
   
   If doRedraw Then
      CorelScript.RedrawScreen
      ActiveWindow.Refresh
      Application.Refresh
   End If
End Sub

Sub PrintBranch(ByRef strBranchName As String)

    Dim currLayer As Layer
    Dim strRep As String
    
    Dim currIdx As Integer
    Dim headerIdx As Integer
    Dim footerIdx As Integer
    Dim startIdx As Integer
    Dim stopIdx As Integer
    
    headerIdx = 0
    footerIdx = 0
    currIdx = 0
    startIdx = 0
    stopIdx = 0
    
    For currIdx = 1 To ActiveDocument.MasterPage.Layers.Count Step 1
    
        Set currLayer = ActiveDocument.MasterPage.Layers(currIdx)
        
        If currLayer.Name Like "Header*" & strBranchName Then        
            headerIdx = currIdx
            currLayer.Visible = True
            currLayer.Printable = True           
        ElseIf currLayer.Name Like "Footer*" & strBranchName Then       
            footerIdx = currIdx
            currLayer.Visible = True
            currLayer.Printable = True        
        Else
            currLayer.Visible = False
            currLayer.Printable = False            
        End If
            
    Next currIdx
    
    startIdx = headerIdx + 1
    stopIdx = footerIdx - 1

    For currIdx = startIdx To stopIdx Step 1
    
        Set currLayer = ActiveDocument.MasterPage.Layers(currIdx)
        
        strRep = currLayer.Name
        
        currLayer.Visible = True
        currLayer.Printable = True
        
        PrintPagesForRep strBranchName, strRep, False
        PrintPagesForRep strBranchName, strRep, True
    
        currLayer.Visible = False
        currLayer.Printable = False
            
    Next currIdx
    
End Sub

Sub PrintPagesForRep(ByRef strBranchName As String, ByRef strRepName As String, ByRef bForBlankPaper As Boolean)

    Dim strDirectory As String
    Dim strFileName As String
    Dim p As Page
    
    If bForBlankPaper Then
        strDirectory = "C:\Flyers\" & strBranchName & "\" & strRepName & "\For Blank Paper\"
    Else
        strDirectory = "C:\Flyers\" & strBranchName & "\" & strRepName & "\For Preprinted Paper\"
    End If

    myMkDir (strDirectory)
    
    For Each p In ActiveDocument.Pages
    
        p.Activate
    
        p.Layers("FlyerPaper").Printable = bForBlankPaper
            
        If bForBlankPaper Then
            strFileName = strRepName & "_Blank_" & p.Name & ".pdf"
        Else
            strFileName = strRepName & "_Preprinted_" & p.Name & ".pdf"
        End If
    
        With ActiveDocument.PDFSettings
            .PublishRange = 1 ' CdrPDFVBA.pdfCurrentPage
            .PageRange = ""
            .ConvertSpotColors = False
            .EncryptType = 1 ' CdrPDFVBA.pdfEncryptTypeStandard
        End With
  
        ActiveDocument.PublishToPDF strDirectory & strFileName
    
    Next p
    
    

End Sub

Sub myMkDir(strFolderName As String)
    On Error Resume Next
    Dim a, t As String, i As Integer
    a = Split(strFolderName, "\")
    t = a(0)
    For i = 1 To UBound(a)
      t = t & "\" & a(i)
      MkDir t
    Next
End Sub
Attached Files
File Type: cdr Flyers.cdr (26.0 KB, 195 views)

Last edited by shelbym; 09-06-2009 at 23:56. Reason: Added Code Brackets
Reply With Quote
  #6  
Old 09-06-2009, 23:55
shelbym's Avatar
shelbym shelbym is offline
Senior Member
 
Join Date: Nov 2002
Location: Cheyenne, WY
Posts: 1,769
Blog Entries: 9
Send a message via ICQ to shelbym Send a message via AIM to shelbym Send a message via MSN to shelbym Send a message via Yahoo to shelbym
Default Your Code

I have run your sample code a dozen or so times and cannot get Draw to hang or crash. It is pretty quick about 3 - 4 seconds.

-Shelby
Reply With Quote
  #7  
Old 10-06-2009, 08:39
xxxslowjoe
Guest
 
Posts: n/a
Default

Hi Shelby,

Thanks for looking at it. Actually, I can't get it to crash with these "flyers" either. The real file I have is ~180MB. Each page contains real photos and text. When I print for a small branch, things work fine. But when I have a branch with say 20 reps, it will get through maybe 14 and then the program dies. It doesn't really worry me now because everything is saved off and I just start again. It seems to have something to do with the size or amount of data then. Does my code look like it's doing anything wrong?

Thanks again,
Joe
Reply With Quote
  #8  
Old 10-06-2009, 12:18
ddonnahoe's Avatar
ddonnahoe ddonnahoe is offline
Senior Member
 
Join Date: Jan 2004
Location: Louisville, KY
Posts: 552
Send a message via ICQ to ddonnahoe Send a message via AIM to ddonnahoe Send a message via MSN to ddonnahoe Send a message via Yahoo to ddonnahoe
Default

Your code looks fine. I would suggest putting some kind of text file writer in the code so that you can trace errors. It may be as simple as a corrupt shape or font in your document. If you write to an external text file as the code progresses, you can track down the culprit a little easier. At least to the specific page.
__________________
Sean
Waiting for a ride in the T.A.R.D.I.S.
Reply With Quote
  #9  
Old 10-06-2009, 18:08
xxxslowjoe
Guest
 
Posts: n/a
Default

Hi,

The thing is it happens at different places at different times. I could run the program for a large branch and have it crash, restart CorelDRAW, run it again on the same branch and it works. That's why I was thinking it had something to do with "undo" events or auto-save. Basically I was thinking that something made windows think the program was locked up and report the error.

Thanks again for looking into it,
Joe
Reply With Quote
  #10  
Old 17-06-2009, 01:50
wOxxOm's Avatar
wOxxOm wOxxOm is offline
Senior Member
 
Join Date: Mar 2005
Posts: 836
Default

if the problem persists, try to close/reopen the document once in a while programmatically :-) e.g. each 10 pages or whatever
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
copy paste takes long time vindaa CorelDRAW/Corel DESIGNER VBA 1 08-08-2008 00:49
F1 - going for a long shot dungbtl CorelDRAW/Corel DESIGNER VBA 2 04-08-2008 09:19
Calculating creep in a long publication sallybode General 2 03-04-2006 21:03
New Macro / script for CorelDraw V. x acaputo1 New product ideas 4 31-08-2003 13:43
takes Draw a long time to start a new document! bbolte CorelDRAW/Corel DESIGNER VBA 5 14-05-2003 09:09


All times are GMT -5. The time now is 10:17.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
Copyright © 2011, Oberonplace.com