wordpress hit counter
Unspecified error - While opening MsWord 2010 - Open XML SDK 2.0 - Development Tools - OpenXML Developer

Unspecified error - While opening MsWord 2010

Development Tools

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

Unspecified error - While opening MsWord 2010

  • rated by 0 users
  • This post has 1 Reply |
  • 2 Followers
  • Hello Everyone, i written the code to replace the Text & image in bookmark in Msword using openxml2.0. After the execution of the following Code when we open Word2007 it working good. but when i trying the same process in Word2010.When we open the document it shows Unspecified error.

     Please tell me what is happening and whats wrong i did.... thank you...

     string strImgPath = "C:\\TestDocument\\2.jpg";
                string StrTempPath = "C:\\TestDocument\\NomalTemplate Relatório.dotx";
                string strReportPath = "C:\\TestDocument\\Test.docx";
                TemplateConvert(StrTempPath, strReportPath);
                using (var document = WordprocessingDocument.Open(strReportPath, true))
                {
                    var docPart = document.MainDocumentPart;
                    var doc = docPart.Document;
                    DocumentFormat.OpenXml.Wordprocessing.Paragraph imageParagraph = new Paragraph();
                    Word.Table imageTable;
                    imageTable = new DocumentFormat.OpenXml.Wordprocessing.Table();
                    OpenXmlElement baseBookMark;
                    baseBookMark = (from bm in document.MainDocumentPart.Document.Body.Descendants<BookmarkStart>()
                                    where bm.Name.ToString().ToUpper() == "RECOMMENDATION"
                                    select bm).FirstOrDefault();
                    var parent = baseBookMark.Parent;
                    Word.TableRow imageRow = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
                    Paragraph p = new Paragraph();
                    Run r = new Run();
                    ParagraphProperties p1 = new ParagraphProperties();
                    DocumentFormat.OpenXml.Wordprocessing.Text t = new DocumentFormat.OpenXml.Wordprocessing.Text("Image Testing");
                    r.Append(t);
                    p.Append(r);
                    Word.TableCell tc1;
                    tc1 = new DocumentFormat.OpenXml.Wordprocessing.TableCell(p);
                    imageRow.Append(tc1);
                    //imageTable.Append(imageRow);
                    Word.TableProperties tableProperties = new TableProperties(new TableWidth { Width = "4500", Type = TableWidthUnitValues.Dxa });
                    imageTable.Append(tableProperties);
                    var imagePart = docPart.AddNewPart<ImagePart>("image/jpg", null);
                    var imagePartId = docPart.GetIdOfPart(imagePart);
                    byte[] imageFileBytes;
                    Bitmap imageFile;
                    using (FileStream fsImageFile = File.OpenRead(strImgPath))
                    {
                        imageFileBytes = new byte[fsImageFile.Length];
                        fsImageFile.Read(imageFileBytes, 0, imageFileBytes.Length);
                        imageFile = new Bitmap(fsImageFile);
                    }
                    using (BinaryWriter writer = new BinaryWriter(imagePart.GetStream()))
                    {
                        writer.Write(imageFileBytes);
                        writer.Flush();
                    }
                    long cxHeight = (long)(100 * 10500L);
                    long cyWidth = (long)(100 * 10500L);
                    Body bd = new Body();
                    var element =
                               new DocumentFormat.OpenXml.Wordprocessing.Run(
                                   new DocumentFormat.OpenXml.Wordprocessing.RunProperties(
                                       new NoProof()),
                                   new Drawing(
                                       new DocumentFormat.OpenXml.Drawing.Wordprocessing.Inline(
                                           new DocumentFormat.OpenXml.Drawing.Wordprocessing.Extent() { Cx = cxHeight, Cy = cyWidth },
                                           new DocumentFormat.OpenXml.Drawing.Wordprocessing.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                                           new DocumentFormat.OpenXml.Drawing.Wordprocessing.DocProperties() { Id = (UInt32Value)1U, Name = "Picture 0", Description = "Test" },
                                           new DocumentFormat.OpenXml.Drawing.Wordprocessing.NonVisualGraphicFrameDrawingProperties(
                                               new DocumentFormat.OpenXml.Drawing.GraphicFrameLocks() { NoChangeAspect = true }),
                                           new DocumentFormat.OpenXml.Drawing.Graphic(
                                               new DocumentFormat.OpenXml.Drawing.GraphicData(
                                                   new DocumentFormat.OpenXml.Drawing.Pictures.Picture(
                                                       new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureProperties(
                                                           new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = "Test" },
                                                           new DocumentFormat.OpenXml.Drawing.Pictures.NonVisualPictureDrawingProperties()),
                                                       new DocumentFormat.OpenXml.Drawing.Pictures.BlipFill(
                                                           new DocumentFormat.OpenXml.Drawing.Blip() { Embed = imagePartId, CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Print },
                                                           new DocumentFormat.OpenXml.Drawing.Stretch(
                                                               new DocumentFormat.OpenXml.Drawing.FillRectangle())),
                                                       new DocumentFormat.OpenXml.Drawing.Pictures.ShapeProperties(
                                                           new DocumentFormat.OpenXml.Drawing.Transform2D(
                                                               new DocumentFormat.OpenXml.Drawing.Offset() { X = 0L, Y = 0L },                   
                                                          new DocumentFormat.OpenXml.Drawing.Extents() { Cx = cxHeight, Cy = cyWidth }),
                                                           new DocumentFormat.OpenXml.Drawing.PresetGeometry(
                                                               new DocumentFormat.OpenXml.Drawing.AdjustValueList()
                                                           ) { Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle }))
                                               ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                                       ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }));
    Word.TableCell tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
                    tc.Append(new Paragraph(new Run(element)));
                    imageRow.Append(tc);
                    imageTable.Append(imageRow);
                    baseBookMark.Parent.InsertBeforeSelf(imageTable);
    doc.Save();
     document.Close();
  • This is a tricky problem to solve, but I can offer some ideas without looking at the details of the code. First, I would try making a small change to the document in Word 2007, save it and then see if it opens in Word 2010. If it does, then compare the before and after to see what changed. It could be a very small difference, so the Productivity Tool can help you find the exact difference. If you need information about the Productivity Tool, see Eric White's blog post about it.

    If you cannot open the saved document in Word 2010, then I suggest you try to create the same (or similar) document manually in Word 2010, save it, and then compare to what you have working in Word 2007.

    Be sure you look carefully for differences, it doesn't have to be much. You can also experiment with making direct edits to the XML in the document to see if it eliminates the error.

Page 1 of 1 (2 items)