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-11-08T00:25:15+00:00

    There is definitely a difference then between the enterprise and what I and many of our customers are running. I don't have the enterprise version. Since I first posted about this months ago everyone who has tried Enterprise versions has said it worked, but non-enterprise versions are the ones that are consistently failing.

    I just upgraded 2 of my PCs to the current build "Microsoft Word for Microsoft 365 MSO (Version 2210 Build 16.0 15726.20070) 64-bit (that's what the About screen shows) but the Account screen says Version 2210 (Build 15726.20174 Click to Run) Current Channel and both are still failing with the original InsertFile error code 5097 that goes back to the Aug 3 build. And I continue to get more customers computers encountering it. Running Windows 11 Pro (current version).

    Also reinstalled Office on both computers.

    I really need to figure out these glitches to have proper working product again. Everything else so far are just hacks and bandaids.

    I know I can add an extra process to find and delete all the bullet characters but its kind of a cleanup hack that shouldn't have to be done. But why would it work fine when inserting the document via the toolbar (when I record a macro of it as well, the macro is only a single line which is Word calling the InsertFile function with the same parameters the VBA code uses). But via VBA it adds this extra bullet graphic.

    Thanks for the input. Hoping someone somewhere sees this and has some kind if insight or something that helps point me in a direction for something to try.

    Was this answer helpful?

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

    Using a 64-bit installation of Microsoft 365 Apps for enterprise Version 2210(Build 15726.20174 Click-to-Run) Current Channel on Windows 11 Pro Version 21H2, OS Build 22000.1098, no errors were received when running the code in your original post after making suitable modifications in the code for the path\filename of the SourceFile and the FileToInsert.

    I am not sure from where that extraneous character comes, but it can be deleted by adding the following command to the code immediately after the DocRng.InsertFile

    DocRng,Paragraphs(1).Range.Characters(1).Delete

    If I try to get the ASC code for the character, it returns 1

    In the backstage Print view, the character appears in the position shown below, which is also where it appears if the document is saved as a pdf.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-11-07T21:18:40+00:00

    InsertFile seems to be breaking down in multiple ways now. Still having it crash with the 5097 error when calling InsertFile to insert one file into another, but when using Word's toolbar on the same files selecting Insert > Object > Text from File and picking the file to insert it works fine, where InsertFile returns 5097 (nothing new there, just still doing that since the Aug 3 build).

    But now I'm seeing a different behavior with InsertFile that works fine via the Word toolbar. I've been rebuilding the templates that fail by creating a new document and copy/pasting the contents into the new doc. Then InsertFile is working when inserting the the rebuilt template - but as noted in the post above it is introducing a bullet graphic right before the inserted contents.

    I just tried using Word's Insert > Object > Text from File via the Word tool bar and it inserts the file without introducing the extra bullet graphic. Using InsertFile on the same file always introduces the bullet graphic - but with the rebuilt docs doesn't crash with the 5097 error.

    Still can't figure out any clean way to get InsertFile working properly anymore.

    Test Files

    This zip file contains the 2 rebuilt templates I'm using InsertFile on. Via VBA the ExecSummary.docx is opened, a page break is inserted at the end, then InsertFile is called to insert the 2nd file. The results are like in the screenshot in the previous comment where a bullet is inserted before the contents of the file. These same 2 files using the Word toolbar work fine without a bullet added.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-11-07T19:49:56+00:00

    Hi Doug,

    MSFT has done a couple Word builds now and still InsertFile is crashing with that original problem. In the meantime I've been experimenting with using the Copy/Paste option to rebuild templates and while it fixes the problem with InsertFile returning errors. It is introducing a glitch I can't figure out that's never happened with InsertFile before either.

    I took a set of the failing templates (and I've done this manually using the toolbar and programmatically using VBA) and same results. I can create a new document, insert my custom set of Word Styles. One of which is updating the Bullet style with a custom graphic for the bullet point. Then open the original template, do a copy/paste into the new doc and save the new doc.

    Then I assemble them all using InsertFile and it doesn't crash InsertFile with the 5907 error anymore and the document assembles - but.

    Every time InsertFile inserts a new file and I stop it on that line in the debugger as well to ensure its InsertFile. It has inserted the custom bullet point graphic as the 1st character in the page in front of the content from the file. The files its inserting have no leading character. If I highlight the bullet graphic it isn't in the Bullet Word style either. Its set to Normal and it doesn't act like a bullet point (no indenting and doesn't create a new bullet if you create a new paragraph under it). I've been trying all sorts of things like removing the use of the Bullet style and removing the custom graphic from the Bullet style but it still inserts that graphic. Only if I do not have any bullet points in a template and the copy/paste has not changed the Bullet style does it not insert a bullet graphic ahead of the content of the inserted file.

    In the attached pic is the original template that is inserted using InsertFile on the left. On the right is the document InsertFile inserted the template into after having added a page break. It does this on every template it inserts. Same code that I've been using to use InsertFile for decades, never in any other case in any edition of Word with any templates we've used or any of our customers have used over this time has InsertFile stuck an extra character into the document. Only cropped up since I've been trying to come up with a solution to rebuild these templates using Copy/Paste.

    Image

    And this is also only happening with the templates I've rebuilt by starting with a blank New document and copy/pasting the old content into. One of my other workarounds I experimented with that got around the InsertFile failing was to SaveAs the .docx file as a .doc legacy file and then use InsertFile on the legacy format file - and that gets around the InsertFile error and also doesn't introduce this artifact.

    I've also reproduced this on a 2nd computer running the current build of Word and with multiple sets of our templates that have different graphic designs and bullets. And it uses the bullet graphic from whatever document is being inserted.

    Any idea what could be causing InsertFile to add an extra element like this into the document?

    Was this answer helpful?

    0 comments No comments