General discussion

Locked

Opening Excel in VB6 using GetObject

By johnleonard ·
I am a wannabe VB programmer, and as such, am still learning the ropes. I am writing a little app to help a colleague generate reports on an excel spreadsheet he uses daily.

Have a look at the code below:

On Error Resume Next
Set objXl = GetObject(, "Excel.Application")
If Err.Number = 0 Then
blnXlOpen = True
Else
Set objXl = CreateObject("Excel.Application")
blnXlOpen = False
objXl.Visible = True
End If

It uses getobject to see if excel is already open. if so, then it sets a flag, if not, it sets the flag differently and opens excel. I 'recycled' the getobject code from the MSDN library, but here's my problem.
When I try and run the code from within the IDE (i'm using visual studio), I get a 'Run-time error 429 - ActiveXcomponent can't create object'. It highlights the Getobject line as the cause. but.... if I compile the code into an executable, it runs OK!
I reinstalled Office, thinking that its file associations were causing the problem, but that hasn't fixed it. I'm using Office XP.

Any ideas?

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Opening Excel in VB6 using GetObject

by johnleonard In reply to Opening Excel in VB6 usin ...

Point value changed by question poster.

Collapse -

Opening Excel in VB6 using GetObject

by sl-campbell In reply to Opening Excel in VB6 usin ...

Try using
Set xlObj = GetObject(, "Excel.Application.9")

and/or setting a reference to the "Microsoft Excel 9.0 Object Library"
under the menu item: Project | Reference

Collapse -

Opening Excel in VB6 using GetObject

by johnleonard In reply to Opening Excel in VB6 usin ...

Tried that suggestion, but it still doesn't work... I'm using Office XP, anyway, so I thought the object would be "excel.application.10". anyway, changing that to 9 or 10 doesn't work! adding the reference - nice thought, but that doesn't work either! thanks for your comments.

Collapse -

Opening Excel in VB6 using GetObject

by sl-campbell In reply to Opening Excel in VB6 usin ...

If you set a reference to the "Microsoft Excel 9.0 Object Library, try:
Dim xlObj As Excel.Application
Set xlObj = New Excel.Application

Collapse -

Opening Excel in VB6 using GetObject

by RuudA In reply to Opening Excel in VB6 usin ...

Hi,
That your code execution returns an error when you are in the IDE, eventhough the statement says "On Error Resume Next", means that the option "break on all errors" is selected in the options of your IDE.When compiled, the code runs as intended. Change the option in your IDE and the problem "26 - activeX component cannot create object" will not happen.

Collapse -

Opening Excel in VB6 using GetObject

by johnleonard In reply to Opening Excel in VB6 usin ...

Thanks very much - well spotted! I had also neglected to mention that this code worked on another machine I had with the same IDE - when I looked at the 'break on all errors' option it had been set differently.

Thanks very much for your help.

Collapse -

Opening Excel in VB6 using GetObject

by johnleonard In reply to Opening Excel in VB6 usin ...

This question was closed by the author

Back to Web Development Forum
7 total posts (Page 1 of 1)  

Related Discussions

Related Forums