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;
 
<pre>
        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);</pre>
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;
 
<pre>
        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)&quot;C:\\SampleDoc.docx&quot;;
 
        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 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200


Advertise | Privacy | Mobile
Web03 | 2.8.150331.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2015
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