Jump to content

Recommended Posts

Posted (edited)

@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
Posted

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

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted

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:

  Reveal hidden contents

 

Posted
  On 2/27/2023 at 8:07 PM, MichaelCrawley said:

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

Expand  

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

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted

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

  On 2/27/2023 at 8:07 PM, water said:

Excel - Example Scripts - Wiki

Expand  

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.

  • Solution
Posted

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:

  Reveal hidden contents

 

Posted

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:

  Reveal hidden contents

 

  • 2 weeks later...
Posted

:)

My UDFs and Tutorials:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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