2015-01-13 49 views
0

我正在嘗試將工作表另存爲新的工作簿,但這樣做時初始工作簿的自定義屬性未被繼承。工作表另存爲不繼承工作簿自定義屬性

' 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 

我已經進行了大量搜索,找不到解決方案。

謝謝,

回答

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