CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2007
    Posts
    405

    I want to finding code check Office installed ?

    Do I want to finding code or example check Office component installed ? thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: I want to finding code check Office installed ?

    What are you trying to do? If you want to deploy an app that uses Office, you can deploy to a particular version, or else deploy to a general version of that app (you make sure version supports lowest user)

    You bind the object before you compile, or you allow the object to be set when the app is running. Early or Late Binding

    Here's Late Binding:
    Code:
    Option Explicit
    ' These are both examples of Late Binding
    
    Public Sub RunAccessMacro(strDB As String, strMacro As String)
    '================================================================
    'for late binding declare it As Object and use CreateObject() function
      Dim AccessDB As Object
      Set AccessDB = CreateObject("Access.Application")
    
        With AccessDB
            .OpenCurrentDatabase strDB
            .DoCmd.RunMacro strMacro, 1
            '.Visible = True    'you decide
            .CloseCurrentDatabase
        End With
        Set AccessDB = Nothing
    
    End Sub
    
    Public Sub ExcelMacro()
      Dim excl As Object
      Dim wrbk As Object
    
        Set excl = CreateObject("Excel.Application")
        excl.DisplayAlerts = False
        Set wrbk = excl.Workbooks.Open("myfile", , True, , "mypassword")
        excl.Run "MacroName", "arg1", "arg2"
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: I want to finding code check Office installed ?

    So maybe to detect if Excel is installed something like this could do it:
    Code:
    Function ExcelInstalled() as Boolean
      On Error Goto NoExcel
      dim excl as Object
      set excl = CreateObject("Excel.Application")
      ExcelInstalled = True
    NoExcel :
    End Function
    The function returns true if excel is installed.
    Similar functions could be written for other office components.
    It's a little time consuming to create an Excel.Application, thouh. Maybe one can create a smaller excel object instead. If excel is not there, the CreateObject will produce an error which is trapped by the function to return false.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: I want to finding code check Office installed ?

    EDIT : Didn't see your reply WoF+

    Quote Originally Posted by dongtrien View Post
    Do I want to finding code or example check Office component installed ? thanks
    Doa search here on this forum, for office, office installed, office registry - that should give you lots of decent threads about this very same topic.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: I want to finding code check Office installed ?

    Never mind, Hannes.
    Do you think my little trick will work? Have no computer WITHOUT Excel at the moment to verify it.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: I want to finding code check Office installed ?

    No worries

    I quickly uninstalled my MS Office 2003. And did this ( Mainly based on your code ) :
    Code:
    Option Explicit
    Function ExcelInstalled() As Boolean
      On Error GoTo NoExcel
      Dim excl As Object
      Set excl = CreateObject("Excel.Application")
      ExcelInstalled = True
    NoExcel:
    End Function
    
    Private Sub Command1_Click()
    If Not ExcelInstalled Then MsgBox "No Excel On This Machine"
    End Sub
    And it did give me the MessageBox saying that there is No Excel On This Machine

    Good job as usual!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: I want to finding code check Office installed ?

    Fine. Thanks.
    The only drawback is, if there IS Excel on a machine the function takes a couple of seconds to complete, because it then really creates an Excel.Application.
    Maybe there are smaller Excel-only objects which you could try to create. I'm not into VBA for Excel, but I can imagine, we could create a worksheet object or something only Excel has.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: I want to finding code check Office installed ?

    Initially I thought of posting this code, as I've mentioned to the OP that he / she must search for excel registry.

    If we look at the HKEY_CLASSES_ROOT key in the registry, we'll find many hints of applications currently instaled on the system. For example Access.Database would tell us if Access is installed, PowerPoint.Slide tells us PowerPint isnstalled, Word.Document would tell us Word is Installed. Using the same logic, we could just check for Excel.Sheet to check specifically for the presence of Excel. We could actually check for the presence for Excel, Word, orPowerPoint to determine if Office is installed. Access may no always be included in the installation.

    This is what I've done, just add this code to a new project, and add one button :
    Code:
    Option Explicit
    
    Private Declare Function RegOpenKey Lib "advapi32" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, lpReserved As Long, lptype As Long, lpData As Any, lpcbData As Long) As Long
    Private Declare Function RegCloseKey& Lib "advapi32" (ByVal hKey&)
    
    Private Const HKEY_CLASSES_ROOT = &H80000000
    Private Const WM_QUIT As Long = &H12
    Private Const REG_SZ = 1
    Private Const REG_EXPAND_SZ = 2
    Private Const ERROR_SUCCESS = 0
    
    Private Function GetRegString(hKey As Long, strSubKey As String, strValueName As String) As String
        Dim strSetting As String
        Dim lngDataLen As Long
        Dim lngRes As Long
        
        If RegOpenKey(hKey, strSubKey, lngRes) = ERROR_SUCCESS Then
            strSetting = Space(255)
            lngDataLen = Len(strSetting)
            
            If RegQueryValueEx(lngRes, strValueName, ByVal 0, REG_EXPAND_SZ, ByVal strSetting, lngDataLen) = ERROR_SUCCESS Then
                
                If lngDataLen > 1 Then
                    GetRegString = Left(strSetting, lngDataLen - 1)
                End If
            End If
    
            If RegCloseKey(lngRes) <> ERROR_SUCCESS Then
                MsgBox "RegCloseKey Failed: " & strSubKey, vbCritical
            End If
        End If
    End Function
    
    Private Function IsAppPresent(strSubKey$, strValueName$) As Boolean
        IsAppPresent = CBool(Len(GetRegString(HKEY_CLASSES_ROOT, strSubKey, strValueName)))
    End Function
    
    Private Sub Command1_Click()
              If IsAppPresent("Excel.Sheet\CurVer", "") = False Then
                    MsgBox "Please Install MS Office", vbCritical, "Exam Finder Error"
              Else
                    MsgBox "Excel Is Installed!"
              End If
    End Sub
    This works very fast, and there is not additional overhead of creating an Excel object, if Excel is already present.

  9. #9
    Join Date
    Sep 2007
    Posts
    405

    Re: I want to finding code check Office installed ?

    The code of friends are very good, Thank for wrote to me, thank you very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured