wordpress hit counter
Re: cannot open word document after merge c# - .Net - Development Tools - OpenXML Developer

Re: cannot open word document after merge c#

Development Tools

Discussions about working with Open XML using a wide range of development tools

cannot open word document after merge c#

  • rated by 0 users
  • This post has 2 Replies |
  • 1 Follower
  • Hi,

    I used open xml c# to generate word document but I cannot open it . the error message is like this :

    Can not open file problems were detected in its content  (I've translated message error from french)

       private void MergeSourceDocument(int id,string fieldName, byte[] buffer, string destinationFile)
            {
                if (!System.IO.File.Exists(destinationFile))
                {
                    throw new FaultException("destination File Not Found ", new FaultCode("1"));
                }
                try
                {
                    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(destinationFile, true))
                    {
                        MainDocumentPart mainPart = myDoc.MainDocumentPart;
                        List<SdtElement> sdtList = mainPart.Document.Descendants<SdtElement>().Where(
                             s => fieldName.Contains(s.SdtProperties.GetFirstChild<Tag>().Val.Value)).ToList();
                        if (sdtList.Count != 0)
                        {
                            string mergedFieldId = "mergedFieldId" + id;
                            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                                AlternativeFormatImportPartType.WordprocessingML, mergedFieldId);
                            chunk.FeedData(new MemoryStream(buffer));
                            AltChunk altChunk = new AltChunk();
                            altChunk.Id = mergedFieldId;
                            
                            foreach (SdtElement sdt in sdtList)
                            {
                                OpenXmlElement parent = sdt.Parent;
                                try
                                {
                                    parent.InsertAfter(altChunk, sdt);
                                    sdt.Remove();
                                }
                                catch (Exception ex) { Log.Write(ex.Message, LogSeverity.Information); }
                            }
                            mainPart.Document.Save();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw DataAccessExceptionHelper.GetSoapException(ex);
                }
            }

     

    Please Help

  • Hi,

    It looks as though you are inserting the same altChunk content multiple times in the one document.  I am not sure that this is supported.  You may need to insert a separate part for each altChunk element that you insert into the document.

    Also, from your example it is not clear what is in your buffer.  What are you inserting?  Another word document?  Html?  If you are inserting invalid content, it could also cause the error you are seeing.

    -Eric

  • Hi,

    I'm inserting  text coming from database fields

Page 1 of 1 (3 items)