Jump to content
Sign in to follow this  
LurchMan

Excel Help

Recommended Posts

LurchMan

Hey everyone -

I'm working on a simple excel script that's going to start Excel, run a macro and wait for the next instance of excel to start. When the next instance starts I want it to close the first one and exit the script.

What I have:

#include <excel.au3>
Opt ("WinTitleMatchMode", 2)

$oExcel = _ExcelBookOpen (@ScriptDir & "\macro.xls", 0)
$oExcel.Run ("Start")

While 1
    Sleep (100)
    If BitAND(WinGetState("Microsoft Excel"), 2) Then
        _ExcelBookClose ($oExcel, 0, 0)
        Exit
    EndIf
WEnd

This works, except it closes the entire excel program (both workbooks) - I only want it to close the first one.

Thanks for any and all help.


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
Share on other sites
reb

Hi

instead of

If BitAND(WinGetState("Microsoft Excel"), 2) Then

maybe

If BitAND(WinGetState("Microsoft Excel - macro.xls"), 2) Then

not tested

REB

Edit put in first book name

Edited by reb

MEASURE TWICE - CUT ONCE

Share this post


Link to post
Share on other sites
zfisherdrums

LurchMan,

Untested and possibly brittle, but should work in theory. Let me know if it doesn't.

#include <excel.au3>
Opt ("WinTitleMatchMode", 2)

Func GetNumExcelProcess()
    $_aProcesses = ProcessList( "Excel.exe" )
    return $_aProcesses[0][0]
EndFunc

$oExcel = _ExcelBookOpen (@ScriptDir & "\macro.xls", 0)
$oExcel.Run ("Start")

; Establish a baseline number of known Excel processes
$iNumExcelProcess = GetNumExcelProcess()

; Periodically test for changes in the number of Excel processes
While ( $iNumExcelProcess = GetNumExcelProcess() )
    Sleep (100)
WEnd

_ExcelBookClose ($oExcel, 0, 0)
Exit

Zach...

Share this post


Link to post
Share on other sites
LurchMan

Reb - Thank you for your suggestion, I've tried that also but it doesn't close just that window still.

Zach - Thank you for your help I will test it later. It looks like it should work to me.


Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Share this post


Link to post
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
Sign in to follow this  

×