2009-09-09 11 views
6

Dodatek My Excel wymaga zainstalowania opcji Visual Basic for Applications programu Excel, aby działała. Chciałbym, aby moja instalacja (która została napisana za pomocą InnoSetup) była w stanie wykryć, czy VBA jest zainstalowany i ostrzegać użytkownika, jeśli tak nie jest.Jak sprawdzić, czy komponent VBA pakietu Office jest zainstalowany?

Jak mogę sprawdzić, czy opcja jest już zainstalowana?

alt text http://img35.imageshack.us/img35/9333/officeqm.png

Odpowiedz

2

Jedną możliwością jest sprawdzenie na obecność vbe6.dll w C: \ Program Files \ Common Files \ Microsoft Shared \ VBA \ VBA6. Lub podsłuchuj w rejestrze, szukając odniesień do tej biblioteki DLL lub do ciągu VBA.

Należy zauważyć, że ta lokalizacja/nazwa pliku może się różnić w Office 2010, ponieważ są pewne zmiany w edytorze VBA.

+0

To nie działa na Office 365, niestety. –

0

Dlaczego nie spróbujesz funkcję takiego ... 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 
0
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; 
    } 
} 
+0

Znacznikami są VBA i Office. Czy na pewno chcesz opublikować odpowiedź, która nie jest związana z tagami bez żadnego wyjaśnienia? – Fionnuala

0

Mówimy o składnikach Instalatora Windows. Instalator ma interfejs API, w którym można uzyskać żądanie, czy zainstalowana jest funkcja/komponent. ofcurse, że api również powróci tam, gdzie jest zainstalowany składnik. jeśli nie możesz zainstalować brakujących składników.

Jedyne czego potrzebujesz, to komponent und product guid.

see documentation

0

Najlepszym sposobem, aby wykryć, czy jest zainstalowany VBA jest użycie API MsiQueryFeatureState i zapytać Instalatora Windows, czy funkcja jest zainstalowana lub nie. Poniżej znajduje się przykładowy kod, który robi to w VB.NET, jednak można go zakodować w dowolnym języku, który pozwala na wywołanie składników COM (przepraszam, nieznajomy z 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 
Powiązane problemy