Re: Late Binding of COM components
- From: "Stephany Young" <noone@localhost>
- Date: Wed, 23 May 2007 15:35:23 +1200
Isn't it amazing what a bit more research reveals.
The worst aspect is that you can research a subject for weeks and get nowhere, but a matter of hours after you post a question in here you stumble across the piece of information that you were missing. I'm going to call it Stephany's Colollary on Murphy's Law :)
The upshot is that the Office 11 (Office 2003) PIA's are forward compatible with Office 12 (Office 2007), but not backward compatible with Office 10 (Office XP). Unfortunately, to install the Office 11 PIA's on your development machine you need to have Office 2003 installed which is not practical if you have Office 2007 installed.
A workaround for this is to collect up the Office 11 PIA's and place them (in a folder) on your development machine and add references to these PIA's rather than the Office 12 PIA's.
Once you've done this you don't have to worry about late-binding anymore.
Obviously you can't use any Office 12-specific features is your code.
Now the casting of the object of type System.__ComObject returned by the GetObject function to an object of type Excel.Application works a treat, and therefore the business rules can easily be honoured.
The amended code is shown below:
Imports Microsoft.Office.Interop
Imports System.Reflection
Imports System.Runtime.InteropServices
Public Class Form1
Private Const ProgID As String = "Excel.Application"
Private m_type As Type
Private m_object As Excel.Application
Private m_created As Boolean
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
m_type = Type.GetTypeFromProgID(ProgID)
m_object = Nothing
m_created = False
Try
m_object = CType(GetObject(, ProgID), Excel.Application)
Catch
Try
m_object = CType(CreateObject(ProgID), Excel.Application)
m_created = True
Catch
m_object = Nothing
End Try
End Try
Dim _message As String
If m_object Is Nothing Then
_message = String.Format("Failed to instantiate compatible {0} object", ProgID)
Else
Dim _created As String = "existing"
If m_created Then _created = "new"
_message = String.Format("Instantiated {0} compatible {1} object - Version {2}", _created, ProgID, m_object.Version)
End If
Console.WriteLine(_message)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If m_object Is Nothing Then
Console.WriteLine("m_object Is Nothing - No action")
Else
If m_created Then m_object.Quit()
GCCom(m_object)
Dim _message As String
If m_created Then
_message = String.Format("Destroyed new {0} object", ProgID)
Else
_message = String.Format("Destroyed reference to existing {0} object", ProgID)
End If
Console.WriteLine(_message)
End If
End Sub
Private Sub GCCom(ByVal ParamArray objects As Object())
For Each _object As Object In objects
If _object IsNot Nothing Then
Dim _references As Integer = Integer.MaxValue
Do
_references = Marshal.ReleaseComObject(_object)
Loop While _references > 0
_object = Nothing
End If
Next
GC.Collect()
GC.WaitForPendingFinalizers()
End Sub
End Class
.
- References:
- Late Binding of COM components
- From: Stephany Young
- Late Binding of COM components
- Prev by Date: Side-by-Side Folder Compare - Which box is best - ListBox, ListView or ????
- Next by Date: What type of object should I pass to outlook email as a attachment when doing drag drop?
- Previous by thread: Re: Late Binding of COM components
- Next by thread: How to MAP the drive
- Index(es):