Remove Book1 After Script SaveAs

edrichard

New Member
Joined
Mar 31, 2009
Messages
7
Hi,

I have a script that opens Excel, inserts some data in specific cells and then saves the spreadsheet as the date. Everything works fine, but when I am shutting down my PC at the end of the day, there is a lingering Book1 file open on my PC, since I saved the file as something different. Is there a way to close this file as well within the script. I inserted 2 objExcel.Quit statements at the end of the script and that did not work. Any ideas?

Code snippet:
Code:
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Add
 
' ! Prompt for the Project Number
' !
 
Wscript.Echo " "
Wscript.StdOut.Write " Project Number: "
ProjNumber = Wscript.StdIn.ReadLine
objExcel.Cells(8, 1).Value = "Project Number:"
objExcel.Cells(8, 2).Value = ProjNumber
 
objExcel.DisplayAlerts = False
dtmDate = Date
strMonth = Month(Date)
strDay = Day(Date)
strYear = Right(Year(Date),2)
 
strFileName = "C:\temp\" & "Project_" & ProjNumber & "_" & strMonth & "-" & strDay & "-" & strYear & ".xls"
objWorkbook.SaveAs(strFileName)
 
objExcel.Quit
objExcel.Quit
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi
Try
Code:
workbooks("Book.xls").close false
false does not save changes. replace with true if you want changes saved.
 
Upvote 0
Hi,

When I tried this, I received the error - Microsoft VBScript runtime error: Type mismatch: 'Workbooks'

Then I tried objWorkbook.Workbooks("Book.xls").close false and received - (null): The object invoked has disconnected from its clients

I'm sure I am just keying something incorrectly. What do you think? Thanks.
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top