GDG Macros Lesson 7: A touch of WinAPI and using keyboard modifier keys

Hi.

Holding a key modifies the fucntion of a macro.

Ie hold shift and run the macro and it does this, hold Crtl and it does this... etc.

A great way to make your macros multi functional.

I also lightly touch base on Option Explicit which makes variable declaration necessary.

I also quickly talk about If, Else, ElseIf conditionals and colors.

Enjoy! (the code is pasted below)

~John

 

Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As LongPtr) As Integer
#Else
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
#End If

'Private Const VK_ESCAPE = &H1B
'Private Const VK_RETURN = &HD
Private Const VK_SHIFT = &H10
Private Const VK_CONTROL = &H11

Sub makeRect()

    Dim s As Shape, sRect As Shape
    Dim x As Double, y#, h#, w#
    Dim dMarg#
    Dim col As New Color
   
   
   
   
    If GetAsyncKeyState(VK_SHIFT) Then
        dMarg = 0.25
        col.RGBAssign 255, 0, 0
    ElseIf GetAsyncKeyState(VK_CONTROL) Then
        dMarg = 0.25
        col.RGBAssign 255, 0, 0
    Else
        dMarg = 0.1
        col.RGBAssign 0, 0, 255
    End If
   
    ActiveDocument.Unit = cdrInch
    Set s = ActiveShape
    If s Is Nothing Then Exit Sub
   
    's.GetBoundingBox x, y, w, h
    myGetBoundingBox s, x, y, w, h
   
    Set sRect = ActiveLayer.CreateRectangle2(x - dMarg, y - dMarg, w + (dMarg * 2), h + (dMarg * 2))
    sRect.Fill.UniformColor.CopyAssign col
    sRect.OrderToBack
   
End Sub

Private Function myGetBoundingBox(ByVal s As Shape, ByRef x As Double, y#, w#, h#)
     h = s.SizeHeight
     w = s.SizeWidth
     ActiveDocument.ReferencePoint = cdrBottomLeft
     x = s.PositionX
     y = s.PositionY
End Function

Parents
  • Hi.

    My explanation of the New and Set statement was a little wrong.

    Set assigns an object reference to a variable. In the video I said it creates it.

    Example.

    Set s = ActiveShape 'This sets the variable s to be a reference to the object.

    This basically means now you can use s to reference the object. You must do this with objects.

    In the video I said it created the variable when in actual fact it just sets the reference to an object. I hope this makes sense.

    Many times in the video I will use basic and simple terminology to try to explain things. My hope is that everyone can understand and follow along easily.

    So just to clarify

    You declare and object variable but must Set the reference. If you use the New word you can do this in one step, in the declaration!

    ~John


    Here's the VBA definition for Set and New:

     

    Set Statement

     

    Assigns an object reference to a variable or property.

    Syntax

    Set objectvar = {[New] objectexpression | Nothing}

    The Set statement syntax has these parts:

    Part Description
    objectvar Required. Name of the variable or property; follows standard variable naming conventions.
    New Optional. New is usually used during declaration to enable implicit object creation. When New is used with Set, it creates a new instance of the class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can't be used to create new instances of any intrinsic data type and can't be used to create dependent objects.
    objectexpression Required. Expression consisting of the name of an object, another declared variable of the same object type, or a function or method that returns an object of the same object type.
    Nothing Optional. Discontinues association of objectvar with any specific object. Assigning Nothing to objectvar releases all the system and memory resources associated with the previously referenced object when no other variable refers to it.

     

    Remarks

    To be valid, objectvar must be an object type consistent with the object being assigned to it.

    The Dim, Private, Public, ReDim, and Static statements only declare a variable that refers to an object. No actual object is referred to until you use the Set statement to assign a specific object.

    The following example illustrates how Dim is used to declare an array with the type Form1. No instance of Form1 actually exists. Set then assigns references to new instances of Form1 to the myChildForms variable. Such code might be used to create child forms in an MDI application.

    Dim myChildForms(1 to 4) As Form1
    Set myChildForms(1) = New Form1
    Set myChildForms(2) = New Form1
    Set myChildForms(3) = New Form1
    Set myChildForms(4) = New Form1
    

    Generally, when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created. More than one object variable can refer to the same object. Because such variables are references to the object rather than copies of the object, any change in the object is reflected in all variables that refer to it. However, when you use the New keyword in the Set statement, you are actually creating an instance of the object.

  • Hey RunFlaCruiser these video tutories about writing macros are great. Thank you for doing them

    and I hope you plan on doing a bunch more.

  • RunFlaCruiser,

     

    There is very little information on writing macros for Coreldraw and phototpaint. The PDF they have has

    lots of information but it is much easier to learn seeing with you are doing and then using the PDF as a reference.  Do you know

    if you will have a list of your videos someplace so we dont have to search for them in this form?

Reply Children