How to get an object reference to an ALREADY open excel spreadshee

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to manipulate an already open excel spreadsheet using VBScript. My
problem is that I can't seem to be able to get an object reference to excel
if it's open already. I was able to open an excel workbook and add a sheet,
but I wasn't able to connect to a spreadsheet it if it was already open.
Anybody knows how to do it in VBScript?
E.g: manually open MS Excel and type something in Sheet1. Then when you'd
run this VBScript that I need, it would connect to Sheet1 and would display
the content of it in a message box.

Thanks a lot!
JP
 
have you tried using GetObject instead of CreateObject?

Assuming you are NOT talking about Excel VBA when you say VBScript
 
JP,

Try using

Set xlApp = GetObject(,"Excel.Application")

rather than

Set xlApp = CreateObject("Excel.Application")

or better still, use both

Set xlApp = Nothing
On Error Resum Next
Set xlApp = GetObject(","Excel.Application")
On Error Goto 0
If xlApp Is Nothing
'no instance of Excel running, so create one
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Fatal error"
Exit Sub
End If
End If
 
Thank You!
JP

Tom Ogilvy said:
have you tried using GetObject instead of CreateObject?

Assuming you are NOT talking about Excel VBA when you say VBScript
 
Thank You very much! Great help!
JP

Bob Phillips said:
JP,

Try using

Set xlApp = GetObject(,"Excel.Application")

rather than

Set xlApp = CreateObject("Excel.Application")

or better still, use both

Set xlApp = Nothing
On Error Resum Next
Set xlApp = GetObject(","Excel.Application")
On Error Goto 0
If xlApp Is Nothing
'no instance of Excel running, so create one
Set xlApp = CreateObject("Excel.Application")
If xlApp Is Nothing Then
MsgBox "Fatal error"
Exit Sub
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top