GetObject Excel ???

galileogali

Well-known Member
Joined
Oct 14, 2005
Messages
748
Since Word I open Excel, but if it was already open I would like to assign it to an object with this code

Code:
On Error Resume Next
Set xlp = GetObject(Application.Path & "\EXCEL.EXE", "Excel.Application")
If xlp Is Nothing Then
    Set xlp = CreateObject("Excel.Application")
End If

However, still open Excel

Code:
Set xlp = GetObject(Application.Path & "\EXCEL.EXE", "Excel.Application")


xlp is Nothing

What I Have wrong????
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello ,

Here is some information about using GetObject:

Syntax - GetObject([pathname] [, class])

<title>GetObject Function</title><style>@import url(office.css);</style><link disabled="" href="msoffice.css" rel="stylesheet">If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

This should work.
Code:
On Error Resume Next
  Set xlp = GetObject(, "Excel.Application")
  Err.Clear
On Error GoTo 0

If xlp Is Nothing Then
   Set xlp = CreateObject("Excel.Application")
End If
Sincerely,
Leith Ross
 
Upvote 0
Credit to the following post to Kevin Jones. I now use this technique whereas previously I used code similar to Leith's


Be aware that when launching Excel through automation using the CreateObject function, Excel does not load any Add-Ins or other workbooks normally loaded automatically. This is not a good way to start an Excel session that will be used by the user. To start an Excel application instance without using automation from any application other than Excel, the Excel application must be launched using non-automation means. The code below illustrates the steps to do this. The code first tries to obtain an automation handle to an existing application instance. If an existing instance is not found then a new instance is started using the Shell command.


Code:
   Dim ExcelApplication As Object
   Dim TimeoutTime As Long
   
   On Error Resume Next
   Set ExcelApplication = GetObject(, "Excel.Application")
   On Error GoTo 0
   If ExcelApplication Is Nothing Then
       Shell "Excel.exe"
       TimeoutTime = Timer + 5
       On Error Resume Next
       Do
           DoEvents
           Err.Reset
           Set ExcelApplication = GetObject(, "Excel.Application")
       Loop Until Not ExcelApplication Is Nothing Or Timer > TimeoutTime
       On Error GoTo 0
   End If
   If ExcelApplication Is Nothing Then
       MsgBox "Unable to launch Excel."
   Else
       ' Do something with the Excel instance...
   End If
 
Upvote 0

Forum statistics

Threads
1,224,541
Messages
6,179,418
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top