OberonPlace.com Forums  

Go Back   OberonPlace.com Forums > Blogs > shelbym

Rate this Entry

Making Your Favorite Macro / Script Better

Posted 05-10-2021 at 23:44 by shelbym

Introduction

In this post we will be looking at how to modify a Macro / Script in CorelDRAW. Beginning in CorelDRAW 2019 Macros were renamed to Scripts. I have been using CorelDRAW a long time, so I still refer to them as Macros. Just know that depending on your version of CorelDRAW it may use the term Macro or in the newer versions it will use the term Script. The goal of this post is to give you a nudge to try and make an improvement to your favorite macro yourself.

Macro

If you follow the CorelDRAW Community forums there have been several questions recently about the Oberon Color Replacer Macro. This is an excellent example of how a macro has been improved over time. You can find the original here: Oberon Color Replacer for CorelDRAW/Corel DESIGNER The most recent version with changes from Os, Lev and myself can be found here: Oberon Color Replacer Advanced Remake Lets use this macro to make a few improvements.

Setup

I have downloaded the Oberon Color Replacer Advanced Remake zip (lf_ColorReplacer29.3.8.zip) and extracted it. It extracts to: lf_ColorReplacer29.3.8.gms Since I am making a change to the macro I am going to make a copy of it so that I have a backup and rename my copy to: lf_ColorReplacer29.3.9.gms
I then copy the lf_ColorReplacer29.3.9.gms to my GMS folder. To do this, open Windows File Explorer, type %appdata% and press enter.

Name:  WindowsFileExplorer.png
Views: 383
Size:  19.4 KB

That will take you to your AppData folder. Since I am using CorelDRAW 2021 I would select Corel | CorelDRAW Graphics Suite 2021 | Draw | GMS, copy the file into the GMS folder.

Run the Macro

The first thing we want to do is run the macro and make sure it works. So launch CorelDRAW. From the menu select Tools | Scripts | Run Script, from the Macros in: dropdown select ColorReplacer (lf_ColorReplacer29.3.9.gms) that should highlight the MainModule.StartReplacer so you may just click Run.

Name:  OberonColorReplacer.png
Views: 364
Size:  39.3 KB

Hopefully you see the macro running as shown in the screenshot above. The first thing I noticed is that the copyright text is cut off. I think this should be our first fix.

A Simple UI Fix

To fix the UI we need to get into the Script Editor. You can use the menu to get there by clicking Tools | Scripts | Scripts Editor or by pressing Alt+F11.
Once the editor is opened on the left hand side find the ColorReplacer (lf_ColorReplacer29.3.9.gms) and click the + next to is so that it expands. Then click the + next to Forms so it expands. You should see an entry for MainForm, double click it and the form should open on the right-hand side.

Name:  CopyrightTextBefore.png
Views: 358
Size:  6.5 KB

Click the label that has the copyright text and I am going to make it a little wider by dragging the middle square to the right. The text doesn’t flow correctly so I am going to fix that. Click again inside the label and you should get an edit text cursor. I am going to delete the space after the www.oberon.com and then click Shift + Enter to make a new line. I will do that same for the other lines until it looks nice.

Name:  CopyrightTextLineBreaks.png
Views: 363
Size:  6.9 KB

This looks much nicer. But you will notice that it has two URLs in it and if try these URLs they are no longer valid. Lets update them to a valid URL. So I delete the recent.tekcities.com and recentfiles.sbn.bz and replace it with woxxom.vbabyexample.com
Close the Editor and run the macro again. It should look like this:

Name:  CopyrightTextAfter.png
Views: 364
Size:  6.8 KB
Name:  OberonColorReplacerAfter.png
Views: 346
Size:  38.9 KB

Congratulations you have made your first improvement to a macro. Yes, it was a simple change but we all need to start somewhere. Lets look for another way we can improve the macro.

Replace in Group in Powerclip

We need to do a little setup for this section. First create a rectangle and fill it with Blue, then make a copy of it. Then create two more rectangles, fill one yellow and one blue. Group these two rectangles. Create a bigger rectangle that will fit the group of the yellow and blue rectangles and the single blue rectangle. Select the group and single blue rectangle and from the menu click Object | Powerclip | Place inside frame and select the larger rectangle. When you are done you should have something that looks like this:

Name:  ShapesSetup.png
Views: 355
Size:  27.5 KB

Run the Oberon Color Replacer and Find the Blue Color and Replace it with Red. You will notice that only two of the three blue rectangles change to red. The one that does not change is a group within a powerclip.

Name:  OberonColorReplacerReplace.png
Views: 373
Size:  40.5 KB

Name:  ShapesGroupInPowerclip.png
Views: 348
Size:  3.0 KB

So we know it works inside a powerclip but not for a group inside a powerclip. Lets take a look at the code. Open the Editor and right click on the MainForm and select View Code. Since we know it works inside a powerclip the first thing I did was do a find (Ctrl + F) for “powerclip”, set the Search for Current Project so that it will look in all the code and clicked Find Next. The word is found only twice and in the same line of code:

Code:
If Not s.PowerClip Is Nothing Then DoReplaceOnShapes s.PowerClip.Shapes.All, cfg
This line is telling the code what shapes to look at in the powerclip. If we select the powerclip and in the Immediate windows type:

Code:
?ActiveShape.PowerClip.Shapes.All.Count
It will print in the window “2”. This is because it is only finding two shapes, the ungrouped shape and then the group. What we want it to do it also return the shapes in the group. The can be accomplished by using FindShapes. In the Immediate windows type:

Code:
?ActiveShape.PowerClip.Shapes.FindShapes().Count
This time it will print to the window “4” because it now finds the ungrouped shape, the group and then the two shapes inside it for a total of four.

Lets make the change. The new line of code should now look like this:

Code:
 If Not s.PowerClip Is Nothing Then DoReplaceOnShapes s.PowerClip.Shapes.FindShapes(), cfg
Close the Editor. Undo the previous replace so that all three rectangles are blue again. Run the macro, click OK and this time all three blue rectangles should now be red.

Eyedropper

If you use the same sample file we created and try to use the eyedropper to select a color inside the powerclip it does not select a color, it just beeps. Lets add the ability for the eyedropper to find a color inside the powerclip.

Open the Editor, right click on the MainForm and select View Object. You should see the form at this point. On the form double click the eyedropper, this will take you to the code that runs when you click on the eyedropper.

Looking at the code there is no check for a powerclip so lets add one. Change the following line:

Code:
If sh.Type = cdrGroupShape Then
To the following:

Code:
If sh.Type = cdrGroupShape Or Not sh.PowerClip Is Nothing Then
This adds a check for a powerclip. Once we know there is a powerclip we want to extract the contents so that they can be found. We can do this by changing the following line:

Code:
 sh.UngroupAll
To

Code:
If Not sh.PowerClip Is Nothing Then
    sh.PowerClip.ExtractShapes.UngroupAll
Else
    sh.UngroupAll
End If
Close the editor and run the macro. Give the eyedropper a try again and this time it should find the color of a shape inside the powerclip.

You will also want to make this same change to the code for the Replace eyedropper.

Conclusion

In this post we took an existing macro and fixed the follow:
  • Copyright Text
  • Replace now works with Groups inside a Powerclip
  • Eyedropper now works for shapes inside Powerclip

If you do not want to make the changes yourself, you may download the macro with these changes here:

lf_ColorReplacer29.3.9.zip

Happy Coding!
Posted in VBA
Views 1325 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 
Total Trackbacks 0

Trackbacks


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


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