October 2nd, 2013, 10:59 AM
Copy text from word (including formats bold,underline,highlighted areas)into excelcel
I am currently working on excel add-in project, that has search filter. Upon giving search keywords and click on search, it connects sharepoint server and pulls the text/data from word documents located in the directories of sharepoint server and displays in the list box in add-in. I am trying to copy this text from word document and paste into excel cell through programmatically. I can bring the text from word file and show it in excel cell, but could not get the formats that are in word. I appreciate if anyone could throw ideas in resolving this issue.
To make it clear, this should work like copying the text from word document(with formats like underline,bold,itaclicize, font color) and paste as 'keep source formatting' in excel cell.
below is the code I have.
private void InsertItemIntoDocument(SearchResultItem item, ItemInsertionLocation location)
Word.Application wordApp = new Word.Application();
object nullobj = System.Reflection.Missing.Value;
if (item != null)
string localFileLoc = this.DownloadFileWithWebClient(item.ContentsUrl);
Word.Document document = wordApp.Documents.Open(localFileLoc.Replace("\\\\", "\\"), Type.Missing, true, false, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
IDataObject data = Clipboard.GetDataObject();
var content = data.GetData(DataFormats.Text).ToString();
content = content.Replace("\\n\\r", Environment.NewLine);
var ActiveCell = Globals.ThisAddIn.Application.ActiveCell.Address.ToString();
var range = CurrentApplication.Range[ActiveCell];
range.Value2 = content;
range.WrapText = true;
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
range.VerticalAlignment = Excel.XlVAlign.xlVAlignTop;
Click Here to Expand Forum to Full Width