我有一个模板,可以将数据复制到其中,然后使用SaveCopyAs
将其保存到其他位置。我想每次开始添加它。但是,所有发生的事情是,它无需密码即可将其保存到新位置。
下面是代码,我之所以使用多余的(不必要的).Open
,是因为我找不到仅使用SaveCopyAs
的方法
Option Explicit
Sub SaveCopyAs_password()
Dim passdate As String
Dim wb, wbresults As Workbook
passdate = "hello"
Workbooks.Open ("R:\Macros\pre\Mealplan.xlsx")
Set wbresults = Workbooks("Mealplan")
wbresults.SaveCopyAs ("R:\Macros\post\Mealplan.xlsx")
Workbooks.Open ("R:\Macros\post\Mealplan.xlsx")
Workbooks("Mealplan.xlsx").Activate
Set wb = ActiveWorkbook
ActiveWorkbook.SaveAs Password:=passdate
wb.Close
End Sub
答案 0 :(得分:1)
此简单代码将为您工作:
Sub SaveCopyAs_password()
Dim passdate As String
Dim wbresults As Workbook
passdate = "hello"
FileCopy "R:\Macros\pre\Mealplan.xlsx", "R:\Macros\post\Mealplan.xlsx"
Set wbresults = Workbooks.Open("R:\Macros\post\Mealplan.xlsx")
wbresults.SaveAs Password:=passdate
wbresults.Close
End Sub
Sub SaveCopyAs_password()
Dim passdate As String
Dim wbresults As Workbook
passdate = "hello"
Set wbresults = Workbooks.Open("R:\Macros\pre\Mealplan.xlsx")
wbresults.SaveAs Filename:="R:\Macros\post\Mealplan.xlsx", Password:=passdate
wbresults.Close
End Sub
从除Macro中使用的2之外的任何其他工作簿中运行此Macro。