Hi Paul,
The code posted in reply would work from VBA, but we need some minor
changes to make it work from an Excel-DNA macro. In particular, we
need to get hold of the Application object, and use ActiveWorkbook
instead of ThisWorkbook, since the code will not be associated with a
particular workbook.
There is something else I need to address. Excel has two interfaces,
the COM automation interface familiar from VBA and a C API described
in the Excel .xll SDK. Excel-DNA gives you the opportunity to use
either API.
To use the COM automation interface, you need some way of getting hold
of the root Application object (this is 'magically' always around in
VBA, and the default object when resolving a token like ThisWorkbook).
From a macro in Excel-DNA, you get hold of the right Application
object by accessing ExcelDnaUtil.Application. From there the object
model is accessed as usual.
To use the C API, Excel-DNA defined a type called XlCall (after the
xlcall.h header file in the SDK). To call C API functions, you use
XlCall.Excel(XlCall.xlcXXXXX, param1, param2...).
The XlCall.xlfXXX and XlCall.xlcXXX constanst refer to the various C
API functions, which match the Excel 4.0 macro functions, and are
documented in the help file that is available from Microsoft here:
http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c09bf7f7-d30e-4ce9-8930-5d03748ca5cd&displaylang=en.
As an example, to get the worksheets in a workbook, you use the
GET.WORKBOOK information function (xlfGetWorkbook). When passed the
first parameter '1', if will return an array with all the sheets in
the workbook. A fairly complete example of accessing the GET.XXX
information functions is in the Distribution\Samples\GetInfoAddin.dna
file, and the matching GetInfoSample.xls book. To load these, just
make a copy of ExcelDna.xll in the same directory, rename to
GetInfoAddin.xll and open.
For the COM automation style: below is a complete .dna file with
VB.NET code which adds a menu and macro to dump the list of sheets in
the active workbook to a new sheet.
Regards,
Govert
<DnaLibrary>
<![CDATA[
' Importing the ExcelDnaUtil type allows the
' direct call to the Application property work.
Imports ExcelDna.Integration.ExcelDnaUtil
Public Module Test
<ExcelCommand(MenuName:="Test", MenuText:="List Sheets")> _
Sub GetNames()
Dim i As Integer
Dim count As Integer
Dim sheetNames() As String
Dim wb As Object
Dim sh As Object
' Get hold of the application object and active workbook
wb = Application.ActiveWorkbook
' Get sheet names into an array
count = wb.Worksheets.Count
ReDim sheetNames(count)
For i = 1 To count
sheetNames(i) = wb.Worksheets(i).Name
Next i
' Dump sheet names to a new sheet
sh = wb.Worksheets.Add()
For i = 1 To count
sh.Cells(i, 1).Value = sheetNames(i)
Next i
End Sub
End Module
]]>
</DnaLibrary>
On Jun 3, 9:28 am, "
HR...@telefonica.net" <
HR...@telefonica.net>
wrote: