none
Delete template when closing document RRS feed

  • Question

  • Hello everyone,

    I have the following problem: out of a .dotm template are created .dotx documents, and the user can make inputs through a UserForm.
    When the document or rather possible is too when the UserForm is closed, the reference to the template should be removed and the template deleted.
    I tried this with the following code:

    Private Sub Document_Close() 'or rather Private Sub UserForm_Terminate()

        If ActiveDocument.Type = 0 Then 'Document is closed, not template

            Dim pathtempl As String
            pathtempl = ThisDocument.FullName

            ActiveDocument.AttachedTemplate = "" 'Remove reference to template

            SetAttr pathtempl, vbNormal 'Remove potential read only property

            Kill pathtempl 'Delete template

        End If

    End Sub

    All commands are executed except Kill, it is shown every time Runtime-Error 70: Permission denied.
    Can anybody help me?
    Thank you!

                                            
    Sunday, December 17, 2017 9:24 AM

All replies

  • Johannes,
    re: deleting file

    I believe you have to close the file before you can kill it.

    '---
    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)
    Sunday, December 17, 2017 7:25 PM
  • I thought when I delete the reference from the document to the template, the template is not relevant any more. Is there any possibility to delete a file on closing?

    Thank you very much!

    Monday, December 18, 2017 7:47 AM
  • File suicide is not an option.  I just tried it.
    Close the file...

    Then Kill the file from another file using the path and file name...
      Kill C:\Excel Files\Use For Testing\MyFileName

    '---
    Jim Cone
    Portland, Oregon USA
    https://goo.gl/IUQUN2 (Dropbox)
    (free & commercial excel add-ins & workbooks)
    Monday, December 18, 2017 2:47 PM
  • But yes I know a solution for file suicide in Excel through VBA, but this does not work in Word, does anyone know a solution?
    Monday, December 18, 2017 9:31 PM
  • Word is not Excel. What you have is a classic Catch 22 situation. You cannot delete a document/template that is open in Word and you cannot continue to run code from a document/template that has been closed.

    What might work is to start with a macro enabled document rather than a template and before closing it save that document as an ordinary non-macro enabled document. The code will stay (and still run) in the document until you close it.

    Try the following test code in a new document. Save it as a macro enabled document. Then run the macro. The macro enabled document should have been removed from the folder leaving a non-macro enabled document.

    This would fulfil your requirement of removing the code AND the template. You just have to distribute copies of your macro enabled document.

    Sub Macro1()
    Dim oDoc As Document
    Dim strPath As String
    Dim strNewName As String
        Set oDoc = ActiveDocument
        oDoc.Save
        strPath = oDoc.FullName
        strNewName = Replace(strPath, ".docm", ".docx")
        oDoc.SaveAs2 strNewName, wdFormatDocumentDefault
        Kill strPath
        oDoc.Close 0
    End Sub
    


    Graham Mayor - Word MVP
    www.gmayor.com

    Tuesday, December 19, 2017 9:11 AM
  • I found the following solution with a Bookmark "loc":

    If this is inserted in an <.dotm> template, it will delete itself on closing.

    Private Sub Document_Close()
                
                If ActiveDocument.Type = wdTypeTemplate Then
                    
                    Dim locb As String
                    Dim cRange As Range
                    
                    If ActiveDocument.Bookmarks.Exists("loc") Then
                    Set cRange = ActiveDocument.Bookmarks("loc").Range
                        locb = cRange.Text
                    End If
                    With ActiveDocument.Bookmarks
                        .Add Range:=cRange, Name:="loc"
                    End With
                                
                    SetAttr locb, vbNormal
                    
                    Kill locb
                    
                Else:
                    
                    Dim bRange As Range
        
                    If ThisDocument.Bookmarks.Exists("loc") Then
                    Set bRange = ThisDocument.Bookmarks("loc").Range
                        bRange.Text = ThisDocument.FullName
                    End If
                    With ThisDocument.Bookmarks
                        .Add Range:=bRange, Name:="loc"
                    End With
                    
                    ActiveDocument.AttachedTemplate.Save
                    
                    Dim loca As String
                    loca = ThisDocument.FullName
                    
                    ActiveDocument.AttachedTemplate = ""
                    ActiveDocument.Saved = True
    
                    Documents.Add Template:=loca, NewTemplate:=True
    
                    Application.Quit
                
                End If
    
    End Sub

    Tuesday, December 19, 2017 4:05 PM