ZOrder of Shape objects changes when Word document reopened. RRS feed

  • Question

  • I have a VBA script that copies the shape objects from the header of a Word document and then pastes them into the body of the page.

    When the debugger is stopped right before the Save As command the Word document is correct with all of the shape objects in the correct ZOrder.

    After saving and closing the document as a standard Word .docx format file.

    Then reopening the document the ZOrder of one of the shape objects has changed.

    If I reposition it manually via Word's normal editing (not VBA script) then save the file it is in the correct ZOrder.

    Running Windows 10 with Office 365 (current update).

    What might be causing the ZOrder to change?   Is there something programattically I should be doing differently?

    The code snippet below basically groups the shapes in the header, cuts the group, pastes the group into the body of the page.  Some cleanup is done after this as some shapes are not aligned or sized the same after the group is pasted.  Then the group is moved to the back to ensure all the shapes are behind anything that was previously on the page.  Note there is no body text in the page.  The main body of the page only has text shapes and other shape objects (no inline shapes).  So that the zorder of everything can be controlled.

    This all works correctly but as soon as the document is saved then reopened, the reopened version has the zorder changed.

    The attached image shows the before and after.   The left image is the original with 1 picture and 3 shapes in the header that are cut and paste from the header to the body.  It looks correct until saved and reopened.  The right picture is what the document looks like after it is reopened.  The blue shape object has been moved forward.

    ActiveWindow.ActivePane.View.Type = wdPrintView If (ActiveDocument.Sections.Count = 1) Then If (ActiveDocument.Sections(1).Headers.Count > 0) Then For jj = 1 To ActiveDocument.Sections(1).Headers.Count If (ActiveDocument.Shapes.Count > 1) Then ActiveDocument.Shapes.SelectAll Selection.ShapeRange.Group End If ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader Selection.WholeStory Selection.Cut ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument Selection.Paste

    ' Some code here moves the group to the back to ensure its behind shapes that were in the main body of the page.

    Selection.Collapse RepeatUngroup: For ii = 1 To ActiveDocument.Shapes.Count If (ActiveDocument.Shapes(ii).Type = msoGroup) Then ActiveDocument.Shapes(ii).Ungroup GoTo RepeatUngroup End If Next ii Next jj End If End If

    Ian Lauder

    Thursday, May 2, 2019 5:24 AM

All replies

  • I have since discovered that when looking at the Shape objects in the Word VBA debugger that the .ZOrderPosition  seems to be corrupted in the documents this happens in.

    The .ZOrderPosition should match the index of the shape objects (i.e. 1,2,3,4,5, etc.).

    But in the documents that are failing one of the .ZOrderPosition values will be random large values, but I can't seem to fix the values without manually rebuilding the document by hand.

    This seems to have been introduced when these documents were originally created in older versions of Word in .doc format and since then have been resaved as .docx in the current Word 2016 formats.

    Ian Lauder

    Sunday, June 2, 2019 8:00 PM