• Problem with this macro

    Author
    Topic
    #472742

    Some time ago, someone on this forum wrote a macro for me to change letterspacing (Kerning) between letters. If the shift key is held when the macro icon is clicked, the letterspacing is reduced by one point. If the shift key is not held down, then the letterspacing is reduced by 1/4 pt.

    The macro works fine when using the shift key, but when not using the shift key,I have to click the macro button (I put one in the toolbar for this macro) 3 times for it to work once. When using the Shift key, the letterspacing reduces with every click. Could someone check the code and see if they can see why it takes 3 clicks for the 1/4 pt reduction?

    Dim FF As Object
    Dim x
    On Error GoTo -1: On Error GoTo UserCancel
    If WordBasic.SelType() = 2 Then
    Set FF = WordBasic.DialogRecord.FormatFont(False)
    WordBasic.CurValues.FormatFont FF

    If GetAsyncKeyState(16) < 0 Then
    x = WordBasic.Val(FF.Spacing) – 1
    Else
    x = WordBasic.Val(FF.Spacing) – 0.25
    End If
    WordBasic.FormatFont Spacing:=Str(x)
    End If
    UserCancel:

    Viewing 3 reply threads
    Author
    Replies
    • #1253050

      Could you post all of the related code?
      This code includes a call to a custom function called “GetAsyncKeyState” – without that function, this snippet of code won’t compile or run.

      Just how long ago did someone here write this code? WordBasic was officially replaced by VBA with the advent of Office 97. It’s still possible to use WordBasic commands, but with a few exceptions, they’ve been superseded by VBA – so this code appears to date from around 1996 or earlier. What version of Word are you currently running this in?

      Gary

    • #1253093

      Private Declare Function GetAsyncKeyState Lib “User32” (ByVal nVirtKey As Long) As Integer
      Private Declare Function GetKeyState Lib “User32” (ByVal nVirtKey As Long) As Long
      ==========================

      Those 2 lines are at the top of my macro list.

      Yes, these macros are old. Some (maybe most) of my big time-savers go back to the late 90s. I’ve got 4 or 5 that are very handy that I use to show my computer class how to create macros and customize the toolbar (or ribbon). They all still work except for this little glitch with the shift key on this kerning macro, and one that deals with line spacing that doesn’t work at all.

      We just switched from Office 2003 to 2010 this fall. These same macros wouldn’t work on 2003, either.

      I’m not a programmer. I’ve been able to study the ones that people have done for me and been able to modify them or create some simple new ones, but I could never make anything like this from scratch with VBA.

    • #1253103

      It’s hard to remember the WordBasic handling of dialogs, but I think this is the VBA equivalent of that code:

      Code:
      Sub ReduceLetterSpacing()
      With Selection
          If .Type = wdSelectionNormal Then
              With .Font
                  If GetAsyncKeyState(16) < 0 Then
                      .Spacing = .Spacing - 1
                  Else
                      .Spacing = .Spacing - 0.25
                  End If
              End With
          End If
      End With
      End Sub
    • #1253106

      Well, it appeared as though that version wasn’t working, either. Every third time I ran it, it would condense the type, but the first two times would do nothing. Then it struck me that maybe it WAS working, but the amount was too small to visualize. I zoomed way in on the text, and then it would show movement on every click. If I would have bothered to open up the Font window and look at the spacing settings, I would have seen that it WAS increasing the condensing of the font by .25 pts each time it was clicked.

      So, the old version was working all along, but thanks for giving me the new code. Maybe I can learn a few things from it. Thanks a lot.

    Viewing 3 reply threads
    Reply To: Problem with this macro

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: