设置"标题" excel电子表格的属性

时间:2014-09-26 19:40:48

标签: excel excel-vba vba

首先,我是一个完整的Excel互操作菜单。

我正在尝试从单元格中获取日期,然后在文档保存之前设置文档的标题,即日期的月份。这是我的代码:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   ThisWorkbook.Title = DateTime.Month(ThisWorkbook.Sheets("Sheet1").Cell("A10"))
End Sub

我不确定是否有效。我在代码上设置了断点,但是我不能“运行”它,因为它不是宏,而是事件处理程序,所以我不认为断点会起作用。我没有得到任何错误。我甚至都不知道ThisWorkbook.Title是我想要的,而且我甚至不确定从单元格中获取月份。

3 个答案:

答案 0 :(得分:3)

更改标题'在Excel中内置属性:

ActiveWorkbook.BuiltinDocumentProperties(" Title")="我的标题名称"

答案 1 :(得分:0)

该文件的标题是"内置" property - 当您右键单击文件并查看属性时显示的信息 Propery page of excel sheet

电子表格的名称是在保存时设置的,因此如果要在文件本身上查看日期,则需要使用新名称保存文件

这样的代码可以给你想要的结果:
(请注意,此代码是VBA,因此可能需要进行一些调整以在互操作中工作。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim FilePath As String
Dim varName As String

On Error GoTo ErrorHandler

' This disables all Excel events.
Application.EnableEvents = False

' disable the default behaviour of the save like so:
Cancel = True

'you can leave this blank if you want it to save in the default directory
FilePath = "C:\The path\To\The File"

varName = Format(ThisWorkbook.Sheets("Sheet1").Cell("A10"),"mmmm")

ActiveWorkbook.SaveAs Filename:=FilePath & varName & ".xlsx"

ErrorExit:
' This makes sure events get turned back on again no matter what.
Application.EnableEvents = True
Exit Sub

ErrorHandler:
MsgBox "No value submitted - File Not Saved"
Resume ErrorExit
End Sub

答案 2 :(得分:0)

我认为这符合您的目的:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
End Sub

另一件事是:

下次打开工作簿时,此标题不会出现。所以:

Private Sub Workbook_Open()
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
ThisWorkbook.Save
End Sub