Speeding Up CorelDraw Automation
Allowing CorelDraw to refresh the screen as it automatically places objects such as barcodes and text on the screen can slow up your code by 16 times.
For a small number of objects this is not a problem but when your computer takes half an hour or more to run your code you should alter your code for speed.
CorelDraw has the equivalent to Excel's & Word's Application.ScreenUpdating command. Corel's command is
Application.Optimization = True
Initially the terminology seems backward. Optimization = True seems like it is going to refresh the screen but it is referring to Optimizing the code.
Place Application.Optimization=True early in your code to prevent the screen refreshing as each object is automatically placed & moved on your screen.
Near the end of your code you need 2 lines of code to reverse Optimization = True.
Application.Optimization = False
ActiveWindow.Refresh
Application.Optimization = False will show the pages in your document, you may have created many new pages.
ActiveWindow.Refresh will show the active page with its contents.
A word of warning!
Should your code crash or you intentionally crash your code your CorelDraw windows will no longer refresh.
You either have to restart CorelDraw or preferably run Application.Optimization = False and ActiveWindow.Refresh
Run this piece of code to fix the problem.
Sub RefreshApplication()
Application.Optimization = False
ActiveWindow.Refresh
End Sub
Another problem of not seeing the screen refreshing is that you do not know if your code is on an infinite loop or whether it is progressing.
You should initially test your code on a small amount of data to make sure it works correctly but on the final set of data maybe the data has an error.
A progress bar is very useful.
The file in Windows\System32, MSComCtl.ocx can create a useful progress bar.
I have covered this in the topic Miscellaneous VBA under the heading of Adding a Progress Meter.
Is Reading From a Variable Faster Than From a Form?
This is a question that I have wondered.
In cases where your code repeatedly uses the same data such as the address of a logo file and places 100 of the same logo on a page.
Should the code read directly from the Form each time or create a variable that gets its value once from the Form and uses that variable multiple times.
Which method is quicker?
It turns out that reading from the Form over and over is slower than reading the Form once and placing the value into memory.
However the difference in time is negligible.
You have to read the Form over 1,000,000 times to slow you code by 1 second.
For my notebook computer running CorelDraw XI 64 bit it had to read it 1,037,344 times.
What I did was create a While loop that counted to 500,000,000.
The code noted the start time and the finish time, the difference being the run time.
The count increment was read from a TextBox on a Form, its value being 1.
Reading the increment repeatedly from the Form took 482 seconds to complete the code.
Reading the increment once and equating that to a variable took 6 seconds to complete the code.
I repeated basically the same code but used Microsoft Access and again reading the same TextBox in a Form is slower than reading the TextBox once.
Reading the increment repeatedly from the Form took 2589 seconds to complete the code.
Reading the increment once and equating that to a variable took 6 seconds to complete the code.
For my NoteBook the code had to read the Form 193,125 to slow the code by 1 second.
Here is the code.
Private Sub btnRun_Click()
'I had a Form with 3 text boxes.
'One was called ctrlInput that had a value of 1.
'Another TextBox was called ctrlForm for an answer.
'Another TextBox was called ctrlMemory for an answer.
'There was a Button called btnRun.
Dim lngCounter As LongPtr
Dim lngInput As LongPtr
Dim StartTime As Date
Dim FinishTime As Date
'Clear the answer TextBoxes.
Me.ctrlForm = ""
Me.ctrlMemory = ""
DoEvents
Application.Refresh
StartTime = Time()
lngCounter = 0
While lngCounter < 500000000
lngCounter = lngCounter + Me.ctrlInput
Wend
'Convert to seconds.
Me.ctrlForm = (Time() - StartTime) * 24 * 60 * 60
StartTime = Time()
lngInput = Me.ctrlInput
lngCounter = 0
While lngCounter < 500000000
lngCounter = lngCounter + lngInput
Wend
'Convert to seconds.
Me.ctrlMemory = (Time() - StartTime) * 24 * 60 * 60
DoEvents
Application.Refresh
End Sub
2017_08_31
|