

- #.net office interop copy word table to excel code
- #.net office interop copy word table to excel windows
ObjWordApp.Quit(ref objNull, ref objNull, ref objNull) Read and Import MS Word Document to Database using Microsoft Interop Word library in ASP.Net SqlBulkCopy: Bulk Copy data from DataTable (DataSet) to SQL Server. ObjDoc.Close(ref objNull, ref objNull, ref objNull) String strWordText = data.GetData(DataFormats.Text).ToString() IDataObject data = Clipboard.GetDataObject() Document objDoc = (ref file, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull) ApplicationClass objWordApp = new .ApplicationClass()
#.net office interop copy word table to excel code
MyString = myString.Substring(0, Math.Please check below sample code to read word document, foreach (.Table table in objDoc.Tables)įor (int row = 1 row 0 & myString.Substring(len - 1) = charString13) || (myString.Substring(len - 1) = charString7))

2) Navigate to the workbook (Can also set the excel visisble False so everything is done in invisible mode) 3) Find the data range using Excel range object. With the help of Cindy Meister, combined with the answer from Paul Walls in this other question for replacing characters in a C# string, here is the resulting answer. From your word add-in on the button click you use Excel interop library (excel application object/open workbook/Range Copy) operations to do the above. S = s.Substring(0, Math.Min(len-1, len)) I'm copying a table from Word then pasting the copied table in an Excel file: () oSheet.Activate () rng oSheet.Range ('A1') rng.Activate () rng.PasteSpecial () But when I do this, the table is pasted as a picture. While (len > 0 & s.Substring(len - 1) = (char)13 || s.Substring(len - 1) = (char)7) Copy table from Word then paste to Excel using VB.NET. Private string TrimCellText2(s As String) cut off ANSI 13 + ANSI 7 from the end of the string coming from a replace remaining paragraph marks with the Excel new line character convert the numbers to plain text, then undo the conversionĬellContent = TrimCellText2(cellContent) (Note: this is VBA code that I've converted off the top of my head, so watch out for small syntax "gotchas") Word.Range rng =

The following bit of sample code takes care of all three conversions. Or the numbering has to be converted to plain text.Īnother issue is the "dot" at the end of the cell content, which is again ANSI 13 in combination with ANSI 7 (end-of-cell marker). Passing a string loses formatting it can only be carried across using Copy. So that needs to be converted.Īutomatic Line numbering is formatting. (In this case it must be new paragraphs since the numbering is being generated.) Excel wants ANSI 10 Word is using ANSI 13. There are multiple problems involved with achieving the stated result:Įxcel doesn't use the same character as Word for new lines or new paragraphs. That yielded this result:Īlthough I can get the text with the list numbers, I do not get a carriage return, line break, or line feed to separate the items the list.Īt the very least, I would like to preserve the list numbering and line breaks without having to manually cut/paste with Keep Text Only and I want to avoid having to parse the text for the list numbers (which could be numbers or letters) and inserting line feeds. I also tried converting the list in the Word file by copying and pasting with Keep Text Only to remove Word's automatic formatting, and manually deleting the tabs. I have also tried the following: worksheet.Cells = table.Cell(row, col).Range.FormattedText To do this, follow these steps: On the Project menu, click Add Reference. Add a reference to the Microsoft Excel Object Library.
#.net office interop copy word table to excel windows
Select Windows Application from the Visual C Project types. I thought this line would accomplish what I wanted: (.WdUnits. However, I get the following result where the Word cell containing the list is not copied properly into Excel: On the File menu, click New, and then click Project. Unfortunately the code is pasting the table as part of the current table instead of a new table, so consequently a new table is not added to the tables collection of the word document. String text = table.Cell(row, col).Range.Text Using the code below, I can copy cells from the Word Table to a corresponding cell in an Excel worksheet: foreach (.Table table in objDoc.Tables)įor (int row = 1 row <= row++)įor (int col = 1 col <= col++) I have the following test table in Word, with one cell having a multilevel list:
