Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Can't open word document created by open XML in SharePoint 2010 Document Library

$
0
0

Hi, 

     I have created a word document by using Open XML and Visual studio sequential workflow in my SharePoint 2010  Document Library.But while I tried to Open the document it is showing an error message that 'the file ####.docx cannot be opened because there are problems with the contents. Screen shot attached.

The code snippet I used is 

using (MemoryStream memStream = new MemoryStream())
            {
                using (WordprocessingDocument doc = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document))
                {
                    var mainPart = doc.AddMainDocumentPart();
                    SPFile file = workflowProperties.Web.Lists["Candidate Documents"].RootFolder.SubFolders["Offer_Letter_Template"].Files["Offer_letter_sample.docx"];
                    Stream s = file.OpenBinaryStream();
                    using (WordprocessingDocument doctemp = WordprocessingDocument.Open(s, true)) //(workflowProperties.Web.Lists["Candidate Documents"].RootFolder.SubFolders["Offer_Letter_Template"].Files["Offer_letter_sample.docx"], true))
                    {
                        string docText = null;
                        using (StreamReader sr = new StreamReader(doctemp.MainDocumentPart.GetStream()))
                        {
                            docText = sr.ReadToEnd();
                        }
                        docText = Regex.Replace(docText, "<Designation>", litem["Designation"].ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "Doj", litem["Date Of Joining"].ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<Name>", litem["Title"].ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<Salary>", litem["CTC Offered"].ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<Responsibilities>", litem["Responsibilities"].ToString(), RegexOptions.IgnoreCase);

                        docText = Regex.Replace(docText, "<BASIC>", basic.ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<HRA>", hra.ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<PA>", pa.ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<TA>", ta.ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<EMPF>", empf.ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<NPF>", npf.ToString(), RegexOptions.IgnoreCase);

                        docText = Regex.Replace(docText, "<MBASIC>", (basic / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MHRA>", (hra / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MPA>", (pa / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MTA>", (ta / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MSALARY>", (salary / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MEMPF>", (empf / 12).ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<MNPF>", (npf / 12).ToString(), RegexOptions.IgnoreCase);

                        docText = Regex.Replace(docText, "<Responsibilities>", litem["Responsibilities"].ToString(), RegexOptions.IgnoreCase);
                        docText = Regex.Replace(docText, "<Address>", litem["Address"].ToString(), RegexOptions.IgnoreCase);
                        using (StreamWriter sw = new StreamWriter(doc.MainDocumentPart.GetStream(FileMode.Create)))
                        {
                            sw.Write(docText);
                        }

                    }
                    doc.MainDocumentPart.Document.Save();
                    
                }
                AddToSharePoint(memStream, docOffer, foldername);
            }

The same error is repeating for all the files I am creating using this method.

Hope you will help me to solve the issue. Thanks In advance.

Regards,





Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>