Re: Kill File
Hi All,
I have a workbook which is sent to the end user via email.
On openning the attachment (.xls), the user has the option of filing a form and either:
1. Saving in desired location to come back to later and resume filling in, or
2. a "Completed & Send Back Button"
My issue is with point 2.
(Note: The Filled in data is placed onto the spreadsheet)
I would like the file to be "Temporarily Saved" then attached to send back.
I have been unlucky in attempting to save the File in the users own "Temp" Folder, so decided that the macro would save the spreadsheet to a common local area (i.. desktop), attach and send back, open a new instance of a workbook, paste macro and call this macro which woulf delete (Kill) the temporarily desktop saved file.
Below is a test macros of this process.
The macro works but doesnt kill the file.
However, if i call Sub Killme manually, the file DOES delete.
Any ideas??
Sub AddNewMacro()
ThisWorkbook.SaveAs "Deskptop Directory" & "\Test 1.xlsm"
Y = ThisWorkbook.Name
Set Newbook = Workbooks.Add
X = Newbook.Name
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim VBComp2 As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = Workbooks(X).VBProject
Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "Module1"
Dim LineNum As Long
Const DQUOTE = """"
Set VBProj = Workbooks(X).VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub Test()"
LineNum = LineNum + 1
.InsertLines LineNum, "Workbooks(" & DQUOTE & Y & DQUOTE & ").close SaveChanges:=False"
LineNum = LineNum + 1
.InsertLines LineNum, "Application.wait(Now + TimeValue(" & DQUOTE & "00:00:05" & DQUOTE & "))"
LineNum = LineNum + 1
.InsertLines LineNum, "Call Killme"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
Set VBComp2 = VBProj.VBComponents("Module2")
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub Killme()"
LineNum = LineNum + 1
.InsertLines LineNum, " Kill " & DQUOTE & "Deskptop Directory" & DQUOTE & "\Test 1.xlsm"
LineNum = LineNum + 1
.InsertLines LineNum, "Msgbox" & DQUOTE & "Done!" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
Workbooks(X).Activate
Application.Run (X & "!Test")
End Sub
Hi All,
I have a workbook which is sent to the end user via email.
On openning the attachment (.xls), the user has the option of filing a form and either:
1. Saving in desired location to come back to later and resume filling in, or
2. a "Completed & Send Back Button"
My issue is with point 2.
(Note: The Filled in data is placed onto the spreadsheet)
I would like the file to be "Temporarily Saved" then attached to send back.
I have been unlucky in attempting to save the File in the users own "Temp" Folder, so decided that the macro would save the spreadsheet to a common local area (i.. desktop), attach and send back, open a new instance of a workbook, paste macro and call this macro which woulf delete (Kill) the temporarily desktop saved file.
Below is a test macros of this process.
The macro works but doesnt kill the file.
However, if i call Sub Killme manually, the file DOES delete.
Any ideas??
Sub AddNewMacro()
ThisWorkbook.SaveAs "Deskptop Directory" & "\Test 1.xlsm"
Y = ThisWorkbook.Name
Set Newbook = Workbooks.Add
X = Newbook.Name
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim VBComp2 As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = Workbooks(X).VBProject
Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "Module1"
Dim LineNum As Long
Const DQUOTE = """"
Set VBProj = Workbooks(X).VBProject
Set VBComp = VBProj.VBComponents("Module1")
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub Test()"
LineNum = LineNum + 1
.InsertLines LineNum, "Workbooks(" & DQUOTE & Y & DQUOTE & ").close SaveChanges:=False"
LineNum = LineNum + 1
.InsertLines LineNum, "Application.wait(Now + TimeValue(" & DQUOTE & "00:00:05" & DQUOTE & "))"
LineNum = LineNum + 1
.InsertLines LineNum, "Call Killme"
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
Set VBComp2 = VBProj.VBComponents("Module2")
With CodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, "Sub Killme()"
LineNum = LineNum + 1
.InsertLines LineNum, " Kill " & DQUOTE & "Deskptop Directory" & DQUOTE & "\Test 1.xlsm"
LineNum = LineNum + 1
.InsertLines LineNum, "Msgbox" & DQUOTE & "Done!" & DQUOTE
LineNum = LineNum + 1
.InsertLines LineNum, "End Sub"
End With
Workbooks(X).Activate
Application.Run (X & "!Test")
End Sub