Click here to Skip to main content
15,998,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to identity a part of word document with a key then in need to copy it into another word document. how can i achieve it through C#.net.

i tried the following it result as text i need it as document
and paragraph should be i identified with a key word

What I have tried:

C#
public string ReadFileContent(string path, int paraGraphNum)
        {
            int i = 0;

            StringBuilder sb = new StringBuilder();

            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);


            Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;

            // Count number of paragraphs in the file
            long parCount = DocPar.Count;
           
            // Step through the paragraphs
            while (i < parCount)
            {
                i++;
                if (i == paraGraphNum)
                {
                    sb.Append(DocPar[i].Range.Text);
                    break;
                }
            }

            doc.Close(ref nullobj, ref nullobj, ref nullobj);
            wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
            return (sb.ToString());
        }
    }
Posted
Updated 4-Aug-16 4:48am
v2
Comments
Richard MacCutchan 3-Aug-16 9:26am    
You are only copying the Text into a StringBuilder, so any font or layout information is lost. You need to capture all the metadata that controls how the text should be formatted.

1 solution

 
Share this answer
 
v2
Comments
[no name] 4-Aug-16 13:51pm    
Great links, a 5.
Maciej Los 4-Aug-16 13:52pm    
Thank you, Bruno.

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