D
dabramov
So I am running into an interesting problem trying to access a sample
COM object I wrote in C# (see code below) from EXCEL (see VB code
below). I created 2 projects: One called COMTEST2003 which I compiled
and registered using Visual Studio 2003 and another called COMTEST2005
which I compiled and registered using Visual Studio 2005. So now I have
2 references in Excel's VB References called COMTEST2003 and
COMTEST2005. I created 2 different spreadsheets: one using a reference
to COMTEST2003 and the other using a reference to COMTEST2005 and ran
on different versions of Microsoft Office. I found the following:
(A) COMTEST2003 works on Office 2002 & Office 2003's Excel.
(B) COMTEST2005 doesn't work on Office 2002 (it says "File or assembly
name "COMTEST2005", or one of it dependencies, was not found) but works
fine on 2003.
What is going on here? Does anyone have any ideas why COMTEST2005
doesn't work on Office 2002? What version of the .NET framework does
Excel use?
########################################################################
########################################################################
For reference here is the code I am trying to run in VB in a
spreadsheet with 1 button:
' VB Code ********************************************************
' Build and register C# class library first
' Add a reference to the class library CSCL in the VB project.
' Paste this code into a simple form with 1 button (Name = Command1)
Option Explicit
Private WithEvents moComObj As COMTEST<2003 or 2005>.ComClassCS
Private Sub Command1_Click()
moComObj.Method
End Sub
Private Sub moComObj_MyEvent(ByVal StringParam As String)
MsgBox "Event caught : " & StringParam
End Sub
Private Sub Form_Load()
Set moComObj = New COMTEST<2003 or 2005>.ComClassCS
End Sub
Private Sub CommandButton1_Click()
Form_Load
Command1_Click
End Sub
#######################################################
#######################################################
For reference here is the code for the COM object I am trying to
compile in C#:
// C# Code ********************************************************
// Project type = Class Library
// Project name = CSCL
// Post build step = regasm cscl.dll /tlb:cscl.tlb /codebase
using System;
using System.Runtime.InteropServices;
namespace CSCL
{
[Guid("42E6DFAC-E988-4883-AC63-8D61A479D86B")]
public interface IComClassCS
{
string Method();
}
public delegate void MyEventHandler(string StringParam);
// Events interface
[Guid("49F6403F-B05D-4f97-AD1E-D62EC8C28DEC"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComClassCSEvents
{
void MyEvent(string StringParam);
}
// Class ComClassCS
[Guid("F610875B-40D3-4666-B367-60CDF30F5647"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComClassCSEvents))]
public class ComClassCS : IComClassCS
{
public event MyEventHandler MyEvent;
public ComClassCS()
{
}
public string Method()
{
if (MyEvent != null)
{
MyEvent("C# Event string...New");
}
return "C# COM Object return string";
}
}
}
COM object I wrote in C# (see code below) from EXCEL (see VB code
below). I created 2 projects: One called COMTEST2003 which I compiled
and registered using Visual Studio 2003 and another called COMTEST2005
which I compiled and registered using Visual Studio 2005. So now I have
2 references in Excel's VB References called COMTEST2003 and
COMTEST2005. I created 2 different spreadsheets: one using a reference
to COMTEST2003 and the other using a reference to COMTEST2005 and ran
on different versions of Microsoft Office. I found the following:
(A) COMTEST2003 works on Office 2002 & Office 2003's Excel.
(B) COMTEST2005 doesn't work on Office 2002 (it says "File or assembly
name "COMTEST2005", or one of it dependencies, was not found) but works
fine on 2003.
What is going on here? Does anyone have any ideas why COMTEST2005
doesn't work on Office 2002? What version of the .NET framework does
Excel use?
########################################################################
########################################################################
For reference here is the code I am trying to run in VB in a
spreadsheet with 1 button:
' VB Code ********************************************************
' Build and register C# class library first
' Add a reference to the class library CSCL in the VB project.
' Paste this code into a simple form with 1 button (Name = Command1)
Option Explicit
Private WithEvents moComObj As COMTEST<2003 or 2005>.ComClassCS
Private Sub Command1_Click()
moComObj.Method
End Sub
Private Sub moComObj_MyEvent(ByVal StringParam As String)
MsgBox "Event caught : " & StringParam
End Sub
Private Sub Form_Load()
Set moComObj = New COMTEST<2003 or 2005>.ComClassCS
End Sub
Private Sub CommandButton1_Click()
Form_Load
Command1_Click
End Sub
#######################################################
#######################################################
For reference here is the code for the COM object I am trying to
compile in C#:
// C# Code ********************************************************
// Project type = Class Library
// Project name = CSCL
// Post build step = regasm cscl.dll /tlb:cscl.tlb /codebase
using System;
using System.Runtime.InteropServices;
namespace CSCL
{
[Guid("42E6DFAC-E988-4883-AC63-8D61A479D86B")]
public interface IComClassCS
{
string Method();
}
public delegate void MyEventHandler(string StringParam);
// Events interface
[Guid("49F6403F-B05D-4f97-AD1E-D62EC8C28DEC"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IComClassCSEvents
{
void MyEvent(string StringParam);
}
// Class ComClassCS
[Guid("F610875B-40D3-4666-B367-60CDF30F5647"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(IComClassCSEvents))]
public class ComClassCS : IComClassCS
{
public event MyEventHandler MyEvent;
public ComClassCS()
{
}
public string Method()
{
if (MyEvent != null)
{
MyEvent("C# Event string...New");
}
return "C# COM Object return string";
}
}
}