工作表保存不继承工作簿自定义属性

时间:2015-01-13 09:27:18

标签: vba excel-vba excel

我正在尝试将工作表另存为新工作簿,但这样做不会继承初始工作簿的自定义属性。

' Set the workbook properties
ActiveWorkbook.CustomDocumentProperties.item("customProp1") = variableOne
ActiveWorkbook.CustomDocumentProperties.item("customProp2") = variableTwo

' Save the worksheet as a new workbook
Worksheets("Sheet1").SaveAs FileName:=documentName
ActiveWorkbook.Close SaveChanges:=True

我进行了广泛的搜索,但我无法找到解决方案。

谢谢,

1 个答案:

答案 0 :(得分:0)

我不确定这是否可行,但我确实找到了解决方案。它默默地打开需要自定义属性的工作簿,为其提供自定义属性,保存它然后将其关闭。

Dim app As New Excel.Application
app.Visible = False 'Visible is False by default, so this isn't necessary
Dim book As Excel.Workbook

Set book = app.Workbooks.Add(documentName)

    With book.CustomDocumentProperties
        .Add Name:="property1", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="One"
        .Add Name:="property2", _
            LinkToContent:=False, _
            Type:=msoPropertyTypeString, _
            Value:="two"
    End With

    app.DisplayAlerts = False
    book.Close FileName:=documentName, SaveChanges:=True
    app.Quit
Set app = Nothing
相关问题