Run-time error '1004' Method 'SaveAs' of object '_Workbook' failed

Mike UK

New Member
Joined
Dec 14, 2011
Messages
43
Within an excel workbook I have created a routine that automatically logs the user and date when the workbook is initially opened. The Sub Workbook_Open() calls the routine.

For most users it has worked effectively for sometime. However, myself and a few users are now getting the Run-time error '1004' Method 'SaveAs' of object '_Workbook' failed error message. The code is shown below. The log file is an excel (.xlsx) file held on a central Sharepoint site which all users have read and write access to. Any ideas to overcome would be greatly appreciated.

Sub LogDetails()
Dim LR As Long

Application.ScreenUpdating = False


Workbooks.Open Filename:="http://.../Annual%20Statement%20Log/Log%20for%20Annual%20Statement.xlsx"
Set xls = CreateObject("Excel.Application")
xls.Application.DisplayAlerts = False

Workbooks("Log for Annual Statement.xlsx").Activate
Worksheets("Log").Activate
LR = Range("A" & Rows.Count).End(xlUp).Row

Sheets("Log").Cells(LR + 1, 1) = (Environ$("Username"))
Sheets("Log").Cells(LR + 1, 2) = Now()


Workbooks("Log for Annual Statement.xlsx").SaveAs Filename:="http://.../Annual%20Statement%20Log/Log%20for%20Annual%20Statement.xlsx"
Workbooks("Log for Annual Statement.xlsx").Close
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Have you considered using a hidden sheet in the workbook itself for the log ?
- your problem could not happen
The sheet can be hidden so that VBA is required to unhide it to prevent unhelpful user-meddling!
 
Upvote 0
Thanks Yongle, that's a good workaround. However, the workbook copies sheets across from another template workbook which is read only (to avoid damage to the templates and code) so each workbook is unique. That's why I thought of having a separate simple workbook for the log.
 
Last edited:
Upvote 0
I solved the problem using the following code.

Sub LogDetails()
Dim LR As Long

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Workbooks.Open Filename:="http://..../Log%20for%20Annual%20Statement.xlsx"

Workbooks("Log for Annual Statement.xlsx").Activate
If ActiveWorkbook.ReadOnly Then ActiveWorkbook.LockServerFile
Worksheets("Log").Activate
LR = Range("A" & Rows.Count).End(xlUp).Row

Sheets("Log").Cells(LR + 1, 1) = (Environ$("Username"))
Sheets("Log").Cells(LR + 1, 2) = Now()

Workbooks("Log for Annual Statement.xlsx").Save
Workbooks("Log for Annual Statement.xlsx").Close

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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