Word VBA function InsertFile is now failing on all builds from 15427.20194 Aug 3, 2022 and later

Anonymous
2022-09-11T17:03:40+00:00

We have a product that assembles custom templates that has been around for about 20 years that operates on a library of about 1 million Word .docx format templates.  With users across the world.  Its been in use on every version of Word since 2003 (currently only supported on all versions of Office 365 but it also still works on Word 2010).   We use the Word VBA function InsertFile to append pages together to create custom documents from the template library.

Recently all of our installations started failing with the InsertFile function returning error 5097 - "Word has encountered a problem".  And customers have started reporting the same error.

We traced it down to being introduced in the Aug 3 Build of Word and verified it on multiple computers (both Windows 10 and Windows 11).   If we roll back our Word builds through the last 7 builds we found that in every case the June 14, June 29, July 6, July 18 builds of Word work fine.   But all 3 builds after that which are Aug 3, Aug 9 and the current Aug 31 fail.

Something appears to have been recently broken by Microsoft in the Aug 3 Build that causes the Word's InsertFile function to fail.    This happens on almost all of our Word .docx template we have tested.    But when inserting the file via the Word toolbar's Insert > Text From File > browse to the file it works.  But it always fails now when calling the InsertFile function via VBA.   Again, this is not a new macro or templates.  This code has been in use for almost 2 decades and the templates have been in use for for many years.   Its only since the recent builds of Word in the last month that installations are now starting to fail everywhere.

This zip file linked below contains the macro vba test function and some example files to reproduce the problem.

InsertFile Failure Test

When we save those templates that are failing to insert as legacy Word 2003-2007 .doc files they do insert without failing.   And if we save them in Word 2003-2007 XML or as an OpenDocument .odt file they insert correctly.   But if we save them as Word XML .xml or Strict Word XML .xml they also fail.  So there is nothing corrupt in the files, it appears to be inserting current format files of various file formats are failing, but saving those failing files as legacy formats they insert correctly.

How do we submit this problem to Microsoft's Office 365 team as a bug report? This is a serious problem that breaks our product and requires us to have everyone roll back their Office 365 to the July 18, 2022 build and we're in the process of having to update our product to provide instructions when the error occurs on how to roll back Office 365.

The code in the test script is also here. But it needs the .docx files from the ZIP file to operate on.

Sub InsertTest()

Dim DocRng As Range  
Dim FileToInsert As String  
Dim SourceFile As String  
Dim MsgStr As String  
Dim MsgResult As Long  
          
On Error GoTo ErrHandler  
 
SourceFile = "c:\temp\CoverLetter.docx"  
  
FileToInsert = "c:\temp\ExecSummaryA.docx"      ' This file fails to insert  

' FileToInsert = "c:\temp\ExecSummaryB.doc" ' This file works (this was the ExecSummaryA.docx Saved As a .doc file in Word 2003-2007 format)
' FileToInsert = "c:\temp\AccessoriesA.docx" ' This file fails to insert
' FileToInsert = "c:\temp\AccessoriesB.doc" ' This file works (this was the AccessoriesA.docx Saved As a .doc file in Word 2003-2007 format)

Documents.Open FileName:=SourceFile  

ActiveWindow.ActivePane.View.Type = wdPrintView  
  
Set DocRng = ActiveDocument.Content  

DocRng.Collapse (wdCollapseEnd)  
              
DocRng.InsertFile FileName:=FileToInsert, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False  

Exit Sub  
  

ErrHandler:

MsgStr = "Error: " & Err.Number & " - " & Err.Description  

MsgResult = MsgBox(MsgStr, vbSystemModal + vbMsgBoxSetForeground, "Insert Error")  

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2023-03-01T15:20:19+00:00

Great news. The 2302 patch just came out, tested it on a couple computers here and the InsertFile function is now working properly.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

66 additional answers

Sort by: Newest
  1. Anonymous
    2022-10-05T14:00:44+00:00

    These are interesting tests for sure, and I've done some of them as well. One of my temporary workarounds is instead of using InsertFile I have my software open the template and do copy/paste. But that is problematic as a software solution as its slower, plus my documents are fairly complex with up to 5 section breaks with sections headers/footers unlinked as the headers/footers are different between sections, and using copy/paste you have to also do copy/paste operations on the headers and footers as well as the body. And many pages have overlapping objects like photos with a colored shape object underneath and a full page shape object behind those that the software can change the colors of, and Word sometimes gets the zorder messed up pasting in a complex set of objects, which means having to individually copy every element and set the zorder. Its a huge amount of extra coding. And Word also messes up the footers in previous sections when pasting in a footer even if they are unlinked when doing it through VBA. Its a workaround that mostly gets around the problem for now, but its not a longterm solution.

    I also have a 2nd workaround option built into the software now that when right before the InsertFile I save the file to be inserted (the .docx file) to a .doc legacy format file, then insert the temporary .doc file, then delete it. Then the InsertFile function works. Again, this is not a long term solution either as I've found in testing that sometimes a background object or a footer disappears from the final assembled document. And its also slows down the software doing the extra file copy and delete.

    I'm not sure what you mean by rezipping the .docx files. Are you individually zipping the .docx files to .zip files, but then just renaming the file extension from .zip to .docx? Any solution that involves changing all the templates is problematic as well. I have over 1 million of these templates built into 1000 product installers, rebuilding every template by running somekind of batch would take months. And that doesn't solve the problem of all the customers in the field using templates, some of which have very customized systems and customized documents.

    The templates were created using VBA where I have hand created pages which are just plain text. Then a VBA script uses the Word functions to insert some graphics, set some colors, add some shape objects and saves them as .docx files. So if there is something glitchy in Word's InsertFile function that is balking at something in the .docx files during the InsertFile function, its probably something they overlooked in how something is stored in the file vs how 99% of people create their documents just all through the Word toolbar. If the templates work when saved in a .doc legacy format but don't work when resaved in a .docx format, that means something in how something is saved in the file carries forward that InsertFile doesn't like anymore (but its odd that it works if in .doc legacy format). But if using copy/paste to move that same content that was inserted via VBA functions to insert pictures is then inserted into a new document via Copy/Paste and that copy/paste version works, is something in the picture objects stored differently in the file that InsertFile is fine with when added via copy/paste or by hand using toolbar functions, but how its added via VBA functions is put into the file in a way its balking now?

    Also, if I run the test script in VBA and halt in the debugger right before the InsertFile function is called, but I then just click that open CoverLetter.docx file and use Word's Insert > Text from File toolbar option and pick the .docx file to insert it works. But continuing in the debugger and doing it through InsertFile it fails. So something is different in how the file is being inserted via the toolbar vs the InsertFile function.

    Was this answer helpful?

    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2022-10-05T07:47:46+00:00

    Hi Ian,

    Doing some more testing here on a Windows 11 machine, both AccessoriesA.docx and ExecSummaryA.docx fail to insert.

    However, if I open both of those documents and then use CTRL+A and then CTRL+C to copy the contents to the clipboard and then create a new, blank document and use CTRL+V to paste the contents into that document and then save them as AccessoriesA - Copy.docx and ExecSummaryA - Copy.docx, after making appropriate changes to the FileToInsert command, the InsertTest macro successfully inserts both the AccessoriesA - Copy.docx and ExecSummaryA - Copy.docx into the CoverLetter.docx

    Likewise, if I unzip the AccessoriesA.docx and ExecSummaryA.docx files and then without making any changes to the components of the documents I re-zip the files and change the file extension to .docx then the InsertTest macro successfully inserts those files into the CoverLetter.docx

    So, while I do not know what is causing the problem, performing both of the above procedures on the documents, that are to be inserted, eliminates the problem.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-10-05T06:00:54+00:00

    Thanks, hopefully I'll hear something vs waiting and checking every build as they come out. Even having some insight as to what it is about the templates that is causing the InsertFile to fail would help.

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323.6K Reputation points MVP Volunteer Moderator
    2022-10-05T04:10:50+00:00

    Hi Ian,

    I have sent a follow-up to the Product Team.

    Was this answer helpful?

    0 comments No comments