Power of Macros
Macros are very powerful tool for work, and if you are not familiar with them you are missing great opportunity to speed up most of your work, especially if you are working with tables, text formatting and even with drawing. With this SoftwareTipsPalace.com post in out Tech & Tests section we will try to help you to understand much better this great tool which is available in most of software’s like Word, Excel, CorelDRAW, etc…
Jul 16, 2007 18:21 PM
-----------------------------------------------------------------------------------------------------------
Most of software users do have possibility to work with macros, scripts, actions etc... In basic description it's possibility of applications to remember group of your moves and to repeat those move as much as you want. Primarily ideas on using of Macros were to make any work easier and to make shorter all repeating operations. We will start with example inside macrosoft Word (which of course have possibility to work with Macros). Let us say that you have some text from your friend with more tithe of tables which do not look like same trough whole text or simply don't like you want. To edit all these tables using macros do next:
- Open your text with MS Word
- Mark first table,
- then choose option "Tables --> Macros Record new Macro",
- Give the name to macro which you just created and start with table formatting (table weight, line weight, font style etc.)
- After you finish with table formatting click "Tools --> Macros --> Stop Recording" to end macro recording,
- Next time when you select any table inside this document you can setup same values by calling previous save macro (Tools --> Macros --> Macros --> choose macro and click Run)
- You can also add shortcut for this macro and with one click you can fix every table inside this document.
To see how Macro is written, choose "Tools --> Macros "STPTable" --> Edit" where "STPTable" represent macro name:
Sub FixSTPTable ()
With Selection.Cells(1)
.LeftPadding = MillimetersToPoints(1)
.RightPadding = MillimetersToPoints(1)
.VerticalAlignment = wdCellAlignVerticalCenter
.WordWrap = Tru
.FitText = False
End With
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.Tables(1) .PreferreWidthType = wdPreferreWidthPoints
Selection.Tables(1) .PreferreWidt = MillimetersToPoints(112)
Selection.Font.Name = "Arial"
Selection.Font.Size = 8
With Selection.Tables(1)
With .Bords (wdBorderHorizontal)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorBlack
End With
With .Borders (wdBorderVertical)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth075pt
.Color = wdColorBlack
End Withn
End With
Selection.Rows.AllowBreakAcrossPages = True
End Sub |
Even if you never did met with something similar, it will be evidence to you that macros are simple writhed options and their parameters.
Unfortunately, there are some limits on macros using. When you call saved macro, he will literally repeat all steps which you saved for a first time. If you change any parameter condition, macros which you are calling will not do his job and he will report on error. One more limitation on using of macros is that programs can’t record all your moves which you are making with your mouse.
Small macro Modifications
Next example is “text cleaner” and it is function “Replace”, function which we here in SoftwareTipsPalace.com using for small cleaning of “dirty” text. You will see that with small modifications of saved macro you can very easily add new macro possibility. Also the process of macro saving will be shorter with all set of functions compared with previous example.
If you save your macro like one text replacement “aaa” into “bbb” using function “Edit:Replace” and look how it looks like (Tools --> macro --> Macros --> choose macro --> Edit) it will look like example below :
Sub Macro1 ()
Selection.Find.ClearFormating
Selection.Find.Replecement. ClearFormating
With Selection.Find
.Text = “aaa”
.Replecement.Text = “bbb”
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub |
We will change macro so the function call Replace will apply on whole content of active document and not only on selection and instead one “Execute” we will add whole series of them, with different values for marks which we are changing and marks which we are using to change them. Look example below:
Sub TextReader ()
With ActiveDocument.Content.Find
.ClearFormating
.Replacement.ClearFormating
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWholecards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll, Findtext:=”<<”, Replacewith:=Chr (34)
.Execute Replace:=wdReplaceAll, Findtext:=”>>”, Replacewith:=Chr (34)
.Execute Replace:=wdReplaceAll, Findtext:=”,,”, Replacewith:=Chr (34)
.Execute Replace:=wdReplaceAll, Findtext:=”””, Replacewith:=Chr (34)
.Execute Replace:=wdReplaceAll, Findtext:=”””, Replacewith:=Chr (34)
.Execute Replace:=wdReplaceAll, Findtext:=”^p”, Replacewith:=”^p”
.Execute Replace:=wdReplaceAll, Findtext:=” ^p”, Replacewith:=”^p”
.Execute Replace:=wdReplaceAll, Findtext:=”^p^t”, Replacewith:=”^p”
.Execute Replace:=wdReplaceAll, Findtext:=” ”, Replacewith:=” ”
End With
End Sub |
This macro is equivalent to process in which we with more “Replace” commands replaced different quotation marks into normal quotation marks, remove blank space from end of text and beginning of paragraph, and also changed double space into one space.
Small macros programming
By modifying macros (now we need to use programming techniques) we are accomplish to important goals:
- Possibility to use much more additional functions which are don’t exist in programs menus,
- Making our macros a little bit smarter (now our macros are becoming small programs)
Now, using these new techniques you could add few additional checking’s in our previous example with tables, and we will be sure that macro will work only if he accomplishes certain prerequisite. You can also add that macro selected table if she is not selected (because we hope that you remember that in our first macro example we didn’t choose Table --> Select --> Table).
Most important is that using programming you can have much more options and modifications for your macro and you can save a lot of time in work.
Use Macros in painting
We did talk enough about Word, lets us say few word about CorelDRAW, which also have possibility to work with macros, and he can also work with VBA. Using saved macro you can repeat whole series of same transformations for more than one graphics objects. But the real power of macros is in programming. Here one example:
Sub STPColors()
Dim s(10, 10) As Shape
ActiveDocument.Unit = cdrMillimeter
ActiveDocument.ReferencePoint = cdrBottomLeft
With ActiveLayer
For i = 1 To 10
For j = 1 To 10
Set s(i, j) = .CreateRectangle2(i * 10, j * 10, 10, 10)
s(i, j) .FillUniformColor.CMYKAssign i * 10, j * 10, 0, 0
s(i, j) .Outline.Type = cdrNoOutline
Next j
Next i
End With
End Sub |
In bottom left corner of CorelDRAW document this macro will draw 10x10 square grids with size of 10x10 mm; this square will also have background color with arithmetic color values.
<< Back to Techs & Tests
Progress of computer Technology's >> |