在“Workbook_BeforeSave”事件中更改工作簿“FullName”属性
当用户点击“保存”或“另存为”时,我需要将工作簿重命名为单元格的名称。 我的猜测是,我需要在Workbook_BeforeSave
事件中执行此操作。 我有以下问题:
- 如何设置
ActiveWorkbook.FullName
属性,而不用调用Save
,SaveAs
或SaveCopyAs
? - 是什么我试图做甚至可以不禁用工具栏和添加自定义保存button?
我使用下面的代码来创build文件名
Dim fName As String, sName As String fName = ThisWorkbook.FullName sName = ThisWorkbook.Name fName = Left(fName, (Len(fName) - Len(sName))) & _ Thisworkbook.Worksheet("Cust").Range("FILE").Value & ".xls"
谢谢
除了通过保存操作,您不能更改名称或全名属性。 Before_Save事件有一个取消的参数,如果你设置为True将停止保存操作,让你自己做。 这是一个例子。
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim sOldName As String 'If SaveAsUI Then Cancel = True 'cancel the save and do it yourself in code sOldName = Me.FullName 'store the old name so you can delete it later 'Save using the value in the cell Application.EnableEvents = False Me.SaveAs Me.Path & Application.PathSeparator & _ Me.Worksheets("CUST").Range("FILE").Value & ".xls" Application.EnableEvents = True 'If the name changed, delete the old file If Me.FullName <> sOldName Then On Error Resume Next Kill sOldName On Error GoTo 0 End If 'End If End Sub