VBS Script to Save Open Excel Workbook

rcolving

Board Regular
Joined
May 23, 2002
Messages
124
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
I have a customer that has an Excel Workbook that they never close. His people do not click save. So if the PC happens to update and reboot they lose all data input. I am trying to create a VBS script that I can schedule with task manager to save the workbook at days end. I have this script but get errors: Line 6, Object required: 'objExcel.ActiveWorkbook' Code: 800A01A8. Can anyone help with this?

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks

objExcel.Application.Visible = True

objExcel.ActiveWorkbook.Save ("C:\user\randy\documents\VBS Save Test.xlsx")

objExcel.Application.Quit
WScript.Quit
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:
Code:
Option Explicit

Const xlOpenXMLWorkbook = 51

Dim Excel

Set Excel = Nothing
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
On Error GoTo 0
If Not Excel Is Nothing Then
    If Not Excel.ActiveWorkbook Is Nothing Then
        If Excel.ActiveWorkbook.Path = "" Then
            'Workbook never saved before
            Excel.DisplayAlerts = False 'in case file already exists
            Excel.ActiveWorkbook.SaveAs "C:\user\randy\documents\VBS Save Test.xlsx", xlOpenXMLWorkbook
            Excel.DisplayAlerts = True
        Else
            'Save existing file
            Excel.ActiveWorkbook.Save
        End If
        Excel.ActiveWorkbook.Close False
    End If
    Excel.Quit
    Set Excel = Nothing
    WScript.Sleep 2000  'allow time for Excel.exe process to terminate
End If
 
Upvote 0
Try this:
Code:
Option Explicit

Const xlOpenXMLWorkbook = 51

Dim Excel

Set Excel = Nothing
On Error Resume Next
Set Excel = GetObject(, "Excel.Application")
On Error GoTo 0
If Not Excel Is Nothing Then
    If Not Excel.ActiveWorkbook Is Nothing Then
        If Excel.ActiveWorkbook.Path = "" Then
            'Workbook never saved before
            Excel.DisplayAlerts = False 'in case file already exists
            Excel.ActiveWorkbook.SaveAs "C:\user\randy\documents\VBS Save Test.xlsx", xlOpenXMLWorkbook
            Excel.DisplayAlerts = True
        Else
            'Save existing file
            Excel.ActiveWorkbook.Save
        End If
        Excel.ActiveWorkbook.Close False
    End If
    Excel.Quit
    Set Excel = Nothing
    WScript.Sleep 2000  'allow time for Excel.exe process to terminate
End If
I tried this and got the following error.
Script: C:\Users\Randy\Docuents\VBSSaveFile.vbs
Line: 12
Char: 5
Error: Call was rejected by callee.
Code: 80010001
Source: (null)
 
Upvote 0
Which is line 12 then?

Make sure this path is correct (it's usually Users, not User):
VBA Code:
Excel.ActiveWorkbook.SaveAs "C:\user\randy\documents\VBS Save Test.xlsx", xlOpenXMLWorkbook
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,633
Latest member
DougMo

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