ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel Getobject error (https://www.excelbanter.com/excel-programming/271916-excel-getobject-error.html)

Jaya

Excel Getobject error
 
Hi,

i have the following code in VB.
Set loXLS = GetObject(, "Excel.Application")

Dim loXLS As Excel.Application
For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i

When a excel file is already open, this fails. And the
loxls.workbooks.count is 0. Can you tell me what could be
the reason?
-Jaya

Bill Lunney

Excel Getobject error
 
This should fail when Excel is NOT open. That's because the GetObject line
will generate an error if it can't find an instance of Excel. You're also
defining the loXLS object after calling GetObject. Try this:

Dim loXLS As Excel.Application

On Error Resume Next

Set loXLS = GetObject(, "Excel.Application")
If Err.Number 0 Then
MsgBox "Excel is not running"
End
End If

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

Regards,


Bill Lunney
www.billlunney.com

"Jaya" wrote in message
...
Hi,

i have the following code in VB.
Set loXLS = GetObject(, "Excel.Application")

Dim loXLS As Excel.Application
For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i

When a excel file is already open, this fails. And the
loxls.workbooks.count is 0. Can you tell me what could be
the reason?
-Jaya




Bill Lunney

getobject error
 
I remember using GetObject a long time ago and came up against problems when
multiple version of Excel are running. GetObject will return the first
instance it finds. If that isn't the one you're interested in there's
nothing you can do about it (as far as I know).

I used all sorts of wacky things like getting a handle to a specific
instance by way of the window caption (using the API) but ran into other
problems best not discussed here.

Bottom line is if you have only one instance of Excel open and the GetObject
method does not return it I don't know why. If there are multiple instances
open and your getting a handle to one you don't like you'll need another
approach. I can point you in the right direction if this is the case.




--

Regards,


Bill Lunney
www.billlunney.com

"jaya" wrote in message
...
I execute the code only when the excel file is open. The
getobject does not get the instance of the running excel
application. what could be the reason??
-Jaya

-----Original Message-----
This should fail when Excel is NOT open. That's because

the GetObject line
will generate an error if it can't find an instance of

Excel. You're also
defining the loXLS object after calling GetObject. Try

this:

Dim loXLS As Excel.Application

On Error Resume Next

Set loXLS = GetObject(, "Excel.Application")
If Err.Number 0 Then
MsgBox "Excel is not running"
End
End If

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

Regards,


Bill Lunney
www.billlunney.com

"Jaya" wrote in message
...
Hi,

i have the following code in VB.
Set loXLS = GetObject(, "Excel.Application")

Dim loXLS As Excel.Application
For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i

When a excel file is already open, this fails. And the
loxls.workbooks.count is 0. Can you tell me what could

be
the reason?
-Jaya



.




jaf

getobject error
 
Jaya,
Move the dim statement before the set statement.

Dim loXLS As Excel.Application
Set loXLS = GetObject("c:\path\filename.xls") ' works if filename is open.
<<<<<<<<<<<<<<<<<<<<

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

John

johnf202 at hotmail dot com


"Bill Lunney" wrote in message
...
I remember using GetObject a long time ago and came up against problems

when
multiple version of Excel are running. GetObject will return the first
instance it finds. If that isn't the one you're interested in there's
nothing you can do about it (as far as I know).

I used all sorts of wacky things like getting a handle to a specific
instance by way of the window caption (using the API) but ran into other
problems best not discussed here.

Bottom line is if you have only one instance of Excel open and the

GetObject
method does not return it I don't know why. If there are multiple

instances
open and your getting a handle to one you don't like you'll need another
approach. I can point you in the right direction if this is the case.




--

Regards,


Bill Lunney
www.billlunney.com

"jaya" wrote in message
...
I execute the code only when the excel file is open. The
getobject does not get the instance of the running excel
application. what could be the reason??
-Jaya

-----Original Message-----
This should fail when Excel is NOT open. That's because

the GetObject line
will generate an error if it can't find an instance of

Excel. You're also
defining the loXLS object after calling GetObject. Try

this:

Dim loXLS As Excel.Application

On Error Resume Next

Set loXLS = GetObject(, "Excel.Application")
If Err.Number 0 Then
MsgBox "Excel is not running"
End
End If

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

Regards,


Bill Lunney
www.billlunney.com

"Jaya" wrote in message
...
Hi,

i have the following code in VB.
Set loXLS = GetObject(, "Excel.Application")

Dim loXLS As Excel.Application
For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i

When a excel file is already open, this fails. And the
loxls.workbooks.count is 0. Can you tell me what could

be
the reason?
-Jaya


.






Jaya

getobject error
 
I solved the problem... There were two instances running.
For some reason it was not getting both the instances. I
killed one of the instance and it worked fine.
Thanks,
Jaya
-----Original Message-----
Jaya,
Move the dim statement before the set statement.

Dim loXLS As Excel.Application
Set loXLS = GetObject("c:\path\filename.xls") ' works if

filename is open.
<<<<<<<<<<<<<<<<<<<<

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

John

johnf202 at hotmail dot com


"Bill Lunney" wrote in

message
...
I remember using GetObject a long time ago and came up

against problems
when
multiple version of Excel are running. GetObject will

return the first
instance it finds. If that isn't the one you're

interested in there's
nothing you can do about it (as far as I know).

I used all sorts of wacky things like getting a handle

to a specific
instance by way of the window caption (using the API)

but ran into other
problems best not discussed here.

Bottom line is if you have only one instance of Excel

open and the
GetObject
method does not return it I don't know why. If there

are multiple
instances
open and your getting a handle to one you don't like

you'll need another
approach. I can point you in the right direction if

this is the case.




--

Regards,


Bill Lunney
www.billlunney.com

"jaya" wrote in message
...
I execute the code only when the excel file is open.

The
getobject does not get the instance of the running

excel
application. what could be the reason??
-Jaya

-----Original Message-----
This should fail when Excel is NOT open. That's

because
the GetObject line
will generate an error if it can't find an instance

of
Excel. You're also
defining the loXLS object after calling GetObject.

Try
this:

Dim loXLS As Excel.Application

On Error Resume Next

Set loXLS = GetObject(, "Excel.Application")
If Err.Number 0 Then
MsgBox "Excel is not running"
End
End If

For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName = psFile Then
Exit For
End If
Next i


--

Regards,


Bill Lunney
www.billlunney.com

"Jaya" wrote in

message
...
Hi,

i have the following code in VB.
Set loXLS = GetObject(, "Excel.Application")

Dim loXLS As Excel.Application
For i = 1 To loXLS.Workbooks.Count
If loXLS.Workbooks(i).FullName =

psFile Then
Exit For
End If
Next i

When a excel file is already open, this fails. And

the
loxls.workbooks.count is 0. Can you tell me what

could
be
the reason?
-Jaya


.





.



All times are GMT +1. The time now is 05:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com