• Is Excel Running? Is a given workbook open?

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Is Excel Running? Is a given workbook open?

    Author
    Topic
    #458213

    Is there a way via VBA to determine 1) if Excel is open and 2) if a given file is open?

    I have a process that blows up if the user has certain workbooks open.

    Thanks!

    Viewing 1 reply thread
    Author
    Replies
    • #1151477

      Is there a way via VBA to determine 1) if Excel is open and 2) if a given file is open?

      I have a process that blows up if the user has certain workbooks open.

      Thanks!

      You can use this:

      Code:
      Dim xlApp As Object
      Dim xlWbk As Object
      
      On Error Resume Next
      Set xlApp = GetObject(, "Excel.Application")
      If xlApp Is Nothing Then
        MsgBox "Excel is not running", vbInformation
      Else
        MsgBox "Excel is running", vbInformation
      End If
      
      Set xlWbk = GetObject("C:DocsMyWorkbook.xls")
      If xlWbk Is Nothing Then
        MsgBox "Workbook is not open", vbInformation
      Else
        MsgBox "Workbook is open", vbInformation
      End If
      On Error GoTo 0
    • #1151508

      I modified your code to this

      Code:
      Function ExcelCheck2(strFileIn As String) As Boolean
      	Dim xlApp As Object
      		
      	On Error Resume Next
      	Set xlApp = GetObject(strFileIn)
      	If xlApp Is Nothing Then
      	  ExcelCheck2 = False
      	Else
      	  ExcelCheck2 = True
      	End If
      
      End Function

      but it never returns a FALSE….

      • #1151512

        I even tried this

        Code:
        Function ExcelCheck2(strFileIn As String) As Boolean
        On Error Resume Next
        Set xlApp = GetObject(, "Excel.Application")
        If xlApp Is Nothing Then
          MsgBox "Excel is not running", vbInformation
        Else
          MsgBox "Excel is running", vbInformation
        End If
        
        Set xlApp = GetObject(strFileIn)
        If xlApp Is Nothing Then
          MsgBox "Workbook is not open", vbInformation
          ExcelCheck2 = False
        Else
          MsgBox "Workbook is open", vbInformation
          ExcelCheck2 = True
        End If
        On Error GoTo 0
        End Function

        but never get a correct response on the workbook being open…

        • #1151533

          I apologize – GetObject will check whether Excel is running, but it will open the workbook if it wasn’t open yet.
          Try this function:

          Code:
          Function ExcelCheck(strFileIn As String) As Boolean
            Dim xlApp As Object
            Dim xlWbk As Object
          
            On Error Resume Next
            Set xlApp = GetObject(, "Excel.Application")
            If Not xlApp Is Nothing Then
          	For Each xlWbk In xlApp.Workbooks
          	  If xlWbk.FullName = strFileIn Then
          		ExcelCheck = True
          		Exit For
          	  End If
          	Next xlWbk
            End If
          End Function

          Warning: it may fail if the user has more than one instance of Excel.exe running (this is not the same as having multiple workbooks open).

    Viewing 1 reply thread
    Reply To: Is Excel Running? Is a given workbook open?

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

    Your information: