Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2008, 03:07 PM
Registered User
 
Join Date: Jul 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default GetObject

Hello,
      I have an application in which i am using the GetObject function to detect if there is an instance of Excel currently running.

The code is below. I have checked the task manager to ensure there are no instances of excel. Every time i run the code it ends up creating the excel object. Any one ever see this behavior?


Thanks


Private Sub GetExcel()

        Dim myxl As New Excel.Application

        On Error Resume Next

        myxl = GetObject(, "Excel.Application")
        If Err.Number <> 0 Then
            MessageBox.Show("Excel is not running")
        Else
            MessageBox.Show("Excel is running")
            myxl.Application.Visible = True
        End If

    End Sub

J. David Hock
 
Old November 3rd, 2008, 05:02 PM
Registered User
 
Join Date: Jul 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello all,
        If anyone is interested i have been experimenting and found some interesting things. I created a new project and put the following code into a button click event



        Dim myxl As object

        On Error Resume Next

        myxl = GetObject(, "Excel.Application")
        If Err.Number <> 0 Then
            MessageBox.Show("Excel is not running")
        Else
            MessageBox.Show("Excel is running")
            myxl.Application.Visible = True
        End If



This produced the results I was hoping for, when excel is not running it shows the "Excel is not running message" and if I open an excel workbook and run it then I get the "Excel is running" message.

The difference between this sample app and the app that I have created is that I removed all references to microsoft office and microsoft excel. I also had to rem out any code that had to do anything with Excel. If someone could repeat this that would be great.


Thanks

J. David Hock
 
Old November 3rd, 2008, 09:29 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

> Every time i run the code it ends up creating the excel object.

Well OF COURSE it works like you said!
Code:
Private Sub GetExcel()

        Dim myxl As New Excel.Application
The NEW operator *specifically* is used to CREATE AN INSTANCE of the specified class!!!

So every time you use NEW, you are asking to create something...in this case, Excel!

All you had to do was get rid of the NEW:
Code:
Private Sub GetExcel()

        Dim myxl As Excel.Application
But please, please, please stop using ON ERROR RESUME NEXT. I can't believe MicroSloppy was crazy enough to leave that in VB.NET!





Similar Threads
Thread Thread Starter Forum Replies Last Post
getobject() failure- recent Loralee Access 2 November 24th, 2006 02:29 PM
getObject() and shell() question using automation juneBugz Access VBA 0 August 29th, 2005 08:46 PM
GetObject Trap bostek VBScript 2 November 24th, 2004 02:39 AM
why GetObject with IIS is not working alyeng2000 ASP.NET 1.x and 2.0 Application Design 0 September 15th, 2004 09:00 AM
UCOMIRunningObjectTable.GetObject wstueve C# 0 December 18th, 2003 11:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.