Add a row to an MS Word table using Microsoft.Office.Interop

Leave the parameter value as a missing value for the Row.Add Function object oMissing = System.Reflection.Missing.Value; // get your table or create a new one like this // you can start with two rows. Microsoft.Office.Interop.Word.Table myTable = oWordDoc.Add(myRange, 2,numberOfColumns) int rowCount = 2; //add a row for each item in a collection. foreach( string s … Read more

c# word interop find and replace everything

I use this function to find and replace. you can specify any of the options. private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText) { //options object matchCase = false; object matchWholeWord = true; object matchWildCards = false; object matchSoundsLike = false; object matchAllWordForms = false; object forward = true; object format = false; object matchKashida … Read more

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

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

Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#

To set a range to text: xlYourRange.NumberFormat = “@”; You can also prefix a value you put in a cell with an apostrophe for it to format it as text: xlYourRange.Value = “‘0123456”; To set a range to number xlYourRange.NumberFormat = “0”; Obviously if you want to set the format for the entire column then … Read more