Jump to content

Recommended Posts

Posted

I'm wondering if there is a way to attach to an open excel object. I've been trying to do this:

$test=ObjGet("", "Excel.Application")
$test.Workbook.Close

But I get an error on the close command. I've also tried it without Workbook and it still errors.

 

What I'm really trying to do is attach to a specific instance of excel (based on a window name or file name or something) and then save it and close it. Anyone have suggestions?

Posted (edited)

I think I might have something that can help. I have a script that runs a spreadsheet in the background and when it's done it gets saved. I had to check for the object to make it work.

;Setting up the workbook
$oExcel = ObjCreate("Excel.Application")
$oBook = $oExcel.Workbooks.Open(@ScriptDir & "\..\Spreadsheet.xlsx")
$oExcel.Visible = False ;Invisible Excel!


;Stuff

;Here is where I checked for the object before saving and closing.
If IsObj($oExcel) Then _Excel_BookSave($oBook)
If @error Then MsgBox(64, "", "Unable to save  spreadsheet.")

If IsObj($oExcel) Then _Excel_Close($oExcel, True, True)
If @error Then MsgBox(64, "", "Unable to close  spreadsheet.")

Let the fun begin!

Edited by Bearpocalypse
Posted (edited)

I'm trying to avoid closing other instances so I'm really looking to attach to a window or filename. This is for a calendar for vacation days that I'm working on. I want to make sure that if they open the file, that they don't already have it open, but I don't want to close any other instances of Excel. So if someone was working on an excel file and then remembered they needed to put in their vacation days, it wouldn't close the other excel file on them. This is how I have my startup on the script:

If WinExists("[CLASS:XLMAIN]","Vacation Calendar") Then
    $test=ObjGet("", "Excel.Application")
    $reopen=MsgBox(4,"Oops..", "It appears you already have the speadsheet open. Would you like to save and reopen the file?")
    If $reopen=6 Then
        MsgBox(0,"Oops..", "Trying to save... but I dont know how...")
                $test.Workbook.Close
    Else
        Exit
    EndIf
    $test.Workbook.Close
EndIf
Edited by Jewtus
Posted

Please have a look at the Excel UDF in combination with ProcessExist.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)
  On 1/6/2015 at 7:09 PM, water said:

Please have a look at the Excel UDF in combination with ProcessExist.

I was actually trying to use _Excel_BookAttach, but I keep getting an error on the line:

$oWorkbook = ObjGet("", $sCLSID_Workbook, $iCount + 1)

I'm assuming it relates to the $sCLSID_Workbook variable, but I don't know exactly what is going on. The error description is "Unknown name".

I've tried using Filename, filepath, and title and I get the same result on all of them.

EDIT:

Scratch that. I looked at my process list and there were about a hundred instances of EXCEL.exe so I closed them all and it worked without issue.

Edited by Jewtus
Posted
  On 1/6/2015 at 7:43 PM, Jewtus said:

Scratch that. I looked at my process list and there were about a hundred instances of EXCEL.exe so I closed them all and it worked without issue.

 

Oh how I don't miss that when I was working on my thing.

Posted
  On 1/6/2015 at 9:22 PM, Bearpocalypse said:

Oh how I don't miss that when I was working on my thing.

What are you talking about :huh:

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

When you run _Excel_BookList you get a list of workbooks in all running Excel instances. Loop through the array and select the needed workbook, then use _Excel_BookAttach to connect to this workbooks and save/close them if needed.

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

I ended up going with this:

If ProcessExists('EXCEL.EXE') Then
    If WinExists("[CLASS:XLMAIN]","Vacation Calendar") Then
        $reopen=MsgBox(4,"Oops..", "It appears you already have the speadsheet open. Would you like to save and reopen the file?")
        If $reopen=6 Then
            $oExcel2=_Excel_BookAttach($filePath,'filepath')
            $oExcel2.Save
            $oExcel2.close
        Else
            WinActivate("[CLASS:XLMAIN]","Vacation Calendar")
            Exit
        EndIf
    EndIf
EndIf

Only thing I need to figure out is how to close that excel instance... What it does now is close the workbook, but leaves that instance of excel open.

Posted

Use something like this:

If ProcessExists('EXCEL.EXE') Then
    If WinExists("[CLASS:XLMAIN]","Vacation Calendar") Then
        $reopen=MsgBox(4,"Oops..", "It appears you already have the speadsheet open. Would you like to save and reopen the file?")
        If $reopen=6 Then
            $oWorkbook=_Excel_BookAttach($filePath,'filepath')
            $oWorkbook.Save()
            $oWorkbook.Close()
            _Excel_Close($oWorkbook.Parent)
        Else
            WinActivate("[CLASS:XLMAIN]","Vacation Calendar")
            Exit
        EndIf
    EndIf
EndI

What your script still needs is some type of error checking after each action with Excel.

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...