How to stay always “Available” in Microsoft Teams with Excel

by Misc11 comments

Or create your own Excel file by reading the text below (and copy / paste the vba code)

Why?

First it was a private joke, with this article it’s now a public joke.

Our client switched from Skype to Teams and it was a very cool improvement. My colleagues told me with a bit of fun: “Teams is cool, but you can’t change the time before appearing away!”. And it’s true, we can’t! Somebody said, “If you install a software who will click randomly on your screen, you will not appear away anymore”. Yes,.. but our software policy does not allow us to install anything on our computers.

It gave me an idea! Thanks to our very good old friend, Excel! People will say “Lazy is smart”, Arnaud is “funny” or any other reason. But I had fun with Excel and his perfect VBA editor.

My colleague Isabelle (and also Maslow) will say :

https://en.wiktionary.org/wiki/if_all_you_have_is_a_hammer,_everything_looks_like_a_nail

My hammer is Excel and Code.. I agree.. (nail or snail.. it’s just a “s” who is missing)

I created a small excel sheet who will loop until you ask it to finish and will move your mouse cursor every 5 seconds.

My joy is shared between.

“I’m the Microsoft BI guy who uses Microsoft Office and VBA code to do a joke”.

and

“People will see me as a very lazy guy!! isn’t the name of his website!?!”.

Anyway, if you read this article, then it’s because I think you will pick the first idea. I’m I wrong? 🙂

Where can you download it?

You can download the excel sheet on my github repo.

(If you want your own excel sheet, the code is also available at the end of this post)

Thanks to Rafi for his 64 bits version!

How does it look like?

This is how it looks in Excel. You can start the timer by clicking on the start button or CTRL + l (like lazysnail) or start manually the macro named “DoNotSleep”.

To stop the timer, you have to press any key.

Do you have the VBA code?

If you are curious and want to build your own Excel, let’s copy the following VBA code:

Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_RIGHTDOWN As LongPtr = &H8
Public Const MOUSEEVENTF_RIGHTUP As LongPtr = &H10

Sub DoNotSleepPlease()

Dim i As Integer

For i = 1 To 9999
    'For Info, number of iteration
    'Cells(1, 1) = i
    If Cells(3, 5) = "" Then
        SetCursorPos 200, 200 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        
        SetCursorPos 300, 300 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        SetCursorPos 400, 400 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
        
        SetCursorPos 500, 500 'x and y position
        mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
        mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
        WaitPlease
    Else
        Exit For
    End If
    Next i
End Sub


Sub WaitPlease()
    Dim sngWaitEnd As Single
    sngWaitEnd = Timer + 5
    Do
      DoEvents
      Cells(3, 3).Value = Timer
    Loop Until Timer >= sngWaitEnd
End Sub


11 Comments

  1. Edouard

    Arnaud, this is amazing. I have no need of such tool as I’m always moving my cursor anyway… but might send it to a few friends!

    Reply
  2. Emelia Johansson

    I have issues getting this to work in my 64 bit system. I get the following error code. Could you possible change the script to work for both 32 and 64?

    Compile error:
    The code in this project must be updated for use on 64-bit systems.
    Please review and update Declare statements and then mark them with the PtrSafe attribute.

    Reply
    • Rafi

      Hi Emelia,

      replace the first 6 lines of code with the code I pasted down below. I had the same issue and now its gone.

      CODE FOR 64bit:

      Public Declare PtrSafe Function SetCursorPos Lib “user32” (ByVal x As LongPtr, ByVal y As LongPtr) As LongPtr

      Public Declare PtrSafe Sub mouse_event Lib “user32” (ByVal dwFlags As LongPtr, ByVal dx As LongPtr, ByVal dy As LongPtr, ByVal cButtons As LongPtr, ByVal dwExtraInfo As LongPtr)

      Public Const MOUSEEVENTF_LEFTDOWN = &H2

      Public Const MOUSEEVENTF_LEFTUP = &H4

      Public Const MOUSEEVENTF_RIGHTDOWN As LongPtr = &H8

      Public Const MOUSEEVENTF_RIGHTUP As LongPtr = &H10

      Reply
      • Arnaud Gastelblum

        Thank you Rafi, I did a change in the post and uploaded a new version! 🙂

        Reply
        • gsti

          don’t work on my computer.. 🙁

          The 1st issue, when I try to use the code paste, is :
          -> “Public Const MOUSEEVENTF_xxxxx = &Hx” is in red by VBA

          The 2nd issue, when I replace “Public Const MOUSEEVENTF_xxxxxx = &Hx” by “Public MOUSEEVENTF_xxxxxx As LongPtr”,
          is :
          “Compile error:
          Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules”

          I’m not a chief in VBA.. Could you help me please ??

          Many thanksss

          Reply
  3. Anonymous

    Hi,May I ask why the program can stop if we press any key? Thanks!

    Reply
  4. Anonymous

    how can you set a predetermined amount of minutes to end

    Reply
  5. Anonymous

    code does not work.

    Lines 3,4,5, and 6 are in red and throwing out errors

    Reply
  6. Ben

    For those looking to run this within VBA in Excel, etc., you need to create a new module rather than into the Sheet or Workbook.

    Reply
  7. Paul

    I get the following:

    Run-time error 53
    File not found: user32

    What could be the issue here?

    Reply
  8. W

    Hi! Can anyone tell me what to tweak in the code to make it so the cursor moves every 3 or 4 minutes instead of every couple of seconds? Thanks 🙂

    Reply

Submit a Comment

Stay Up To Date!
Join the newsletter

Arnaud GASTELBLUM