Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2020
    Posts
    21

    How close all open ms excel processes using dba in ms access

    EXPORTING DATA FROM MS ACCESS TO MS EXCEL FAILED DURING THE EXECUTION.
    THE EXCEL PROCESS STILL STAYS OPEN.


    NOW I NEED A COMMAND THAT WILL CLOSE ALL OPEN MS EXCEL PROCESSES USING DBA IN MS ACCESS.

    I got messages from the windows operation system, that I need to close these files.
    I did it closeing one by one with the task manager.

    Now I am looking for DBA commands that will close all open MS-EXCEL processes while I am still in MS-ACCESS.

    Thanks a lot for Your Time and Help.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    No need to shout?
    Run a script.
    This one kills all chrome processes, and then starts just the one. Adjust to suit.
    Code:
    ' ProcessKillLocal.vbs
    ' Sample VBScript to kill a program
    ' Author Guy Thomas http://computerperformance.co.uk/
    ' Version 2.7 - December 2010
    ' ------------------------ -------------------------------' 
    Option Explicit
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strProcessKill 
    strComputer = "."
    strProcessKill = "'chrome.exe'" 
    
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 
    
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = " & strProcessKill )
    For Each objProcess in colProcess
    objProcess.Terminate()
    Next 
    WSCript.Echo "Just killed process " & strProcessKill
    WSCript.echo "Now starting Chrome....."
    CreateObject("WScript.Shell").Run("Chrome.exe") 'C:\Program Files\Google\Chrome\Application\chrome.exe'")
    WScript.Quit 
    ' End of WMI Example of a Kill Process
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    Sep 2020
    Posts
    21
    Hello, Welshgasman !

    Thanks a lot for your quick reply !

    I have next three questions - :

    1.
    I tryed to suit some data in your script as I understand it, as follows-:

    "'crome.exe'" changed to "'excel.exe'"

    I am not sure if it is right for Microsoft Excel (32 bits).

    2.
    Does it need any addition of more refferences in my refference list.

    Why I ask this question ? Becouse, It does not recognize the next string -:

    objProcess.Terminate()

    3.
    Is it general for any computer - :

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" _
    & strComputer & "\root\cimv2")

    Thanks once more for Your Time and Help.

    With best regards

    Quote Originally Posted by Welshgasman View Post
    No need to shout?
    Run a script.
    This one kills all chrome processes, and then starts just the one. Adjust to suit.
    Code:
    ' ProcessKillLocal.vbs
    ' Sample VBScript to kill a program
    ' Author Guy Thomas http://computerperformance.co.uk/
    ' Version 2.7 - December 2010
    ' ------------------------ -------------------------------' 
    Option Explicit
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strProcessKill 
    strComputer = "."
    strProcessKill = "'excel.exe'" 
    
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _ 
    & strComputer & "\root\cimv2") 
    
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = " & strProcessKill )
    For Each objProcess in colProcess
    objProcess.Terminate()
    Next 
    WSCript.Echo "Just killed process " & strProcessKill
    WSCript.echo "Now starting Chrome....."
    CreateObject("WScript.Shell").Run("Chrome.exe") 'C:\Program Files\Google\Chrome\Application\chrome.exe'")
    WScript.Quit 
    ' End of WMI Example of a Kill Process

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    The code is vbs NOT vba, and is a script that you would run with the Shell command?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    Here is a VBA version.
    I had to add the Error Resume, as although it goes through the loop several times, it errors on the second try saying 'does not exist'?

    Code:
    Sub KillProcess(strProcess As String)
    ' ProcessKillLocal.vbs
    ' Sample VBScript to kill a program
    ' Author Guy Thomas http://computerperformance.co.uk/
    ' Version 2.7 - December 2010
    ' Modified for VBA Paul Steel 02/07/2022
    ' ------------------------ -------------------------------'
    Dim objWMIService As Object, objProcess As Object, colProcess As Object
    Dim strComputer As String, strProcessKill As String
    strComputer = "."
    strProcessKill = "'" & strProcess & "'"
    
    
    
    
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
    
    
    
    
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = " & strProcessKill)
    On Error Resume Next
    For Each objProcess In colProcess
        objProcess.Terminate
    Next
    MsgBox "Just killed process " & strProcessKill
    
    
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    Are you setting the Excel object to Nothing in your code?
    That should close the running process?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Join Date
    Sep 2020
    Posts
    21
    Hello, Welshgasman !

    There are no problems any more. Your solution solved the problem !

    It works very well !

    Thank you very much for your quick help and solution !
    Last edited by NeedSomeHelp; 07-02-2022 at 09:27 PM.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    No, I only use a function if I need to return a value.
    So a sub will work fine. Plus I only added the parameter to make it more flexible? Now you could kill any process with it, not just Excel.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Join Date
    Sep 2020
    Posts
    21
    Hello, Welshgasman !

    Thank You for the remark,

    Yes, I understand that. Defining as a Function does not fit here from the reason you mentioned above (there is no "need to return a value").

    Earlier, I defined this procedure as "Sub" in General Module. And tried to execute it calling "RunCode" within Macro.
    I changed the definition from "Sub" to "Public Sub" in the same General Module and tried to execute it using "RunCode" within Macro, but it still did not work.

    When I defined this procedure as "Function" in the same General Module and tried to execute it using "RunCode" within Macro, it worked succesfuly.

    Now, I would define this procedure as "Sub" in some Form's local Module and call this procedure with help of some Form Controls.
    Sure I will try it later! I think that it will work in two ways as a "Sub" in local Form Module and as a "Public Sub" in General Module.
    .
    Thank YOU !

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,985
    You are both correct.
    Whilst the code that was provided in post #5 works perfectly as a sub in a form or standard module, macros can only run code from functions.

    In fact any sub will still work from any location if changed to a function. However you have to use a function where a value is returned.
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  11. #11
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,980
    I only ever used macros for batch functions. and took the time to learn VBA.

    Much. much. much easier to test.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 6
    Last Post: 07-10-2019, 07:34 PM
  2. Excel open/close with access 2016
    By gg80 in forum Import/Export Data
    Replies: 20
    Last Post: 01-18-2019, 11:59 PM
  3. Why does Excel close when I open Access.
    By Bmadigan2000 in forum Access
    Replies: 1
    Last Post: 12-23-2013, 11:11 AM
  4. Using Access to open and close Excel
    By murfeezlaw in forum Programming
    Replies: 4
    Last Post: 04-04-2012, 07:58 PM
  5. VBA to open excel, import on close of excel
    By bdaniel in forum Programming
    Replies: 0
    Last Post: 03-20-2010, 02:45 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