Jump to content

Force Close a Excel


Go to solution Solved by water,

Recommended Posts

@moderators if I posted in the wrong place I do apoligize.   

I am not sure if the format of my script is correct or not I am kind of new autoIT so excuse my format errors. I essientially crate the same  script to close chrome.exe and it works  w\o issue.  I wanted make sure the command work and if just copy TASKKILL /F /IM excel.exe /T in a command prompts it work w\o issue.   I am a NOOB at this so I am sure I am getting stuff wrong.

If you could explain where I am going wrong it would be very helpful. 

So these are snippets of my scripts:

 

Global $exitExcel
        
        ;Button name "Close Excel"
        Global $idButton_closeExcel = GUICtrlCreateButton("Close Excel", 10, 90, 85, 25)
        GUICtrlSetBkColor(-1, 0x99B4D1)


         ;closes excel
         Case $idButton_closeExcel
         $exitExcel()
         
         ;Function to kill excel
            Func exitExcel()
            $runString = StringFormat("TASKKILL /F /IM excel.exe /T")
            $run = Run($runString, "", @SW_HIDE)
         
         EndFunc


 

 


 

 

 

 

 

Edited by MichaelCrawley
clerification
Link to comment
Share on other sites

Hi @MichaelCrawley,

what exactly is it what you want? Simply close the process "excel.exe"? Or do you want to close it by a button on your GUI?

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

I suggest to use the Excel UDF to gracefully close Excel.
By killing a process you might create problems with unsaved files remaining in an undefined state etc.
This is true for every application.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Just now, MichaelCrawley said:

Correct yes I am using a gui and the botton should close excel

So the script above is only a snippet or your complete script? Like @water suggest, include the Excel UDF and use the _Excel_Close() function.

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

Definitely check out the Excel UDF, from water's signature: 

2 minutes ago, water said:

Excel - Example Scripts - Wiki

And: 

However, to just close Excel immediately, look at using ProcessClose(), WinClose(), or WinKill().

Also for your script, there's no reason to use StringFormat, you're not passing any formatting into it, you can just do this directly:

; $runString = "TASKKILL /F /IM excel.exe /T" ; You can also do this, unless you're actually performing some formatting with StringFormat, there's no reason to use it.
$run = Run("TASKKILL /F /IM excel.exe /T", "", @SW_HIDE)

 

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

  • Solution

Quite easy:

#include <Excel.au3>
Global $oExcel = _Excel_Open() ; Starts a new Excel instance or connects to an already running instance
If @error Then ... ; Write an Error-Message and Exit 
_Excel_Close($oExcel, False, True) ; Does not save any open workbooks and closes the Excel instance
If @error Then ... ; Write an Error-Message and Exit

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

No, you need all the statements I have posted above.

#include <Excel.au3>                   ; Copies the Excel UDF code to your script
Global $oExcel = _Excel_Open()         ; Starts a new Excel instance or connects to an already running instance
If @error Then ...                     ; Here you insert the code to execute when Excel is not installed on the machine or other erorrs occure
_Excel_Close($oExcel, False, True)     ; Does not save any open workbooks and closes the Excel instance
If @error Then ...                     ; Here you insert the code to execute when there is an error when closing Excel

#include should be at the top of your script, the rest can be put into a function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • 2 weeks later...

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...