G
Guest
Hello,
I'm writing a macro that accomplishes 3 things.
1) Checks for the existence of a file abc.xls
2) If abc.xls exists, save it in the same folder with the name of def.xls
(overwriting existing copies)
3) Save the current workbook (the one running the macro) as abc.xls
I've done this kind of thing before, but the wrinkle here is that all the
files have passwords to modify (not to open). When I try to save any of the
files, I get an error that says "Operation failed. abc.xls is write-reserved."
Below is the relevant code (watch for line wrap)
Public Sub Do_Export()
' Constants to hold vital information
Const PWD As String = "pizza"
Const SAVE_PATH As String = "\\my\network\path\
Const SAVE_NAME As String = "Loan File.xls"
Const B_NAME As String = "Loan File Old.xls"
Dim objWB As Workbook
Dim objFSO As Object
'
***********************************************************************************
' This sub performs all the tasks needed to export the file to the
appropriate place.
'
**********************************************************************************
' Create a reference to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Ensure that the path exists
If objFSO.FolderExists(SAVE_PATH) Then
' Check to see if there is a copy of the file already on the drive.
If so, save it on the
' drive with the backup name, overwriting what may be there already.
If objFSO.FileExists(SAVE_PATH & SAVE_NAME) Then
Set objWB = Application.Workbooks.Open(SAVE_PATH & SAVE_NAME, ,
, , , PWD)
' Suppress Excel warnings for overwriting
Application.DisplayAlerts = False
objWB.SaveAs SAVE_PATH & B_NAME, , , PWD ' ERROR ON THIS LINE
Application.DisplayAlerts = True
objWB.Close
Set objWB = Nothing
End If
' Create a reference to the current workbook
Set objWB = ThisWorkbook
' Hide worksheets
shtUtility.Visible = xlSheetHidden
shtEntry.Visible = xlSheetHidden
Application.DisplayAlerts = False
objWB.SaveAs SAVE_PATH & SAVE_NAME, , , PWD ' ERROR ON THIS LINE
Application.DisplayAlerts = True
objWB.Close
Set objWB = Nothing
Else ' Save path does not exist
MsgBox "Save path " & SAVE_PATH & " does not exist. Ensure that your
network connection exists and is working properly.", vbExclamation, "No Save
Path"
End If
Set objFSO = Nothing
End Sub
I'm writing a macro that accomplishes 3 things.
1) Checks for the existence of a file abc.xls
2) If abc.xls exists, save it in the same folder with the name of def.xls
(overwriting existing copies)
3) Save the current workbook (the one running the macro) as abc.xls
I've done this kind of thing before, but the wrinkle here is that all the
files have passwords to modify (not to open). When I try to save any of the
files, I get an error that says "Operation failed. abc.xls is write-reserved."
Below is the relevant code (watch for line wrap)
Public Sub Do_Export()
' Constants to hold vital information
Const PWD As String = "pizza"
Const SAVE_PATH As String = "\\my\network\path\
Const SAVE_NAME As String = "Loan File.xls"
Const B_NAME As String = "Loan File Old.xls"
Dim objWB As Workbook
Dim objFSO As Object
'
***********************************************************************************
' This sub performs all the tasks needed to export the file to the
appropriate place.
'
**********************************************************************************
' Create a reference to the file system
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Ensure that the path exists
If objFSO.FolderExists(SAVE_PATH) Then
' Check to see if there is a copy of the file already on the drive.
If so, save it on the
' drive with the backup name, overwriting what may be there already.
If objFSO.FileExists(SAVE_PATH & SAVE_NAME) Then
Set objWB = Application.Workbooks.Open(SAVE_PATH & SAVE_NAME, ,
, , , PWD)
' Suppress Excel warnings for overwriting
Application.DisplayAlerts = False
objWB.SaveAs SAVE_PATH & B_NAME, , , PWD ' ERROR ON THIS LINE
Application.DisplayAlerts = True
objWB.Close
Set objWB = Nothing
End If
' Create a reference to the current workbook
Set objWB = ThisWorkbook
' Hide worksheets
shtUtility.Visible = xlSheetHidden
shtEntry.Visible = xlSheetHidden
Application.DisplayAlerts = False
objWB.SaveAs SAVE_PATH & SAVE_NAME, , , PWD ' ERROR ON THIS LINE
Application.DisplayAlerts = True
objWB.Close
Set objWB = Nothing
Else ' Save path does not exist
MsgBox "Save path " & SAVE_PATH & " does not exist. Ensure that your
network connection exists and is working properly.", vbExclamation, "No Save
Path"
End If
Set objFSO = Nothing
End Sub