VB and C# compilation differences

A

Alan Roberts

First I appologise for cross posting. I put this in the C# group and then
discovered this group which is probably more appropriate. Anyway, can
someone please explain the following for me...

I am trying to link to a .NET DLL from Excel. Excel needs to pass a
reference to itself to the DLL and then the DLL needs to perform some work
on the running instance of Excel via that reference. As an example, a VB
DLL to return the path to the current active workbook contains the following
(in addition to the COM GUIDS automatically added to a new COMClass)...

Public Class Class1
Public Sub New()
MyBase.New()
End Sub
Private gExcel As Object
Public Property Excel() As Object
Set(ByVal value As Object)
gExcel = value
End Set
Get
Excel = gExcel
End Get
End Property
Public Function Path() As String
Return Excel.ThisWorkbook.Path
End Function
End Class

After adding a refernece to the DLL, a macro in Excel could call this as
follows

Sub VBTest()
Dim VBTest As New VBExcelTest.Class1
Set VBTest.Excel = Application
Debug.Print VBTest.Path
End Sub

This all work fine

How can I do the same thing using C#? If I try to create a DLL with
equivalent(?) code eg

public class Class1
{
public Class1(){
}
private object gExcel;
public object Excel{
get{
return gExcel;
}
set{
gExcel = value;
}
}
public string Path(){
return Excel.ThisWorkbook.Path;
}
}

I get a compile error saying that - 'object' does not contain a definition
for 'ThisWorkbook'

How can I get this to work?

Thanks

Alan
 
P

Philip

Hi,

You need to pass in a reference to the workbook you want to use, or
explicitly use one of these options:

Excel.Activeworkbook

or

Excel.workbooks(..)

depending on how c# uses class collections...

HTH

Philip
 
A

Alan Roberts

Hi Philip

Thanks for the reply. As I understand it though, ThisWorkbook should be
valid as well. It is being called from a workbook so the reference is fine.
More to the point the code works fine in VB.Net it just wont compile in C#.
Also, neither the VB.Net nor C# classes have references to the Excel type
library (in order for the code to work with all versions of Excel not just
one specific version) and the C# issue is occuring at compile time. My
question is why won't any of these options (ThisWorkbook/ActiveWorkbook etc)
compile under C#. I'm assuming that it stems from the decleration of gExcel
as object (which doesn't have an ThisWorkbook property per se) but the code
works in VB.Net and I need to try and get the same functionality in C#.

Cheers

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top