I have a Template document on the Desktop which I want to open, then it must ask me if I want to save a copy of the Template document to a New Document, and if so, I want to continue to work in the New Document. The Template document must be closed without saving. The code below create and opens a New Document but on the SaveCopyAs line, I get a Run-time error 1004 indicating Excel cannot access the New Document file although the document was created. Please help!
Private Sub Workbook_Open()
Dim answer As Integer
Dim TemplatePath, TemplateName As String
' Ask if a copy of the template document must be saved. If 'No', exit
answer = MsgBox("Save a copy of the template document as 'New Document'?", _
vbQuestion + vbYesNo + vbDefaultButton2)
If answer = vbNo Then Exit Sub
' Save the current filename and path
TemplateName = ThisWorkbook.Name
TemplatePath = ThisWorkbook.Path
' Save a copy of the Template as a new file with filename "New Document"
' Continue to work in the New Document. Close the Template document.
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs TemplatePath & "\New Document.xlsm"
Workbooks.Open (TemplatePath & "\New Document.xlsm")
Workbooks(TemplatePath & "\" & TemplateName).Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_Open()
Dim answer As Integer
Dim TemplatePath, TemplateName As String
' Ask if a copy of the template document must be saved. If 'No', exit
answer = MsgBox("Save a copy of the template document as 'New Document'?", _
vbQuestion + vbYesNo + vbDefaultButton2)
If answer = vbNo Then Exit Sub
' Save the current filename and path
TemplateName = ThisWorkbook.Name
TemplatePath = ThisWorkbook.Path
' Save a copy of the Template as a new file with filename "New Document"
' Continue to work in the New Document. Close the Template document.
Application.DisplayAlerts = False
ActiveWorkbook.SaveCopyAs TemplatePath & "\New Document.xlsm"
Workbooks.Open (TemplatePath & "\New Document.xlsm")
Workbooks(TemplatePath & "\" & TemplateName).Close SaveChanges:=False
Application.DisplayAlerts = True
End Sub