|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub text()
- Dim wb As Workbook
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set wb = Workbooks.Open(ThisWorkbook.Path & "" & "book2.xlsx")
- With wb.VBProject.VBComponents("ThisWorkbook")
- .CodeModule.DeleteLines 1, .CodeModule.CountOfLines '删除原代码
- .CodeModule.InsertLines 1, "Private Sub workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)"
- .CodeModule.InsertLines 2, " Dim s As Long"
- .CodeModule.InsertLines 3, " If SaveAsUI = True Then"
- .CodeModule.InsertLines 4, " s = MsgBox(""该工作簿不允许用“另存为”来保存,"" & ""你要用原工作簿名称来保存吗? "", vbQuestion + vbOKCancel)"
- .CodeModule.InsertLines 5, " Cancel = (response = vbCancel)"
- .CodeModule.InsertLines 6, " If Cancel = False Then Me.Save"
- .CodeModule.InsertLines 7, " Cancel = True"
- .CodeModule.InsertLines 8, " End If"
- .CodeModule.InsertLines 9, "End Sub"
- End With
- wb.SaveAs Filename:=ThisWorkbook.Path & "\book2", FileFormat:=52
- wb.Close True
- Kill ThisWorkbook.Path & "" & "book2.xlsx"
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- End Sub
复制代码 |
|