1

After adding one or more images to a Word document, I try to open it using Word Interop. At random, the document is corrupted and will throw a COMException "the file may be corrupted".

While trying to determine a cause for this corruption, I used the same document, images, ... over and over again. When my application starts, there's about 70% chance that it will open without issues. Then it will continue to regenerate and open just fine, until one random point after a few attempts where I'd get that COMException. After that, it will continue to throw that exception until I restart the application.

The corrupted document and the one that opens show no differences in the generated XML, apart from the ID's that are generated for the images.

  • .NET Target: .NET Framework 4.6.1
  • DocumentFormat.OpenXml Version: 2.11.3

The code to add the image:

        var element = new Drawing
        (
            new DW.Anchor
            (
                simPos,
                horPos,
                verPos,
                new DW.Extent() { Cx = dimensions.ImageWidth, Cy = dimensions.ImageHeight },
                new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                wrapper,
                new DW.DocProperties() { Id = (X.UInt32Value)ticks, Name = $"Picture {ticks}" },
                new DW.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoChangeAspect = true }),
                new A.Graphic
                (
                    new A.GraphicData
                    (
                        new PIC.Picture
                        (
                            new PIC.NonVisualPictureProperties
                            (
                                new PIC.NonVisualDrawingProperties() { Id = (X.UInt32Value)ticks, Name = $"New Bitmap Image {ticks}.jpg" },
                                new PIC.NonVisualPictureDrawingProperties()
                            ),
                            new PIC.BlipFill
                            (
                                new A.Blip(new A.BlipExtensionList(new A.BlipExtension { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }))
                                {
                                    Embed = relationshipId,
                                    CompressionState = A.BlipCompressionValues.None,
                                },
                                new A.Stretch(new A.FillRectangle())
                            ),
                            new PIC.ShapeProperties
                            (
                                new A.Transform2D
                                (
                                    new A.Offset() { X = 0L, Y = 0L },
                                    new A.Extents() { Cx = dimensions.ImageWidth, Cy = dimensions.ImageHeight }
                                ),
                                new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }
                            )
                        )
                    )
                    { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
                )
            )
            {
                SimplePos = X.BooleanValue.FromBoolean(false),
                BehindDoc = X.BooleanValue.FromBoolean(true),
                LayoutInCell = X.BooleanValue.FromBoolean(true),
                AllowOverlap = X.BooleanValue.FromBoolean(true),
                Locked = X.BooleanValue.FromBoolean(false),
                RelativeHeight = 500U,
                DistanceFromBottom = 0U,
                DistanceFromLeft = 114300U,
                DistanceFromRight = 114300U,
                DistanceFromTop = 0U
            }
        );

The generated XML for the image is as follows:

<w:drawing xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="500" behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
    <wp:simplePos x="0" y="0" />
    <wp:positionH relativeFrom="character">
      <wp:posOffset>0</wp:posOffset>
    </wp:positionH>
    <wp:positionV relativeFrom="line">
      <wp:posOffset>0</wp:posOffset>
    </wp:positionV>
    <wp:extent cx="2331720" cy="2935224" />
    <wp:effectExtent l="0" t="0" r="0" b="0" />
    <wp:wrapNone />
    <wp:docPr id="1747262326" name="Picture 637338535332223862" />
    <wp:cNvGraphicFramePr>
      <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1" />
    </wp:cNvGraphicFramePr>
    <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
      <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
        <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
          <pic:nvPicPr>
            <pic:cNvPr id="1747262326" name="New Bitmap Image 637338535332223862.jpg" />
            <pic:cNvPicPr />
          </pic:nvPicPr>
          <pic:blipFill>
            <a:blip r:embed="ID_465174ea-d95b-48b5-b6a8-451afacf449b" cstate="none" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
              <a:extLst>
                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}" />
              </a:extLst>
            </a:blip>
            <a:stretch>
              <a:fillRect />
            </a:stretch>
          </pic:blipFill>
          <pic:spPr>
            <a:xfrm>
              <a:off x="0" y="0" />
              <a:ext cx="2331720" cy="2935224" />
            </a:xfrm>
            <a:prstGeom prst="rect">
              <a:avLst />
            </a:prstGeom>
          </pic:spPr>
        </pic:pic>
      </a:graphicData>
    </a:graphic>
  </wp:anchor>
</w:drawing>

If the file opens 5 times, I'd think there'd be no issue the 6th time too right? I'm completely lost as to why this would cause corrupted files at random.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Run these tests with the installed anti-malware product temporarily disabled. – Hans Passant Aug 24 '20 at 12:36
  • With both antivirus and firewall disabled --> all runs fail immediately With antivirus disabled and firewall enabled --> no failure received both enabled again --> started failing after 6 runs – LambdaSigma Aug 24 '20 at 12:47

0 Answers0