只读打开 excel 文件并用 VBA 保存但根本没有保存?
Read only opened excel file saved with VBA but is not saved at all?
我有一个 excel 模板,建议在向用户打开时以只读方式打开(我有打开文件的密码以便能够 change/edit 文件)。
我有一个 vba 脚本,用于保存只读打开的文件。
当我以正常方式保存文件时(菜单 > 另存为 > 选择文件 > 保存)excel returns 一条消息告诉我文件无法保存,因为它是以只读方式打开的.
当我使用脚本时,一个弹出窗口显示了我想要的目录和正确的文件名,并且可以保存(所以以只读方式打开,它仍然可以用相同的文件名保存)。完全没有错误。
但是当我现在查看目录时,我更新和保存的大部分文件都没有更新 'last edited date/time'。当我打开文件时,我所做的所有更改都不再存在于文件中。
会不会VBA说保存了相同文件名的文件,虽然文件以只读方式打开,但实际上并没有保存任何东西?
这里是另存为代码:
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Windows(mytemplate).Activate
On Error GoTo Err_
MsgBox ("Bezig met vernieuwen")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:\Commercie\Marktdata\IRi\Segment ontwikkeling\")
MsgBox workbook_Name
If workbook_Name <> False Then
ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
End If
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
它似乎可以工作,但正如我发现的那样,当文件以只读方式打开时它不起作用,尽管我看起来文件已保存并且可以在以只读方式打开时覆盖原始文件-只有.
更新 2019-04-11 10:00上午:
根据 ZACK 的建议,我将另存为代码调整为:
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Windows(mytemplate).Activate
On Error GoTo Err_
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
If ActiveWorkbook.ReadOnly = True Then ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:\Commercie\Marktdata\IRi\Segment ontwikkeling\")
If workbook_Name <> False Then ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
打开只读文件并在覆盖原始只读文件的同时保存它。除此之外,我知道必须再次输入文件名。而如果之前使用原始文件名,则另存为弹出屏幕中不再显示文件名。
更新 2 2019-04-11 10:31 上午:
我有一些额外的 "save as code" 用于获取我之前使用的正确文件名(在原始 post 中)。我将此代码添加到我的主宏并测试它是否有效。现在文件访问已正确更改,另存为弹出窗口在正确的目录中显示正确的文件名并且文件已保存。
结论:案例结束,问题已回答!
这是它的工作原理(对我来说):
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Dim workbookdirectory As String
Dim activewb As String
Windows(mytemplate).Activate
On Error GoTo Err_
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
If ActiveWorkbook.ReadOnly = True Then ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
activewb = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
workbookdirectory = "M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:=workbookdirectory & activewb)
If workbook_Name <> False Then ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
您可以尝试的另一种解决方案是 Zac 所说的。它将更改活动工作簿的访问权限,因此您可以 运行 您的代码,然后将其更改回只读。
Sub saveas()
Dim workbook_Name As Variant
Dim location As String
Dim workbookdirectory As String
Dim activewb As String
ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
activewb = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
workbookdirectory = "M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:=workbookdirectory & activewb)
If workbook_Name = False Then ActiveWorkbook.saveas Filename:=activewb, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
End Sub
我有一个 excel 模板,建议在向用户打开时以只读方式打开(我有打开文件的密码以便能够 change/edit 文件)。 我有一个 vba 脚本,用于保存只读打开的文件。
当我以正常方式保存文件时(菜单 > 另存为 > 选择文件 > 保存)excel returns 一条消息告诉我文件无法保存,因为它是以只读方式打开的.
当我使用脚本时,一个弹出窗口显示了我想要的目录和正确的文件名,并且可以保存(所以以只读方式打开,它仍然可以用相同的文件名保存)。完全没有错误。
但是当我现在查看目录时,我更新和保存的大部分文件都没有更新 'last edited date/time'。当我打开文件时,我所做的所有更改都不再存在于文件中。
会不会VBA说保存了相同文件名的文件,虽然文件以只读方式打开,但实际上并没有保存任何东西?
这里是另存为代码:
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Windows(mytemplate).Activate
On Error GoTo Err_
MsgBox ("Bezig met vernieuwen")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:\Commercie\Marktdata\IRi\Segment ontwikkeling\")
MsgBox workbook_Name
If workbook_Name <> False Then
ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
End If
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
它似乎可以工作,但正如我发现的那样,当文件以只读方式打开时它不起作用,尽管我看起来文件已保存并且可以在以只读方式打开时覆盖原始文件-只有.
更新 2019-04-11 10:00上午:
根据 ZACK 的建议,我将另存为代码调整为:
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Windows(mytemplate).Activate
On Error GoTo Err_
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
If ActiveWorkbook.ReadOnly = True Then ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:\Commercie\Marktdata\IRi\Segment ontwikkeling\")
If workbook_Name <> False Then ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
打开只读文件并在覆盖原始只读文件的同时保存它。除此之外,我知道必须再次输入文件名。而如果之前使用原始文件名,则另存为弹出屏幕中不再显示文件名。
更新 2 2019-04-11 10:31 上午:
我有一些额外的 "save as code" 用于获取我之前使用的正确文件名(在原始 post 中)。我将此代码添加到我的主宏并测试它是否有效。现在文件访问已正确更改,另存为弹出窗口在正确的目录中显示正确的文件名并且文件已保存。
结论:案例结束,问题已回答!
这是它的工作原理(对我来说):
Sub vernieuwalles(mytemplate As String)
Dim workbook_Name As Variant
Dim workbookdirectory As String
Dim activewb As String
Windows(mytemplate).Activate
On Error GoTo Err_
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
If ActiveWorkbook.ReadOnly = True Then ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
activewb = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
workbookdirectory = "M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:=workbookdirectory & activewb)
If workbook_Name <> False Then ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
您可以尝试的另一种解决方案是 Zac 所说的。它将更改活动工作簿的访问权限,因此您可以 运行 您的代码,然后将其更改回只读。
Sub saveas()
Dim workbook_Name As Variant
Dim location As String
Dim workbookdirectory As String
Dim activewb As String
ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
activewb = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5)
workbookdirectory = "M:\Commercie\Marktdata\IRi\Segment Ontwikkeling\"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:=workbookdirectory & activewb)
If workbook_Name = False Then ActiveWorkbook.saveas Filename:=activewb, FileFormat:=50
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
End Sub