Click here to Skip to main content
16,016,770 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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
Updated 6-Dec-12 20:28pm
v2
Comments
Richard MacCutchan 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

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.


C#
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);
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900