Using VB6 how can I check if Word is installed?

Posted on 2004-11-04
Last Modified: 2009-07-29
How can I check if word is installed before I try to create a word object in VB?
Question by:Sparky191
    LVL 26

    Assisted Solution

    Several way you can do.
    1. check registry
    2. check progid


    Option Explicit

    Private Declare Function CLSIDFromProgID Lib "ole32.dll" (ByVal _
    lpszProgID As Long, pCLSID As Any) As Long
    Private Declare Function StringFromCLSID Lib "ole32.dll" (pCLSID As _
    Any, lpszProgID As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias _
    "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
    ' Convert a ProgID (such as "Word.Application") into the
    ' string representation of its CLSID
    Function ProgIdToCLSID(ByVal ProgID As String) As String
    Dim pResult As Long, pChar As Long
    Dim char As Integer, length As Long
    ' No need to use a special UDT
    Dim guid(15) As Byte

    ' get the CLSID in binary form
    CLSIDFromProgID StrPtr(ProgID), guid(0)
    ' convert to a string, get pointer to result
    StringFromCLSID guid(0), pResult
    ' find the terminating null char
    pChar = pResult - 2
    pChar = pChar + 2
    CopyMemory char, ByVal pChar, 2
    Loop While char
    ' now get the entire string in one operation
    length = pChar - pResult
    ' no need for a temporary string
    ProgIdToCLSID = Space$(length \ 2)
    CopyMemory ByVal StrPtr(ProgIdToCLSID), ByVal pResult, length
    ' release the memory allocated to the string
    CoTaskMemFree pResult
    End Function

    Private Sub Command1_Click()

    ' return {00000000-0000-0000-0000-000000000000} if not found
    Debug.Print ProgIdToCLSID("word.application")
    End Sub
    LVL 48

    Assisted Solution

    by:Ryan Chong
    or try like:

    Public Function isWordInstalled() As Boolean
        On Error GoTo EH
        Dim objApp As Object
        Set objApp = CreateObject("Word.Application")
        Set objApp = Nothing
        isWordInstalled = True
        Exit Function
        isWordInstalled = False
    End Function


    msgbox "Is Word installed? > " & isWordInstalled

    LVL 32

    Accepted Solution

    *Assume* it is installed, and use error trapping to control program flow

    On Error Resume Next

    oWord = CreateObject("Word.Application")

    If Err.Number <> 0 Then
        If Err.Number = 429 Then
            'Cannot create object
            MsgBox "Word is not available"
        End if
        Unload Me
        'Use Word here
    End If
    LVL 1

    Author Comment

    The trapping looks like being the best way. I'll try it tomorrow and let you know how I get one.
    LVL 4

    Expert Comment

    did you try by checking if the application exists (like WINWORD.EXE) you can run a scan in the most common places that word would be installed for example "c:\program files\microsoft office\office10" and other places that users would install word to...obviously if the exe exists then word is installed.


    Dim installed As Boolean
    Dim exists As String
    exists = Dir("c:\program files\microsoft office\office10\WINWORD.EXE", 0)
    If exists = "WINWORD.EXE" Then installed = True Else installed = False
    If installed = True Then MsgBox "word is installed" Else MsgBox "word is not installed"

    if the file exists it will return "WINWORD.EXE" and if it dosnt exits it will return nothing
    LVL 32

    Expert Comment

    Be aware that CreateObject("Word.Application") will succeed when Word is available over the network (I believe), so it may no be a good test for local installation.
    LVL 1

    Expert Comment

     vat about this

    if len(dir("C:\Program Files\Microsoft Office\Office\WINWORD.EXE"))>0 then
    end if

    LVL 1

    Author Comment

    I think searching for the exe is bad practise myself. Catching via the error handler is a more concise and robust method in my opinion.
    LVL 1

    Author Comment

    Erick37 - can you explain how you can make word availble over the network and how you can use VBA with a copy installed on the network and no locally.
    LVL 32

    Assisted Solution

    In order to explicitly create an instance of Word located remotely, the servername must be specified.

    Set oWord = CreateObject("Word.Application","\\TheServerName")
    oWord.Visible = True
    LVL 1

    Author Comment

    That seems to suggest that if you don't use the servername switch that the object creation will fail?

    Didn't know you could run it from a server. Thats interesting.
    LVL 32

    Assisted Solution

    From MSDN on the servername parameter:

    "The name of the network server where the object will be created. If servername is an empty string (""), the local machine is used."

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now