Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Outlook 2010 - macro "Print 1st page" - Problem with Num Lock turning off

  1. #1
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location

    Outlook 2010 - macro "Print 1st page" - Problem with Num Lock turning off

    I posted a thread here about a macro to print the first page of an email in Outlook 2010:

    Sub PrintOnePage()
    SendKeys "%FPR"
    SendKeys "%S"
    SendKeys "1"
    SendKeys "{ENTER}"
    End Sub

    This works fine but there is a known issue with multiple SendKey commands turning off Num Lock (I am not allowed to post the Microsoft KB link here but it is Microsoft KB 179987)

    I have tried the DoEvents solution but this just effectively makes the steps manual again.

    I have seen suggested solutions using the toggle and GetKeyboardState but the code I have seen appears to involve other macros and I am unable to tie it into the above.)

    Does anyone have any ideas about how to workaround the NumLock issue? Thanks for your help in advance!



    64 bit Optiplex 3010 - Win 8.1 - Office 2013 (with Outlook 2010)

  2. #2
    did you try
    sendkeys {NUMLOCK}
    to see if it will toggle it back?

    personally i avoid sendkeys, as it will not work with uac turned on, best to use some alternative

  3. #3
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    I like that idea but the only problem (which I failed to mention) is that the issue is intermittent (about 50/60% of the time).

    The benefit of being able to print out the first page of an email chain for our staff is greater than the annoyance of keep checking number lock but it would be nice to have an alternative to Sendkeys.

  4. #4
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Elsewhere on the web, I located a user who had the same problem (multiple send key commands turning off the NumLock but with a different macro). In response, someone posted the following code which apparently worked. Since I am not a coder - I do not know how to adapt this but it does appear to feature some code which looks like it is checking if NumLock is off or on and then taking appropriate course of action. I did try to extract and meddle with my code but it just broke my macro. Can anyone see anything that might work in this?

    Private Declare Sub keybd_event Lib "user32" ( _
    ByVal bVk As Byte, _
    ByVal bScan As Byte, _
    ByVal dwFlags As Long, _
    ByVal dwExtraInfo As Long)
    Private Const VK_NUMLOCK = &H90
    Private Const KEYEVENTF_KEYUP = &H2
    Declare Function GetKeyState Lib "user32.dll" ( _
    ByVal nVirtKey As Long) As Integer
    
    Sub test()
     'NUM_Off
     NUM_On
    End Sub
    
    Sub NUM_TOGGLE()
      'Toggle NUM-Lock key state
      keybd_event VK_NUMLOCK, 1, 0, 0
      keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
    End Sub
    
    Sub NUM_On()  'Turn NUM-Lock on
      If Not (GetKeyState(vbKeyNumlock) = 1) Then
        keybd_event VK_NUMLOCK, 1, 0, 0
        keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
      End If
    End Sub
    
    Sub NUM_Off() 'Turn NUM-Lock off
      If (GetKeyState(vbKeyNumlock) = 1) Then
        keybd_event VK_NUMLOCK, 1, 0, 0
        keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
      End If
    End Sub

  5. #5
    just call num_off from your code after the endkeys

    using keybd_event is more reliable than sendkeys anyway

  6. #6
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Thanks for your continued help but I am not sure I quite understand.

    I tried this (but it didn't work - gave a compile error)
    Sub PrintOnePage()
    SendKeys "%FPR"
    SendKeys "%S"
    SendKeys "1"
    SendKeys "{ENTER}"
    NUM_On
    End Sub
    Then I tried adding this after my code (but it didn't work either)

    Sub test()
    'NUM_Off
    NUM_On
    End Sub

    Sub NUM_TOGGLE()
    'Toggle NUM-Lock key state
    keybd_event VK_NUMLOCK, 1, 0, 0
    keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
    End Sub

    Sub NUM_On() 'Turn NUM-Lock on
    If Not (GetKeyState(vbKeyNumlock) = 1) Then
    keybd_event VK_NUMLOCK, 1, 0, 0
    keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
    End If
    End Sub

    Sub NUM_Off() 'Turn NUM-Lock off
    If (GetKeyState(vbKeyNumlock) = 1) Then
    keybd_event VK_NUMLOCK, 1, 0, 0
    keybd_event VK_NUMLOCK, 1, KEYEVENTF_KEYUP, 0
    End If
    End Sub

  7. #7
    you should probably have added a new module and copied all the additional code to the new module
    then your code should work
    the first 9 lines have to be right at the top of a code pane, so if you add to your existing, put above

  8. #8
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Ok So now my code looks like this. I tried again but the NumLock turns off when I run the macro . Is this what you meant?

    help.jpg

  9. #9
    looks fine
    try like
    Sub PrintOnePage()
     SendKeys "%FPR"
     SendKeys "%S"
     SendKeys "1"
     SendKeys "{ENTER}"
    num_on   'add this line
     End Sub

  10. #10
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Strangely, this produces a result where the Num Lock still turns off intermittently but if I then leave Num lock turned off, it intermittently turns back on when I use the macro! So its definitely an improvement but still not quite the silver bullet.

    Thank you for your continued assistance - much appreciated.

  11. #11
    try doevents before num_on

  12. #12
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Had a go but it didn't work

  13. #13
    as using your sendkeys code, neither prints anything nor turns off numlock on my machine, i can not test further to know what might work

    it is possible that converting the whole code to keybd_event may resolve the issue, but on the other hand it may not work at all

    try like
    keybd_event VK_MENU, 1, 0, 0
    keybd_event 46, 1, 0, 0
    
    keybd_event 46, 1, KEYEVENTF_KEYUP, 0
    keybd_event 50, 1, 0, 0
    keybd_event 50, 1, KEYEVENTF_KEYUP, 0
    'keybd_event 52, 1, 0, 0
    'keybd_event 52, 1, KEYEVENTF_KEYUP, 0
    keybd_event VK_MENU, 1, KEYEVENTF_KEYUP, 0
    keybd_event VK_MENU, 1, 0, 0
    'keybd_event 53, 1, 0, 0
    'keybd_event 53, 1, KEYEVENTF_KEYUP, 0
    keybd_event 47, 1, 0, 0
    
    keybd_event 47, 1, KEYEVENTF_KEYUP, 0
    
    keybd_event VK_MENU, 1, KEYEVENTF_KEYUP, 0
    keybd_event 31, 1, 0, 0
    keybd_event 31, 1, KEYEVENTF_KEYUP, 0
    keybd_event VK_RETURN, 1, 0, 0
    keybd_event VK_RETURN, 1, KEYEVENTF_KEYUP, 0
    define the extra constants
    Private Const VK_RETURN = &HD
    Private Const VK_MENU = &H12

  14. #14
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Thanks - I have tried to incorporate your code (see pic below) but it is giving me a compile error.

    VBA.jpg

  15. #15
    all the code except the constants, need to be within a procedure (Sub)

  16. #16
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Apologies - you need to bear with me - I am truly a novice with this sort of thing. I have tried again putting this in a procedure and replacing the original code. I put the constants in a separate module. Don't know if I needed to. Still getting an error:

    pic.jpg

  17. #17
    I put the constants in a separate module.
    constants (or functions) in a separate module need to be declared public rather than private
    you also need the module you were using before with the keybrd_event function and the other constants defined, you can add the 2 additional constants there

  18. #18
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Ok so now it looks like the pic. I think I have interpreted your instructions correctly about declaring "public" and I put them in the other module. Still getting an error thoughMacro.jpg

  19. #19
    as you will see in image in post #8
    functions and constants need to be defined at the top of the code module
    as i can not see the top of the module, i assume that the other constants and functions are at the top, if not they need to be, and all privates need to be made to public

  20. #20
    VBAX Regular
    Joined
    Jan 2014
    Posts
    14
    Location
    Ahhh - sorry - I missed that text. Ok - so the VB coding now looks like this.

    macro.jpg

    No errors this time - I tried to use it by clicking on an email and activating the macro but it appears to be activating a "clean up conversations" command.

    EDIT: Ok so I presume the VK Menu command is the ALT button? So I would need - ALT, H (for the home) and then Y3 for my macro button on the ribbon. Would that work?
    Last edited by Jarvster; 03-27-2014 at 08:13 AM. Reason: Additional thought

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •