--------------------------------------------------
Your monthly contribution helps the project thrive.
--------------------------------------------------
There is some confusion between the Imports and the types in your code.
What I have below seems to compile and work.
Note that I use a call to ExcelDnaUtil.Application to get hold of the root Application object.
Excel does not allow you to write to or modify the worksheet from inside a function / calculation context.
The best you can do is to schedule a macro which will run as soon as possible, typically after the calculation is complete.
When setting up the macro run you need to save or pass in any context needed, because the 'ActiveSheet' or the 'Selection' might not be what it was when the function was called.
In addition, if you have this function called from different cells, you can't be sure of the order in which the macros will run.
So it will be tricky to make reliable, and you might consider other mechanisms too which might be less interfering with the user's workbook.
For example, you can keep the detailed log in memory and display in a custom task pane.
Anyway, here's some code that will hopefully get you started.
-Govert
Imports Microsoft.Office.Interop.Excel
Imports ExcelDna.Integration
Public Module TestPIA
Public Function TestExcel() As Double ' test access to Excel
' Set up a macro run when Excel is ready again
' - after calculations are complete and the user is not interacting with the sheet.
ExcelAsyncUtil.QueueAsMacro(Sub() WriteStuffToSheet())
Return 0
End Function
Private Sub WriteStuffToSheet()
Dim Application As Application
Dim ws As Worksheet
Application = ExcelDnaUtil.Application
Application.ActiveWorkbook.Sheets.Add(After:=Application.ActiveWorkbook.Worksheets(Application.ActiveWorkbook.Worksheets.Count))
Application.ActiveWorkbook.Sheets(Application.ActiveWorkbook.Worksheets.Count).Name = "ll"
End Sub
End Module