C# – How to add an Excel Worksheet programmatically – Office XP / 2003

You need to add a COM reference in your project to the “Microsoft Excel 11.0 Object Library“ – or whatever version is appropriate. This code works for me: private void AddWorksheetToExcelWorkbook(string fullFilename,string worksheetName) { Microsoft.Office.Interop.Excel.Application xlApp = null; Workbook xlWorkbook = null; Sheets xlSheets = null; Worksheet xlNewSheet = null; try { xlApp = new … Read more

Excel 2007 automation on top of a Windows Server 2008 x64

The solution is really simple. The msdn forum thread can be found here To make a long story short I’m posting the solution here, credit goes to H Ogawa This solution is … ・Windows 2008 Server x64 Please make this folder. C:\Windows\SysWOW64\config\systemprofile\Desktop ・Windows 2008 Server x86 Please make this folder. C:\Windows\System32\config\systemprofile\Desktop …instead of dcomcnfg.exe. This … Read more

Exposing .NET events to COM?

The key concept in .NET code is to define event(s) as method(s) on a separate interface and connect it to the class via [ComSourceInterfacesAttribute]. In the example this is done with this code [ComSourceInterfaces(typeof(IEvents))] where IEvents interface defines the event(s) which should be handled on COM client. Note to event naming: Event names defined in … Read more

Use Office Interop on ASP.net MVC6 website

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. If you are building a solution that runs in a server-side … Read more

WinWord.exe won’t quit after calling Word.Documents.Add – Word .NET Interop

(All of my advice is adapted from this answer about Excel interop.) There are a few important things here: 1) Never use 2 dots on the same line. Also consider an indexer as a dot Good Word.Documents d = wordApp.Documents; Word.Document aDoc = d.Open(/*…*/); BAD Word.Document aDoc = wordApp.Documents.Open(/*…*/); 2) Release all of your pointers. … Read more