Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i'm able to read the content from word file which is corrupted and recovery it but im stuck how to copy the content to create a new word file and copy that content to it
 
here is the bel;ow code to read the content , but please help me to create a new word and copy contetnt without loosing the exact formats of content.
 

Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass();
            object file = path;
            object nullobj = System.Reflection.Missing.Value;
 
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,true);
            doc.ActiveWindow.Selection.WholeStory();
            IDataObject data = Clipboard.GetDataObject();
 
          
            
            txtFileContent.Text = data.GetData(DataFormats.Text).ToString();
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
Posted 6-Dec-12 21:23pm
Edited 6-Dec-12 21:28pm
v2
Comments
Richard MacCutchan at 7-Dec-12 5:17am
   
Why are you using the clipboard, when all you need to do is create a new file and store the contents of your txtFileContent object into it, and save it.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

as my senario is like i had a Corrupted .docx file , i can't open it and read it,so what i have to do is read the content from it and make a duplicate of that file.
 
i got the solution as i didn't copy but Recovered the Corrupted .docx file and saveas it with another name.
 

Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            object file = path;
            object nullobj = System.Reflection.Missing.Value;
 
            Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
                ref file, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, true);
 
            Object oSaveAsFile = (Object)"C:\\SampleDoc.docx";
 
            doc.SaveAs2(oSaveAsFile, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument);
 
            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100