Accessing existing Excel instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Dunn

    #1

    Accessing existing Excel instance

    I'm simply trying to access an instance of Excel that has been opened
    manually by the user in order to put a few values in the existing sheet.
    Here's the code I've tried:

    Dim oXL As Excel.Applicati on
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    oXL = GetObject(, "Excel.Applicat ion")
    oWB = oXL.ActiveWorkb ook
    oSheet = oWB.ActiveSheet

    Unfortunately this doesn't work. What happens is that oXL.ActiveWorkb ook
    is "nothing" so the last line in this code fails when trying to access
    it's ActiveSheet property.

    The reason this happens seems to be that GetObject isn't getting the
    existing Excel instance and is instead behaving exactly like
    CreateObject and making a new instance with no workbooks. In fact
    GetObject even works if Excel isn't currently running which I don't
    think it's meant to. For example the following code (which is adapted
    slightly from the help for GetObject):

    oXL = GetObject(, "Excel.Applicat ion")
    If Err().Number <0 Then
    MsgBox("Excel was not running")
    Else
    MsgBox("Excel was running")
    End If

    This always reports that Excel was running even if it wasn't.

    I'm using Visual Studio 2003 and have tried this with both Excel 2000,
    and Excel 2003 with the same result.

    Any idea what's going wrong or how I can do this properly?

    Thanks

    --
    Ian Dunn
    Polyhedron Software Ltd. - www.polyhedron.com
    Programs for Programmers - QA, Compilers, Tools, Graphics, Consultancy
  • Paul Larson

    #2
    Re: Accessing existing Excel instance


    "Ian Dunn" <ian.news@polyh edron.comwrote in message
    news:P9qdnSf_pY qcCjfZRVnysw@ec lipse.net.uk...
    I'm simply trying to access an instance of Excel that has been opened manually
    by the user in order to put a few values in the existing sheet. Here's the
    code I've tried:
    >
    Dim oXL As Excel.Applicati on
    Dim oWB As Excel.Workbook
    Dim oSheet As Excel.Worksheet
    oXL = GetObject(, "Excel.Applicat ion")
    oWB = oXL.ActiveWorkb ook
    oSheet = oWB.ActiveSheet
    >
    Unfortunately this doesn't work. What happens is that oXL.ActiveWorkb ook is
    "nothing" so the last line in this code fails when trying to access it's
    ActiveSheet property.
    >
    The reason this happens seems to be that GetObject isn't getting the existing
    Excel instance and is instead behaving exactly like CreateObject and making a
    new instance with no workbooks. In fact GetObject even works if Excel isn't
    currently running which I don't think it's meant to. For example the following
    code (which is adapted slightly from the help for GetObject):
    >
    oXL = GetObject(, "Excel.Applicat ion")
    If Err().Number <0 Then
    MsgBox("Excel was not running")
    Else
    MsgBox("Excel was running")
    End If
    >
    This always reports that Excel was running even if it wasn't.
    >
    I'm using Visual Studio 2003 and have tried this with both Excel 2000, and
    Excel 2003 with the same result.
    >
    Any idea what's going wrong or how I can do this properly?
    After adding "Imports Excel=Microsoft .Office.Interop .Excel" and adding a
    reference to the Excel 10 object model (which auto-includes
    Microsoft.Offic e.Core) I was able to run the code successfully.

    I'm using
    VStudio 2003 v7.1.3088
    .Net framework v1.1.4322 SP1
    Excel 2002(10.6789.67 35) SP3

    HTH
    Paul


    Comment

    • Ian Dunn

      #3
      Re: Accessing existing Excel instance

      Paul Larson wrote:
      After adding "Imports Excel=Microsoft .Office.Interop .Excel" and adding a
      reference to the Excel 10 object model (which auto-includes
      Microsoft.Offic e.Core) I was able to run the code successfully.
      >
      I'm using
      VStudio 2003 v7.1.3088
      .Net framework v1.1.4322 SP1
      Excel 2002(10.6789.67 35) SP3
      That's the exact same version of VStudio and .Net framework that I'm
      using so it's odd that it doesn't work for me.

      The only difference is that your using Excel 2002. But I tried it with
      Excel 2000 (Excel 9 object model) and Excel 2003 (Excel 11 object model)
      on two different computers and both had the same problem.

      Thanks for trying anyway.

      --
      Ian Dunn
      Polyhedron Software Ltd. - www.polyhedron.com
      Programs for Programmers - QA, Compilers, Tools, Graphics, Consultancy

      Comment

      • R. MacDonald

        #4
        Re: Accessing existing Excel instance

        Hello, Ian,

        FWIW, I see the same behaviour as you report. I'm using:

        VStudio 2003 v7.1.3088
        .NET Framework version 1.1.4322 SP1
        Excel 2000 (9.0.6926) SP3

        You can get a reference to the existing Excel "process" by using:

        Process.GetProc essesByName("Ex cel")

        but I don't know how to use this to get a reference to the application.
        But maybe someone else does.

        Cheers,
        Randy


        Ian Dunn wrote:
        Paul Larson wrote:
        >
        >After adding "Imports Excel=Microsoft .Office.Interop .Excel" and adding
        >a reference to the Excel 10 object model (which auto-includes
        >Microsoft.Offi ce.Core) I was able to run the code successfully.
        >>
        >I'm using
        > VStudio 2003 v7.1.3088
        > .Net framework v1.1.4322 SP1
        > Excel 2002(10.6789.67 35) SP3
        >
        >
        That's the exact same version of VStudio and .Net framework that I'm
        using so it's odd that it doesn't work for me.
        >
        The only difference is that your using Excel 2002. But I tried it with
        Excel 2000 (Excel 9 object model) and Excel 2003 (Excel 11 object model)
        on two different computers and both had the same problem.
        >
        Thanks for trying anyway.
        >

        Comment

        Working...