View Single Post
Old 10-09-2019, 10:40 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Join Date: Aug 2014
Posts: 3,008
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice

The issue here relates to how you are converting the document (which is not shown in your example) and the reason you cannot kill the file is that it is held open by the lock file. If you look at the folder with the hidden files diplayed at the point of crashing you will see lock files for both the DOT file and the new DOTM/X file.

If you use a method similar to that shown below, the problem does not occur:
Sub Macro1()
Dim strTempName As String
Dim oDoc As Document
Dim oNewDoc As Document
Dim strNewName As String
    Set oDoc = ActiveDocument
    strTempName = oDoc.FullName
    If Right(LCase(oDoc.Name), 3) = "dot" Then
        If oDoc.HasVBProject Then
            strNewName = oDoc.FullName & "m"
            oDoc.SaveAs2 _
                    FileName:=strNewName, _
                    FileFormat:=wdFormatXMLTemplateMacroEnabled, _
            strNewName = oDoc.FullName & "x"
            oDoc.SaveAs2 _
                    FileName:=strNewName, _
                    FileFormat:=wdFormatXMLTemplate, _
        End If
    End If
    oDoc.Close 0
    Kill strTempName
    Set oDoc = Documents.Open(strNewName)
End Sub
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes
Reply With Quote