更新:2007 年 11 月
您需要下列元件才能完成此逐步解說:
在這個步驟中,會建立一個 [Excel 活頁簿] 專案。如果您想要使用現有的 Word 或 Excel 方案來執行此逐步解說,請從標題為「建立安裝專案」的逐步解說開始,並以您的專案名稱取代所有程式碼範例和指示中的專案名稱 ExcelDeployment。
若要建立新的專案
Visual Studio 會在設計工具中開啟新的 Excel 活頁簿,並將 ExcelDeployment 專案加入至 [方案總管]。
專案需要一些程式碼,您才能在開啟文件時,驗證方案是否正常運作。在本逐步解說中,請將訊息方塊加入至活頁簿的 Startup 事件處理常式。
若要將訊息方塊加入至初始化事件
在 [方案總管] 中的 [ThisWorkbook.vb] 或 [ThisWorkbook.cs] 上按一下滑鼠右鍵,然後按一下快速鍵功能表上的 [檢視程式碼]。
將下列程式碼加入至 ThisWorkbook 類別內的 Startup 事件處理常式,以便在初始化期間顯示訊息方塊。
Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Startup
MessageBox.Show("The workbook is deployed successfully.")
End Sub
private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
MessageBox.Show("The workbook is deployed successfully.");
}
按 F5 執行專案。
Excel 隨即啟動且訊息方塊便會出現。
關閉訊息方塊。
結束 Excel。
安裝專案會提供您可編譯的檔案和程式碼,以建立方案的 Windows Installer 檔。如需詳細資訊,請參閱安裝專案。
若要為您的方案建立安裝專案
在 [方案總管] 中的方案節點上按一下滑鼠右鍵。
在快速鍵功能表上指向 [加入],再按一下 [新增專案]。
[加入新的專案] 對話方塊隨即出現。
在 [專案類型] 窗格中,展開 [其他專案類型],然後選取 [安裝和部署]。
在 [範本] 窗格中,選取 [安裝專案]。
將專案命名為 ExcelSetup。
按一下 [確定]。
安裝專案即會出現於 [方案總管] 中。根據預設,您將使用此安裝專案建置的 Windows Installer 檔會包含一個對話方塊,可讓使用者指定方案的安裝位置。如需詳細資訊,請參閱安裝資料夾使用者介面對話方塊。
ExcelDeployment 專案的主要輸出含有活頁簿和方案組件。將這些元件加入至安裝專案。
若要將文件和組件加入至安裝專案中
在 [方案總管] 中,以滑鼠右鍵按一下 ExcelSetup 專案節點。
指向快速鍵功能表上的 [檢視],然後按一下 [檔案系統]。
在左窗格中,以滑鼠右鍵按一下 [應用程式資料夾]。
在快速鍵功能表上指向 [加入],再按一下 [專案輸出]。
在 [專案] 方塊中選取 ExcelDeployment。
在輸出類型的清單中,選取 [主要輸出]。
按一下 [確定]。
專案輸出和相依性就會顯示在右窗格中。
在 [方案總管] 中,展開 ExcelSetup 專案節點底下的 [偵測到的相依性]。
以滑鼠右鍵按一下每個相依性 (Microsoft .NET Framework 除外),然後按一下快速鍵功能表中的 [排除]。
自訂動作是 Windows Installer 的功能。您可以使用這項功能,在安裝的最後階段執行程式碼,以執行無法在安裝過程中執行的動作。如需詳細資訊,請參閱自訂動作。
若要建立自訂動作專案
在 [方案總管] 中的方案節點上按一下滑鼠右鍵。
在快速鍵功能表上指向 [加入],再按一下 [新增專案]。
[加入新的專案] 對話方塊隨即出現。
在 [專案類型] 窗格中,展開程式設計語言的節點,然後選取 [Windows]。
在 [範本] 窗格中選取 [類別庫]。
將專案命名為 ExcelCustomAction。
按一下 [確定]。
新專案即會出現於 [方案總管] 中。
在 [方案總管] 中,以滑鼠右鍵按一下 ExcelCustomAction 專案底下的 [Class1.vb] 或 [Class1.cs],然後按一下 [刪除]。這個檔案並非此逐步解說所需的檔案。
當您先前在此逐步解說中按下 F5 來執行專案時,此建置處理序會將活頁簿中內嵌的應用程式資訊清單編輯成指向組件的相對路徑。如果活頁簿和組件在安裝後仍然保留在相同的資料夾中,您就不需要修改內嵌的應用程式資訊清單,而且可以忽略本節。不過,如果您不想要讓使用者在安裝方案後將活頁簿移至不同的資料夾,則必須將應用程式資訊清單編輯成指向組件的完整路徑。
您必須在安裝程序完成後執行自訂動作,以便更新內嵌於 Visual Studio Tools for Office 方案文件中的應用程式資訊清單,因為方案組件的位置要到使用者在安裝時指定位置才會知道。您可以使用 ServerDocument 類別,編輯內嵌的應用程式資訊清單。若要在安裝專案中使用 ServerDocument 類別,請將程式碼加入至自訂動作專案內的 Installer 類別。
若要建立可編輯應用程式資訊清單的自訂動作
在 [方案總管] 中的 ExcelCustomAction 專案上按一下滑鼠右鍵。
在快速鍵功能表上指向 [加入],再按一下 [新增項目]。
[加入新項目] 對話方塊隨即出現。
選取 [安裝程式類別],然後將類別命名為 ManifestEditor。
將 Microsoft.VisualStudio.Tools.Applications.Runtime 組件的參考加入至 ExcelCustomAction 專案中。
在 [方案總管] 中,以滑鼠右鍵按一下 ManifestEditor.cs 或 ManifestEditor.vb 檔,然後按一下 [檢視程式碼]。
在程式碼檔的頂端加入下列 Imports 或 using 陳述式。
Imports Microsoft.VisualStudio.Tools.Applications.Runtime
using Microsoft.VisualStudio.Tools.Applications.Runtime;
將下列程式碼複製到 ManifestEditor 類別中。
這段程式碼會覆寫 Install 方法,而此方法是用來執行自訂安裝動作。此程式碼會將使用者指定的安裝位置設定為 AssemblyPath 屬性。使用者指定的安裝路徑以及文件和組件的名稱都是從 Parameters 屬性取得。
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
UpdateApplicationManifest()
MyBase.Install(stateSaver)
End Sub
Private Sub UpdateApplicationManifest()
' Get the parameters passed to the task.
Dim targetDir As String = Me.Context.Parameters("targetdir")
Dim documentName As String = Me.Context.Parameters("documentname")
Dim assemblyName As String = Me.Context.Parameters("assemblyname")
' Get the application manifest from the document.
Dim documentPath As String = System.IO.Path.Combine(targetDir, documentname)
Dim serverDocument1 As ServerDocument = New ServerDocument(documentPath, _
System.IO.FileAccess.ReadWrite)
Try
Dim appManifest1 As AppManifest = serverDocument1.AppManifest
Dim assemblyPath As String = System.IO.Path.Combine(targetDir, assemblyName)
appManifest1.Dependency.AssemblyPath = assemblyPath
serverDocument1.Save()
Finally
If Not serverDocument1 Is Nothing Then
serverDocument1.Close()
End If
End Try
End Sub
// Override the Install method to update the customization location
// in the application manifest.
public override void Install(System.Collections.IDictionary stateSaver)
{
UpdateApplicationManifest();
base.Install(stateSaver);
}
// Update the application manifest according to the the install location.
private void UpdateApplicationManifest()
{
// Get the parameters passed to the task.
string targetDir = this.Context.Parameters["targetdir"];
string documentName = this.Context.Parameters["documentname"];
string assemblyName = this.Context.Parameters["assemblyname"];
// Get the application manifest from the document.
string documentPath = System.IO.Path.Combine(targetDir, documentName);
ServerDocument serverDocument1 = new ServerDocument(documentPath,
System.IO.FileAccess.ReadWrite);
try
{
AppManifest appManifest1 = serverDocument1.AppManifest;
string assemblyPath = System.IO.Path.Combine(targetDir, assemblyName);
appManifest1.Dependency.AssemblyPath = assemblyPath;
serverDocument1.Save();
}
finally
{
if (serverDocument1 != null)
{
serverDocument1.Close();
}
}
}
在 [方案總管] 中以滑鼠右鍵按一下 ExcelCustomAction 專案,然後按一下 [建置]。
現在您可以讓 Windows Installer 檔執行編輯應用程式資訊清單的自訂動作。若要執行這項動作,請將 ExcelCustomAction 專案的主要輸出加入至安裝專案中。
若要將自訂動作專案的主要輸出加入至安裝專案中
在 [方案總管] 中,以滑鼠右鍵按一下 ExcelSetup 專案節點。
在快速鍵功能表上指向 [檢視],然後按一下 [自訂動作]。
在 [自訂動作編輯器] 中,以滑鼠右鍵按一下 [安裝] 節點,然後按一下 [加入自訂動作]。
在 [查詢] 方塊中,選取 [應用程式資料夾],然後按一下 [加入輸出]。
在 [專案] 方塊中選取 [ExcelCustomAction]。
在輸出類型的清單中,選取 [主要輸出],然後按一下 [確定]。
確認 [來自 ExcelCustomAction 的主要輸出 (作用中)] 已加入至安裝專案之主要輸出的清單中,然後按一下 [確定]。
在 [自訂動作編輯器] 中,展開 [安裝]。
以滑鼠右鍵按一下 [來自 ExcelCustomAction 的主要輸出 (作用中)],然後按一下 [屬性視窗]。
在 [屬性] 視窗中,將 [CustomActionData] 屬性設定成下列字串。
/targetdir="[TARGETDIR]\" /documentname="ExcelDeployment.xls" /assemblyname="ExcelDeployment.dll"
如需 [CustomActionData] 屬性的詳細資訊,請參閱 CustomActionData 屬性。
在 [方案總管] 中以滑鼠右鍵按一下 ExcelSetup 專案,然後按一下 [建置]。
現在您可以測試專案,以確定在開發電腦上執行 Windows Installer 檔時,系統會安裝方案。
若要測試專案
在 [方案總管] 中以滑鼠右鍵按一下 ExcelSetup 專案,然後按一下 [執行]。
遵循安裝精靈的指示進行,並指定開發電腦上的安裝資料夾。
從安裝資料夾開啟 Excel 活頁簿。
確認訊息方塊是否會出現。
工作
概念
其他資源