Get an already open active Excel workbook in AutoCad 2025

Get an already open active Excel workbook in AutoCad 2025

Thomas.Oppen
Explorer Explorer
359 Views
2 Replies
Message 1 of 3

Get an already open active Excel workbook in AutoCad 2025

Thomas.Oppen
Explorer
Explorer

One of my .net plugins reads and writes data to an aleady open active Excel workbook.

To get the Excel sheet I'm using

Dim appXL As Excel.Application
Dim wbXL As Excel.Workbook
Dim shXL As Excel.Worksheet

appXL = GetObject(, "Excel.Application")
wbXL = appXL.ActiveWorkbook
shXL = appXL.ActiveSheet

This has been working perfectly in AutoCad 2022 and 2023.

 

In AutoCAD 2025 GetObject(, "Excel.Application") doesn't deliver the application objetct anymore ...

 

Up to now I rcognized that this might be related to AutoCad 2025 using .net 8.

I have tried

- GetObject("", "Excel.Application")

- Marshal.GetActiveObject("Excel.Application")

- DirectCast(Marshal.GetActiveObject("Excel.Application"), Excel.Application)

But to no avail ... all three are working in AutoCad 2023 but not in 2025.

 

Do you have any ideas for approaches on this?

 

Best regards

Thomas

 

0 Likes
Accepted solutions (1)
360 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

You should read this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Thomas.Oppen
Explorer
Explorer

Thanks a lot _gile!

Shame on me, but I don't really understand the code - but I think I understand what it is doing and it is working fine.

Here is the code in Visual Basic:

    <DllImport("ole32")>
    Private Shared Function CLSIDFromProgIDEx(<MarshalAs(UnmanagedType.LPWStr)> ByVal lpszProgID As String, <Out> ByRef lpclsid As Guid) As Integer
    End Function

    <DllImport("oleaut32")>
    Private Shared Function GetActiveObject(<MarshalAs(UnmanagedType.LPStruct)> ByVal rclsid As Guid, ByVal pvReserved As IntPtr, <Out> <MarshalAs(UnmanagedType.IUnknown)> ByRef ppunk As Object) As Integer
    End Function

    Public Shared Function GetActiveObject(ByVal progId As String, ByVal Optional throwOnError As Boolean = False) As Object
        If progId Is Nothing Then Throw New ArgumentNullException(NameOf(progId))
        Dim clsid = Nothing
        Dim hr = CLSIDFromProgIDEx(progId, clsid)

        If hr < 0 Then
            If throwOnError Then Marshal.ThrowExceptionForHR(hr)
            Return Nothing
        End If

        Dim obj As Object = Nothing
        hr = GetActiveObject(clsid, IntPtr.Zero, obj)

        If hr < 0 Then
            If throwOnError Then Marshal.ThrowExceptionForHR(hr)
            Return Nothing
        End If

        Return obj
    End Function

 

 

0 Likes