我的Excel插件需要安裝Excel的Visual Basic for Applications選項才能使其工作。我希望我的安裝(用InnoSetup編寫)能夠檢測是否安裝了VBA,並警告用戶,如果不是。如何知道Office的VBA組件是否已安裝?
如何檢測該選件是否已安裝?
alt text http://img35.imageshack.us/img35/9333/officeqm.png
我的Excel插件需要安裝Excel的Visual Basic for Applications選項才能使其工作。我希望我的安裝(用InnoSetup編寫)能夠檢測是否安裝了VBA,並警告用戶,如果不是。如何知道Office的VBA組件是否已安裝?
如何檢測該選件是否已安裝?
alt text http://img35.imageshack.us/img35/9333/officeqm.png
一種可能性是檢查VBE6.DLL在C存在:\ Program Files文件\ Common Files文件\ Microsoft共享\ VBA \ VBA6。或者在註冊表中尋找對該DLL或字符串VBA的引用。
請注意,對於Office 2010,此位置/文件名可能會有所不同,因爲VBA編輯器中有一些更改。
你爲什麼不嘗試這樣的功能... found here
Option Explicit
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Sub cmdCheck_Click()
MsgBox "Exist ??? =" & CheckForComponent("user32.dll")
End Sub
Private Function CheckForComponent(ComPath As String) As Boolean
Dim Ret As Long
Ret = LoadLibrary(ComPath)
FreeLibrary Ret
If Ret = 0 Then
CheckForComponent = False
Else
CheckForComponent = True
End If
End Function
public static class VbePrerequisiteDetector {
private const string VbeInstallationPathKey = @"SOFTWARE\Microsoft\VBA";
private const string Vbe6InstallationPathValue = "Vbe6DllPath";
private const string Vbe7InstallationPathValue = "Vbe7DllPath";
/// <summary>
/// Return true if VBE6 installed. VBE6 is prerequisite for for Office2003 and Office2007
/// </summary>
/// <returns>Return true if VBE6 installed.</returns>
public static bool IsVbe6Installed() {
try {
RegistryKey vbaPathKey = Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);
if (vbaPathKey != null) {
if (vbaPathKey.GetValue(Vbe6InstallationPathValue) != null) {
string pathToVbe = (string)vbaPathKey.GetValue(Vbe6InstallationPathValue);
if (File.Exists(pathToVbe)) {
return true;
}
}
}
}
catch (Exception) {
//Ignore all exceptions
}
return false;
}
/// <summary>
/// Return true if VBE7 installed. VBE7 is prerequisite for for Office2010
/// </summary>
/// <returns>Return true if VBE7 installed.</returns>
public static bool IsVbe7Installed() {
try {
RegistryKey vbaPathKey = Registry.LocalMachine.OpenSubKey(VbeInstallationPathKey);
if (vbaPathKey != null) {
if (vbaPathKey.GetValue(Vbe7InstallationPathValue) != null) {
string pathToVbe = (string)vbaPathKey.GetValue(Vbe7InstallationPathValue);
if (File.Exists(pathToVbe)) {
return true;
}
}
}
}
catch (Exception) {
//Ignore all exceptions
}
return false;
}
}
這個標籤是VBA和Office。您確定要發佈與標籤無關的答案嗎?沒有任何解釋? – Fionnuala 2011-01-14 12:54:13
我們正在談論的Windows Installer組件。 安裝程序有一個API,您可以在其中安裝功能/組件時請求安裝。 的api也返回組件安裝的位置。 如果nessacary你可以安裝缺少的組件。
您需要的唯一東西就是組件和產品guid。
,以檢測是否安裝VBA的最佳方式是使用MsiQueryFeatureState API,並要求Windows安裝程序的功能是否安裝與否。下面是一些在VB.NET中執行此操作的示例代碼,但是您可以使用允許調用COM組件的任何語言對其進行編碼(對不起,不熟悉InnoSetup)。
Private Declare Function MsiQueryFeatureState Lib "Msi" Alias "MsiQueryFeatureStateA" (ByVal Product As String, ByVal Feature As String) As Long
Public Function FVbaAvailable() As Boolean
Dim objExcelApp As Object
Dim strProductCode As String
Dim nState As Long
Dim fAvailable As Boolean = False
Try
' Start an Excel instance and get the product code.
objExcelApp = CreateObject("Excel.Application")
strProductCode = DirectCast(objExcelApp.ProductCode, String)
' Get FeatureState for the VBAFiles Feature.
nState = MsiQueryFeatureState(strProductCode, "VBAFiles")
If (nState = 1) OrElse (nState = 3) OrElse (nState = 4) Then
' VBA is available.
fAvailable = True
End If
' Clean up.
objExcelApp.Quit()
Runtime.InteropServices.Marshal.FinalReleaseComObject(objExcelApp)
objExcelApp = Nothing
Catch ex As Exception
Trace.WriteLine(ex.Message)
End Try
Return fAvailable
End Function
不幸的是,這對Office 365不起作用。 – 2017-04-24 05:41:22