Get handle for excel file that is already open?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
KenHadley
Junior Coder
Posts: 28
Joined: Thu Jul 14, 2011 5:02 pm

Get handle for excel file that is already open?

Post by KenHadley » Fri May 20, 2016 8:09 pm

I am using XLOpen in a macro, but now have a situation where the desired file is already open for other reasons. In this case, MS opens a second, read-only copy. Since my macro is wanting to write to the open file, this does not work.

Is there a way to either use XLOpen with a file that is already open, or is there a way to get the handle for an already-open Excel file so that I can pass that handle to XLSetCell?

Thank you,

Ken

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Get handle for excel file that is already open?

Post by hagchr » Sat May 21, 2016 11:52 am

Now sure if you can connect with the open file with the MS functions, but you can always use VBS to connect and work with it, simple example below (when there is one instance of excel open, possible with one or more open workbooks).

Code: Select all

VBSTART

Sub toXL(file,sheet,cell,val)
    Set objExcel = GetObject( ,"Excel.Application")
    objExcel.workbooks(file).sheets(sheet).range(cell)=val
End Sub

VBEND

Let>file=test.xlsx
Let>sheet=Sheet1
Let>cell=a2
Let>val=123
VBRun>toXL,file,sheet,cell,val

VBRun>toXL,test.xlsx,Sheet2,b33,28

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts