void AddAltChunk(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
        {
            string altChunkId = "AltChunkId" + id;
            id++;
            byte[] byteArray = filename.OpenBinary();

            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
            AlternativeFormatImportPartType.WordprocessingML, altChunkId);

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);
                mem.Seek(0, SeekOrigin.Begin);
                chunk.FeedData(mem);
            }

            Word.AltChunk altChunk = new Word.AltChunk();
            altChunk.Id = altChunkId;

            // Replace content control with altChunk information.
            OpenXmlElement parent = sdt.Parent;
            parent.InsertAfter(altChunk, sdt);
            sdt.Remove();
        }
        void ImportProjectsAndMilestones(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile spreadsheetFileName)
        {
            ArrayList cellText = new ArrayList();

            // Create a Word table.
            Word.Table tbl = new Word.Table();
            Word.TableProperties tblPr = new Word.TableProperties();
            Word.TableStyle tblStyle = new Word.TableStyle();
            tblStyle.Val = "LightShading-Accent1";
            tblPr.AppendChild(tblStyle);

            Word.TableWidth tblW = new Word.TableWidth();
            tblW.Width = "5000";
            tblW.Type = Word.TableWidthUnitValues.Pct;
            tblPr.Append(tblW);
            tbl.AppendChild(tblPr);
            byte[] byteArray = spreadsheetFileName.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true))
                {
                    WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
                    WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet, "Sheet1");

                    Excel.SheetData sheetData =
                       worksheetPart.Worksheet.GetFirstChild<Excel.SheetData>();

                    foreach (Excel.Row r in sheetData)
                    {
                        foreach (Excel.Cell c in r)
                        {
                            cellText.Add(XLGetCellValue(c, workbookPart));
                        }
                        Word.TableRow tr = CreateRow(cellText);
                        tbl.Append(tr);
                        cellText = new ArrayList();
                    }
                }
            }
            // Swap out the content control for the SmartArt.
            OpenXmlElement parent = sdt.Parent;
            parent.InsertAfter(tbl, sdt);
            sdt.Remove();
        }
        void ImportSmartArtFromPowerPoint(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename)
        {
            string docLayoutPartId = "";
            string docDataPartId = "";
            string docColorsPartId = "";
            string docStylePartId = "";

            byte[] byteArray = filename.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                using (PresentationDocument myPres = PresentationDocument.Open(mem, true))
                {
                    PresentationPart presPart = myPres.PresentationPart;
                    // Get the slide that contains the SmartArt graphic.
                    SlidePart slide = (SlidePart)presPart.GetPartById("rId3");
                    // Get all the appropriate parts associated with the SmartArt.
                    DiagramLayoutDefinitionPart layoutPart =
                       slide.DiagramLayoutDefinitionParts.First();
                    DiagramDataPart dataPart = slide.DiagramDataParts.First();
                    DiagramColorsPart colorsPart = slide.DiagramColorsParts.First();
                    DiagramStylePart stylePart = slide.DiagramStyleParts.First();

                    // Get some of the appropriate properties off the SmartArt graphic.
                    PPT.GraphicFrame graphicFrame =
                       slide.Slide.Descendants<PPT.GraphicFrame>().First();
                    PPT.NonVisualDrawingProperties drawingPr = graphicFrame
                       .Descendants<PPT.NonVisualDrawingProperties>().First();
                    Draw.Extents extents =
                       graphicFrame.Descendants<Draw.Extents>().First();

                    // Import SmartArt into the Word document.
                    // Add the SmartArt parts to the Word document.
                    DiagramLayoutDefinitionPart docLayoutPart =
                       mainPart.AddPart<DiagramLayoutDefinitionPart>(layoutPart);
                    DiagramDataPart docDataPart =
                       mainPart.AddPart<DiagramDataPart>(dataPart);
                    DiagramColorsPart docColorsPart =
                       mainPart.AddPart<DiagramColorsPart>(colorsPart);
                    DiagramStylePart docStylePart =
                       mainPart.AddPart<DiagramStylePart>(stylePart);
                    // Get all the relationship ids of the added parts.
                    docLayoutPartId = mainPart.GetIdOfPart(docLayoutPart);
                    docDataPartId = mainPart.GetIdOfPart(docDataPart);
                    docColorsPartId = mainPart.GetIdOfPart(docColorsPart);
                    docStylePartId = mainPart.GetIdOfPart(docStylePart);

                    // Use the document reflector to figure out how to add a SmartArt
                    // graphic to Word.
                    // Change attribute values based on specifics related to the SmartArt.
                    Word.Paragraph p = new Word.Paragraph(
                       new Word.Run(
                       new Word.Drawing(
                       new WP.Inline(
                       new WP.Extent() { Cx = extents.Cx, Cy = extents.Cy },
                       new WP.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                       new WP.DocProperties() { Id = drawingPr.Id, Name = drawingPr.Name },
                       new WP.NonVisualGraphicFrameDrawingProperties(),
                       new Draw.Graphic(
                       new Draw.GraphicData(
                       new Dgm.RelationshipIds()
                       {
                           DataPart = docDataPartId,
                           LayoutPart = docLayoutPartId,
                           StylePart = docStylePartId,
                           ColorPart = docColorsPartId
                       }) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/diagram" }))
                       {
                           DistanceFromTop = (UInt32Value)0U,
                           DistanceFromBottom = (UInt32Value)0U,
                           DistanceFromLeft = (UInt32Value)0U,
                           DistanceFromRight = (UInt32Value)0U
                       })));

                    // Swap out the content control for the SmartArt.
                    OpenXmlElement parent = sdt.Parent;
                    parent.InsertAfter(p, sdt);
                    sdt.Remove();
                }
            }
        }
        void ImportChartFromSpreadsheet(MainDocumentPart mainPart, Word.SdtElement sdt,
   SPFile spreadsheetFileName)
        {
            // Create a paragraph that has an inline drawing object.
            Word.Paragraph p = new Word.Paragraph();
            Word.Run r = new Word.Run();
            p.Append(r);
            Word.Drawing drawing = new Word.Drawing();
            r.Append(drawing);
            // These dimensions work perfectly for the template document.
            WP.Inline inline = new WP.Inline(new WP.Extent() { Cx = 5486400, Cy = 3200400 });
            byte[] byteArray = spreadsheetFileName.OpenBinary();

            using (MemoryStream mem = new MemoryStream())
            {
                mem.Write(byteArray, 0, (int)byteArray.Length);

                // Open the Excel spreadsheet.
                using (SpreadsheetDocument mySpreadsheet = SpreadsheetDocument.Open(mem, true))
                {
                    // Get all of the appropriate parts.
                    WorkbookPart workbookPart = mySpreadsheet.WorkbookPart;
                    WorksheetPart worksheetPart = XLGetWorksheetPartByName(mySpreadsheet,
                       "Sheet2");
                    DrawingsPart drawingPart = worksheetPart.DrawingsPart;
                    ChartPart chartPart = (ChartPart)drawingPart.GetPartById("rId1");

                    // Clone the chart part and add it to the Word document.
                    ChartPart importedChartPart = mainPart.AddPart<ChartPart>(chartPart);
                    string relId = mainPart.GetIdOfPart(importedChartPart);

                    // The frame element contains information for the chart.
                    GraphicFrame frame =
                       drawingPart.WorksheetDrawing.Descendants<GraphicFrame>().First();
                    string chartName = frame.NonVisualGraphicFrameProperties.NonVisualDrawingProperties.Name;
                    // Clone this node so that you can add it to the Word document.
                    Draw.Graphic clonedGraphic = (Draw.Graphic)frame.Graphic.CloneNode(true);
                    ChartReference c = clonedGraphic.GraphicData.GetFirstChild<ChartReference>();
                    c.Id = relId;

                    // Give the chart a unique ID and name.
                    WP.DocProperties docPr = new WP.DocProperties();
                    docPr.Name = chartName;
                    docPr.Id = GetMaxDocPrId(mainPart) + 1;

                    // Add the chart data to the inline drawing object.
                    inline.Append(docPr, clonedGraphic);
                    drawing.Append(inline);
                }
            }
            OpenXmlElement parent = sdt.Parent;
            parent.InsertAfter(p, sdt);
            sdt.Remove();
        }