View Single Post
 
Old 10-09-2019, 10:40 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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:
Code:
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, _
                    CompatibilityMode:=Val(Application.Version)
        Else
            strNewName = oDoc.FullName & "x"
            oDoc.SaveAs2 _
                    FileName:=strNewName, _
                    FileFormat:=wdFormatXMLTemplate, _
                    CompatibilityMode:=Val(Application.Version)
        End If
    End If
    oDoc.Close 0
    DoEvents
    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 www.gmayor.com
Reply With Quote