Jump to content
Sign in to follow this  
egalvez

I don't want two instances of Excel to open...

Recommended Posts

Hi. I want to use AutoIt3 to open a Excel to do list spreadsheet every hour. That's easy enough to set up in conjunction with another program that will open an exe periodically, like KeyText or Peter's Ultimate Alarm Clock. My problem is that if Excel is already open when the to do list spreadsheet opens each hour, two instances of Excel will now be open and I'll get a File in use - locked for editing error. :lmao: I don't want two instances of Excel to open. If Excel is already open when the hourly time comes, I want the to do list spreadsheet to simply open as another workbook in Excel. I know how to use AutoIt to test what apps are open, but how can I tell AutoIt that if Excel is already open, don't open Excel again, just open my to do list spreadsheet? Thank you.

Share this post


Link to post
Share on other sites

If you know how to check for an open Excel window, the rest is easy...

Just set it as the active window, then send Ctrl+O for open, input the path of the spreadsheet and hit {ENTER}...

You may want to block keyboard/mouse input during that time as it may interfere with the keyboard shortcuts your script is sending...

If WinExists("Excel") Then
    BlockInput(1);Blocks user input
    WinActivate("Excel");Activates the Excel window
    Send("^o");Sends Ctrl+O command to display the open file dialog
    WinWaitActive("Open");Wait for the open file dialog to show
    Send($path);Input the filename (with path)
    Send("{ENTER}");Simulates the Open button press
    BlockInput(0);Releases block on user input

EndIf

Hope this helps

Matt

Share this post


Link to post
Share on other sites

Hi. I figured it could be done by sending keys, but I was hoping that somehow it could be done transparently without the interruption of having to send keys. Thank you, anyway.

Share this post


Link to post
Share on other sites

How about this:

You can check for text in an window titled Excel; something like:

if WinExists("Excel", "MyToDoList.xls") then
 ; Don't open to do list; it's already open.
 ; Send an Alt W for Window and then the number of the document
 ; Use Window Spy and you'll determine the filenames of all the documents can each be ascertained as a window number
else
 ;send some hotkeys to Excel to open that bad puppy!
 ;send a Ctrl O and a path and an enter.
 ;if you call a couple of flashes (the quick Open dialog) an interruption then you should also consider the to do list popping up unexpectedly an interruption too.
endif

J


If I am too verbose, just say so. You don't need to run on and on.

Share this post


Link to post
Share on other sites

Also, keep in mind that the office assistant can play hob with scripts. I like to disable it with this handy function:

; Kill the Office Assistant!!!  This is a function to disable it temporarialy.  
; It will be re-enabled on any normal program exit if it was enabled previously.
$CurrentAssist = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Assistant", "AsstState")
$killassist = Hex(38, 8)
If Not $CurrentAssist = $killassist Then
   $restoreAssit = Dec($CurrentAssist)
   $restoreAssit = Hex($restoreAssit, 8)
   RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Assistant", "AsstState", "REG_DWORD", $killassist)
   $office = 1
EndIf

And then re-enable it at the end of the script with:

If $office = 1 Then
   RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Assistant", "AsstState", "REG_DWORD", $restoreAssist)
EndIf

I always hated that dorky paper-clip anyway.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

Tested on WinXP and this doesn't open another instance of Excel.

Use @ComSpec to open your spreadsheet.

Run(@ComSpec & ' /c "' & @DesktopDir & '\test.xls"','',@SW_HIDE)

Or Use

_ShellExecuteDllCall('"' & @DesktopDir & '\test.xls"','','',0)

Func _ShellExecuteDllCall($sCmd, $sVerb, $sArg, $sFolder)
  Local $aRet
 
  $aRet = DllCall("shell32.dll", "long", "ShellExecute", "hwnd", 0, "string", $sVerb, _
        "string", $sCmd, "string", $sArg, "string", $sFolder, "long", @SW_SHOWNORMAL)
  Return $aRet
 
  If $aRet[0] > 32 Then
     Return 1;All Ok
  Else
     Return 0;Problems
  EndIf
EndFunc
Edited by bshoenhair

Share this post


Link to post
Share on other sites

Thank you all for the suggestions. I've incorporated bshoenhair's as follows:

Run(@ComSpec & ' /c "c:\data\eric\2do.xls"','',@SW_HIDE)
and it works great. :)

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...