Results 1 to 9 of 9
  1. #1
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419

    Access VBA to check if Excel file is open

    not one that is saved in a directory that is open, but one that is open as new, Book1, Book2, etc.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If you just need to know if the Excel app is open in order to establish an object variable in VBA, consider:

    Code:
    ' Establish an EXCEL application object
     On Error Resume Next
     Set xlx = GetObject(, "Excel.Application")
     If Err.Number <> 0 Then
           Set xlx = CreateObject("Excel.Application")
           blnEXCEL = True
     End If
     Err.Clear
     On Error GoTo 0
    Also review https://stackoverflow.com/questions/...s-already-open
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    I didn’t follow how that would know if an excel file is open. The answer in stack overflow seems to have answer it in a case when the file is saved in a directory.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    The first example is found code. I just know it works in my procedure. If Excel is already open, determines which method to use for setting the object variable.

    If a workbook has not been saved to folder, then it cannot be an 'opened' file, it's just a new document in progress.

    Exactly what do you need to accomplish?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Here's a function to see if some "program" is running. There is also a test routine to try it(below).

    Code:
    ' Procedure : IsExeRunning
    ' Author    :  James Barash(AccessD)
    ' Created   : 3/17/2009
    ' Purpose   : To determine if a specific program is currently running.
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs:  The name of the program
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Public Function IsExeRunning(strExeName As String) As Boolean
          Dim objProcesses As Object, objProcess As Object
    10      IsExeRunning = False
    20      Set objProcesses = GetObject("winmgmts://" & Environ$("ComputerName") _
           & "/root/cimv2").ExecQuery("select * from Win32_Process")
    30      If Not objProcesses Is Nothing Then
    40        For Each objProcess In objProcesses
    50          If objProcess.name = strExeName Then
    60              IsExeRunning = True
    70              Exit For
    80          End If
    90        Next
    100       Set objProcess = Nothing
    110       Set objProcesses = Nothing
    120     End If
    End Function
    Test routine to see if EXcel is running on my machine

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : jtestit
    ' Author    : Jack
    ' Created   : 3/17/2009
    ' Purpose   : Test routine to ensure IsEXERunning function is working
    '---------------------------------------------------------------------------------------
    ' Last Modified:
    '
    ' Inputs: N/A
    ' Dependency: N/A
    '------------------------------------------------------------------------------
    '
    Sub jtestit()
          Dim smyEXE As String
    
    10    smyEXE = "excel.exe"  '  "msaccess.exe"
    20    Debug.Print "Is " & smyEXE & " running: " & IsExeRunning(smyEXE)
    End Sub

  6. #6
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Thanks for the code and example. But I think is not right in catching it, I have Excel open and isExerunning is False. Didn't go into the loop.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    ????
    I just set this up on my PC

    Code:
    Sub jtestit()
        Dim j As Integer
        Dim smyEXE(5) As String
    10  smyEXE(0) = "msaccess.exe"
    20  smyEXE(1) = "firefox.exe"
    30  smyEXE(2) = "i_view32.exe"
    40  smyEXE(3) = "excel.exe"
    50  smyEXE(4) = "explore.exe"
    60  smyEXE(5) = "greenshot.exe"
    70  For j = 0 To 5
    80      Debug.Print "Is " & smyEXE(j) & " running: " & IsExeRunning(smyEXE(j))
    90  Next j
    End Sub
    First run I did not have excel running, I started excel and ran routine again
    With these results
    Code:
    Is msaccess.exe running: True
    Is firefox.exe running: True
    Is i_view32.exe running: False
    Is excel.exe running: False
    Is explore.exe running: False
    Is greenshot.exe running: True
    
    'Started Excel here
    
    Is msaccess.exe running: True
    Is firefox.exe running: True
    Is i_view32.exe running: False
    Is excel.exe running: True
    Is explore.exe running: False
    Is greenshot.exe running: True
    I tried this with other programs to check and it was consistent with programs I started/stopped.

    Is msaccess.exe running: True
    Is firefox.exe running: False
    Is i_view32.exe running: False
    Is excel.exe running: False
    Is Integrator.exe running: True
    Is TDM.exe running: False

    Is msaccess.exe running: True
    Is firefox.exe running: False
    Is i_view32.exe running: False
    Is excel.exe running: False
    Is Integrator.exe running: True
    Is TDM.exe running: True

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    not one that is saved in a directory that is open, but one that is open as new, Book1, Book2, etc.
    If I understand what you are asking, you can't. You can, as the others have shown you, determine whether Excel is running or not, but if the "file" excel is using is new, then it doesn't actually exist until you save it, so no file is actually "open".

    But what would be the point of doing that? What is it you are trying to determine?

  9. #9
    johnseito is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2013
    Posts
    419
    Don't have other excel open or running while the program is running that's using Excel. Otherwise, it will corrupt the program.


    tested the code again and it work - maybe a typo earlier. Thanks and Best !

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2017, 07:31 AM
  2. Replies: 6
    Last Post: 02-07-2013, 03:15 PM
  3. Replies: 5
    Last Post: 10-15-2012, 11:10 PM
  4. Open Excel .xlsx file from Access
    By Bigmix in forum Programming
    Replies: 6
    Last Post: 02-28-2012, 09:55 AM
  5. Open excel file in access
    By shanky365 in forum Access
    Replies: 1
    Last Post: 09-11-2011, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums