none
Copying text from a certain style range to a new document RRS feed

  • Question

  • Hi,

    I'm using VS express 2013. How do I extract text from a range of styles in a 2010 Microsoft Word document onto a new word document?

    For example, the first section is labelled as "1-Introduction" with heading 1, the second section is labelled as "2-Operations" also with heading 1. I want everything under the introduction up to but not including the design section to be copy pasted into a new document (without changing the formatting) and then everything in section 2 to be pasted into a new document. I currently have the following code to read the styles but I think it reads the styles of the whole document.

     // Extract the styles or stylesWithEffects part from a  
            // word processing document as an XDocument instance. 
            public static XDocument ExtractStylesPart(
              string fileName,
              bool getStylesWithEffectsPart = true)
            {
                // Declare a variable to hold the XDocument. 
                XDocument styles = null;

                // Open the document for read access and get a reference. 
                using (var document =
                    WordprocessingDocument.Open(fileName, false))
                {
                    // Get a reference to the main document part. 
                    var docPart = document.MainDocumentPart;

                    // Assign a reference to the appropriate part to the 
                    // stylesPart variable. 
                    StylesPart stylesPart = null;
                    if (getStylesWithEffectsPart)
                        stylesPart = docPart.StylesWithEffectsPart;
                    else
                        stylesPart = docPart.StyleDefinitionsPart;

                    // If the part exists, read it into the XDocument. 
                    if (stylesPart != null)
                    {
                        using (var reader = XmlNodeReader.Create(
                          stylesPart.GetStream(FileMode.Open, FileAccess.Read)))
                        {
                            // Create the XDocument. 
                            styles = XDocument.Load(reader);
                        }
                    }
                }
                // Return the XDocument instance. 
                return styles;
            }



    • Edited by Ranster101 Monday, October 6, 2014 7:09 PM
    Monday, October 6, 2014 6:52 PM

Answers

  • Hi Ranster,

    Sorry for the misunderstanding.

    >> I want the code to check if the text is of a particular style and if so, copy the first instance of the text when it finds that style until the style changes and so on<<

    As far as I know, the main document story of the simplest WordprocessingML document consists of the following XML elements:

    document — The root element for a WordprocessingML's main document part, which defines the main document story.

    body — The container for the collection of block-level structures that comprise the main story.

    p — A paragraph.

    r — A run.

    t — A range of text.

    And we can loop the run element of the paragraphs to find the particlar style of text we wanted, then insert it into the new document. Also we need to copy the style into the new document if it dosen't exist.

    Here is a sample for your reference, it copied the text which had the style named Heading1Char:

     class CopytParticlarStyleText
        {
            public void Main()
            {
                string filepath = @"C:\Users\UserName\Desktop\Style2.docx";
                string targetFilepath = @"C:\Users\UserName\Desktop\CopyStyleText.docx";
                string styleId="Heading1Char";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath,true))
                {
    
                    //insert style
                    if (!IsStyleIdInDocument(targetFilepath, styleId))
                    {
                        Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;
    
                     
                        // Look for a match on styleid.
                        Style style = s.Elements<Style>()
                            .Where(st => (st.StyleId == styleId))
                            .FirstOrDefault();
                        if (style != null)
                        {
                            
                            AddNewStyle(targetFilepath, (Style)style.Clone());
                        }
                     
                    }
    
                    //insert text
                    foreach (Run run in doc.MainDocumentPart.Document.Descendants<Run>())
                    {
                        if (run.RunProperties != null && run.RunProperties.RunStyle !=null && run.RunProperties.RunStyle.Val ==styleId)
                        {
                            OpenAndAddTextToWordDocument(targetFilepath, run.Descendants<Text>().First().Text);
                        }
                    }
                
                }
            }
    
            public static void OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
                // Add new text.
                Paragraph para = body.AppendChild(new Paragraph());
                RunProperties runProperties1 = new RunProperties();
                RunStyle runStyle1 = new RunStyle() { Val = "Heading1Char" };
                runProperties1.Append(runStyle1);
    
                Run run = para.AppendChild(new Run());
    
                run.Append(runProperties1);
                run.AppendChild(new Text(txt));
    
                // Close the handle explicitly.
                wordprocessingDocument.Close();
            }
    
            // Return true if the style id is in the document, false otherwise.
            public bool IsStyleIdInDocument(string filepath,
                string styleid)
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
                {
                    // Get access to the Styles element for this document.
                    Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;
    
                    // Check that there are styles and how many.
                    int n = s.Elements<Style>().Count();
                    if (n == 0)
                        return false;
    
                    // Look for a match on styleid.
                    Style style = s.Elements<Style>()
                        .Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph))
                        .FirstOrDefault();
                    if (style == null)
                        return false;
    
                    return true;
                }
    
            }
    
            // Create a new style with the specified styleid and stylename and add it to the specified
            // style definitions part.
            private static void AddNewStyle(string filepath, Style style)
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
                {
    
                    // Get the Styles part for this document.
                    StyleDefinitionsPart styleDefinitionsPart =doc.MainDocumentPart.StyleDefinitionsPart;
                    // Get access to the root element of the styles part.
                    Styles styles = styleDefinitionsPart.Styles;
                    // Add the style to the styles part.
                    styles.InsertAt(style, 0);
                    //styles.Append(style);
                }
    
    
            }
    
    
        }

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 9, 2014 3:17 AM
    Moderator

All replies

  • Hi Ranster,

    >> How do I extract text from a range of styles in a 2010 Microsoft Word document onto a new word document?<<

    Based on the description, you want to keep the format when you copy the text to a new document.

    When we copied text into a new word document, we need to check whether the style existed in the new document. If not we need to copy the style into the new document either.

    Here are some realtive articles about it:

    How to: Extract styles from a word processing document (Open XML SDK)

    How to: Apply a style to a paragraph in a word processing document (Open XML SDK)

    hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 7, 2014 8:16 AM
    Moderator
  • I think you misunderstood. I want the code to check if the text is of a particular style and if so, copy the first instance of the text when it finds that style until the style changes and so on
    Tuesday, October 7, 2014 1:12 PM
  • Hi Ranster,

    Sorry for the misunderstanding.

    >> I want the code to check if the text is of a particular style and if so, copy the first instance of the text when it finds that style until the style changes and so on<<

    As far as I know, the main document story of the simplest WordprocessingML document consists of the following XML elements:

    document — The root element for a WordprocessingML's main document part, which defines the main document story.

    body — The container for the collection of block-level structures that comprise the main story.

    p — A paragraph.

    r — A run.

    t — A range of text.

    And we can loop the run element of the paragraphs to find the particlar style of text we wanted, then insert it into the new document. Also we need to copy the style into the new document if it dosen't exist.

    Here is a sample for your reference, it copied the text which had the style named Heading1Char:

     class CopytParticlarStyleText
        {
            public void Main()
            {
                string filepath = @"C:\Users\UserName\Desktop\Style2.docx";
                string targetFilepath = @"C:\Users\UserName\Desktop\CopyStyleText.docx";
                string styleId="Heading1Char";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath,true))
                {
    
                    //insert style
                    if (!IsStyleIdInDocument(targetFilepath, styleId))
                    {
                        Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;
    
                     
                        // Look for a match on styleid.
                        Style style = s.Elements<Style>()
                            .Where(st => (st.StyleId == styleId))
                            .FirstOrDefault();
                        if (style != null)
                        {
                            
                            AddNewStyle(targetFilepath, (Style)style.Clone());
                        }
                     
                    }
    
                    //insert text
                    foreach (Run run in doc.MainDocumentPart.Document.Descendants<Run>())
                    {
                        if (run.RunProperties != null && run.RunProperties.RunStyle !=null && run.RunProperties.RunStyle.Val ==styleId)
                        {
                            OpenAndAddTextToWordDocument(targetFilepath, run.Descendants<Text>().First().Text);
                        }
                    }
                
                }
            }
    
            public static void OpenAndAddTextToWordDocument(string filepath, string txt)
            {
                // Open a WordprocessingDocument for editing using the filepath.
                WordprocessingDocument wordprocessingDocument =
                    WordprocessingDocument.Open(filepath, true);
    
                // Assign a reference to the existing document body.
                Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    
                // Add new text.
                Paragraph para = body.AppendChild(new Paragraph());
                RunProperties runProperties1 = new RunProperties();
                RunStyle runStyle1 = new RunStyle() { Val = "Heading1Char" };
                runProperties1.Append(runStyle1);
    
                Run run = para.AppendChild(new Run());
    
                run.Append(runProperties1);
                run.AppendChild(new Text(txt));
    
                // Close the handle explicitly.
                wordprocessingDocument.Close();
            }
    
            // Return true if the style id is in the document, false otherwise.
            public bool IsStyleIdInDocument(string filepath,
                string styleid)
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
                {
                    // Get access to the Styles element for this document.
                    Styles s = doc.MainDocumentPart.StyleDefinitionsPart.Styles;
    
                    // Check that there are styles and how many.
                    int n = s.Elements<Style>().Count();
                    if (n == 0)
                        return false;
    
                    // Look for a match on styleid.
                    Style style = s.Elements<Style>()
                        .Where(st => (st.StyleId == styleid) && (st.Type == StyleValues.Paragraph))
                        .FirstOrDefault();
                    if (style == null)
                        return false;
    
                    return true;
                }
    
            }
    
            // Create a new style with the specified styleid and stylename and add it to the specified
            // style definitions part.
            private static void AddNewStyle(string filepath, Style style)
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Open(filepath, true))
                {
    
                    // Get the Styles part for this document.
                    StyleDefinitionsPart styleDefinitionsPart =doc.MainDocumentPart.StyleDefinitionsPart;
                    // Get access to the root element of the styles part.
                    Styles styles = styleDefinitionsPart.Styles;
                    // Add the style to the styles part.
                    styles.InsertAt(style, 0);
                    //styles.Append(style);
                }
    
    
            }
    
    
        }

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 9, 2014 3:17 AM
    Moderator