locked
Can an Excel VBA macro determine if the excel process running it is running in the background? RRS feed

  • Question

  • I have an Excel macro which I sometimes use a scheduled task to run in the background. I'd like the macro processing to vary slightly when run as a background task. How can the VBA macro code determine if its process is running in the background or foreground?

    cw

    Friday, October 9, 2015 2:04 PM

Answers

  • I don't know of a way to detect if the task is running as background task or foreground task, but when the task is executed as background task, the window of the task is not visible, right?

    Andreas.

    Option Explicit
    
    Private Const GWL_STYLE = (-16)
    Private Const WS_VISIBLE = &H10000000
    #If Win64 Then
    Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
    #Else
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, ByVal nIndex As Long) As Long
    #End If
    
    Sub Test()
      Debug.Print IsWindowVisible(Application.hwnd)
    End Sub
    
    #If Win64 Then
    Function IsWindowVisible(ByVal hwnd As LongPtr) As Boolean
    #Else
    Function IsWindowVisible(ByVal hwnd As Long) As Boolean
    #End If
      Const WS_VISIBLE = &H10000000
      Dim lngStyle As Long
      lngStyle = GetWindowLong(hwnd, GWL_STYLE)
      IsWindowVisible = ((lngStyle And WS_VISIBLE) = WS_VISIBLE)
    End Function
    

    Saturday, October 10, 2015 7:21 AM

All replies

  • determine if its process is running in the background or foreground

    It depends on what you mean by that.

    There is no "background" or "foreground" feature to run a macro in Excel, a macro is running or not.

    It's enough when you macro calls "DoEvents" from time to time, so other tasks can work (Excel update the screen, process your inputs, etc.)

    Andreas.

    Friday, October 9, 2015 4:05 PM
  • Thanks Andreas.  I already use DoEvents.  So, let me restate.  I'm running Win-8.1 on a tablet if that matters.  The task within Task Scheduler is configured to "Run whether user is logged on or not."  When the task is running, it shows up within Task Manager under the group "Background Processes" under the name "Microsoft Excel."  There is no Excel icon displayed on the Task Bar, and no other indication that Excel is running except for progress indications in a log file that the macro writes to the User's desktop.  The VBA macro attempts to display a User Form (using Load formname and formname.show vbmodeless) for several seconds to allow the user to override some options by clicking a button.  If the user clicks a button on the User Form, or fails to make any selection after 15 seconds, and then the macro takes down the Form (using unload formname) and continues on.  This all works just fine when Excel is running as an App in the foreground (i.e. listed under the "Apps" section within Task Manager, and with an icon visible on the Task Bar.) HOWEVER, if/when Excel is listed within the "Background Processes" section of Task Manager, then the Excel process seems to hang forever at the point in the macro code where it displays the User Form.  Since it's running as a background process, the only way I can break-in to do further debug is to "End Task."  ... not very productive.

    What I'd like to be able to do is either: (1) Adapt the code to make it work when running in the background the same way as it already does when running in the foreground.  Or (2) Allow it to simply not bother displaying the User Form when running in the background and avoid the dead-lock condition that prevents it from continuing.

    Any ideas on either option?

    Friday, October 9, 2015 7:44 PM
  • I don't know of a way to detect if the task is running as background task or foreground task, but when the task is executed as background task, the window of the task is not visible, right?

    Andreas.

    Option Explicit
    
    Private Const GWL_STYLE = (-16)
    Private Const WS_VISIBLE = &H10000000
    #If Win64 Then
    Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As LongPtr, ByVal nIndex As Long) As Long
    #Else
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, ByVal nIndex As Long) As Long
    #End If
    
    Sub Test()
      Debug.Print IsWindowVisible(Application.hwnd)
    End Sub
    
    #If Win64 Then
    Function IsWindowVisible(ByVal hwnd As LongPtr) As Boolean
    #Else
    Function IsWindowVisible(ByVal hwnd As Long) As Boolean
    #End If
      Const WS_VISIBLE = &H10000000
      Dim lngStyle As Long
      lngStyle = GetWindowLong(hwnd, GWL_STYLE)
      IsWindowVisible = ((lngStyle And WS_VISIBLE) = WS_VISIBLE)
    End Function
    

    Saturday, October 10, 2015 7:21 AM
  • Thanks.  That's something I could try ... after I return from vacation.  I'll let you know when I'm back.
    Saturday, October 10, 2015 3:36 PM
  • Hi CharlieWright,

    This is the forum to discuss questions and feedback for Microsoft Excel, I'll move your question to the MSDN forum for Excel Developer:

    http://social.msdn.microsoft.com/Forums/en-US/home?forum=exceldev&filter=alltypes&sort=lastpostdesc

    The reason why we recommend posting appropriately is you will get the most qualified pool of respondents, and other partners who read the forums regularly can either share their knowledge or learn from your interaction with us. Thank you for your understanding.

    Regards,

    Emi Zhang
    TechNet Community Support


    Please mark the reply as an answer if you find it is helpful.

    If you have feedback for TechNet Support, contact tnmff@microsoft.com.
    Monday, October 12, 2015 3:07 AM
  • There is, of course a background and foreground feature of running a macro.  bullshit it is running or not.  you must not understand the concept of shell, or console, or batch or any number of designators that denote where your process is.  Excel is the same way.  Do you think a spreadsheet is dumb as to whether it has a screen or is running form then command line?  
    Thursday, August 1, 2019 11:17 PM