Using C# to write a custom node that writes date to specific cells on a Excel sheet. The code works fine as windows application. but got “Fail to load library” error while adding the dll into Dynamo. I’ve found the the first line that cause the error is at “excelApp.Workbooks.Open()”. Is there any steps that I missing in order to make this work? Thanks?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.DesignScript.Runtime;
using Autodesk.DesignScript.Interfaces;
using Microsoft.Office.Interop.Excel
public class Node
{
public static void DynWriteToXl(double a, double b)
{
string fileName = "C:\\Schedule.xlsx";
Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel._Workbook wBook;
Microsoft.Office.Interop.Excel._Worksheet wSheet;
object misValue = System.Reflection.Missing.Value;
excelApp = new Microsoft.Office.Interop.Excel.Application();
//open
wBook = excelApp.Workbooks.Open(fileName, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);//This is the problem start
wSheet = (Microsoft.Office.Interop.Excel.Worksheet)wBook.Worksheets.get_Item(1);
////write
double col = 2;
wSheet.Cells[6, col] = a;
wSheet.Cells[7, col] = b;
try
{
//save
excelApp.DisplayAlerts = false;
wBook.SaveAs(fileName, misValue, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, misValue, misValue, misValue, misValue, misValue);
wBook.Close(true, fileName, misValue);// close the worksheet
}
finally
{
if (excelApp != null)
{
excelApp.Quit(); // close the excel application
}
}
}
}