vba 设置 Excel 电子表格的“标题”属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26067139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the "Title" property of an excel spreadsheet
提问by Pete
First of all, I'm a total Excel interop noob.
首先,我是一个完全的 Excel 互操作小白。
I'm trying to get a date from a cell and then set the title of the document before the document gets saved, to be the month of the date. This is my code:
我试图从单元格中获取日期,然后在保存文档之前将文档的标题设置为日期的月份。这是我的代码:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Title = DateTime.Month(ThisWorkbook.Sheets("Sheet1").Cell("A10"))
End Sub
I'm not sure that anything is working. I set a breakpoint on the code, but I can't "run" it because it's not a macro, but an event handler, so I don't think the breakpoint is going to work. I don't get any errors. I don't even know that ThisWorkbook.Title
is what I want and I'm not even sure about getting the month from the cell.
我不确定是否有任何工作。我在代码上设置了一个断点,但我不能“运行”它,因为它不是一个宏,而是一个事件处理程序,所以我认为断点不会起作用。我没有收到任何错误。我什至不知道那ThisWorkbook.Title
是我想要的,我什至不确定从牢房里得到月份。
采纳答案by SeanC
The title of the document is a "Built In" property - this is the info that appears when you right click on the file and look at the properties.
文档的标题是“内置”属性 - 这是当您右键单击文件并查看属性时出现的信息。
The name of the spreadsheet is set on save, so you will want to save the file with a new name if you want to see the date on the file itself
电子表格的名称在保存时设置,因此如果您想查看文件本身的日期,则需要使用新名称保存文件
A code something like this should give you the result you desire:
(note that this code is VBA, so it may need some tweaking to work in interop.
像这样的代码应该会给你你想要的结果:(
注意这个代码是 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
回答by Kemp Kennedy
To change the 'Title' built in property in Excel:
要更改 Excel 中的“标题”内置属性:
ActiveWorkbook.BuiltinDocumentProperties("Title") = "My Title Name"
ActiveWorkbook.BuiltinDocumentProperties("Title") = "My Title Name"
回答by ZAT
I assume this would serve your purpose:
我认为这将有助于您的目的:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
End Sub
Another thing is:
另一件事是:
This title would not be there next time you open the workbook. So:
下次打开工作簿时,该标题将不存在。所以:
Private Sub Workbook_Open()
Application.Caption = MonthName(Month(ThisWorkbook.Sheets("Sheet1").Range("C10").Value))
ThisWorkbook.Save
End Sub