vba script to save workbook overwrites entered filename

人走茶凉 提交于 2019-12-13 06:21:54

问题


With the below entered script I want my workbook to be saved with the name entered by the user, say

Sub save_workbook_name()
Dim workbook_Name As Variant
Dim location As String

workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="N:\IRi\Periode Rapportage Px")

If workbook_Name <> False Then

    ActiveWorkbook.SaveAs WriteResPassword:="TM", FileFormat:=50

End If
End Sub

But when I now try to save the file with a different name, lets say with the X replaced by 9, it turns out the script only allows to save with the same file name as the original filename. So I entered Periode Rapportage P9(.xlsb) and then Excel saves the file with filename Periode Rapportage Zelfzorg v2.xlsb.

Any clues on why this happens? The original file is saved with a password to write security.

Entered desired filename:

popup question after pressing ok/save:

Apparently the entered desired filename was changed back to its original filename.

Is there any way to solve this and make the script save the file with the name entered by the user?


回答1:


You didn't include a file name in the saveas line:

ActiveWorkbook.SaveAs filename:=workbook_name, WriteResPassword:="TM", FileFormat:=50


来源:https://stackoverflow.com/questions/52258113/vba-script-to-save-workbook-overwrites-entered-filename

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!