关闭

如何检测当前机器中是否安装Office 及其版本??

标签: officefunctionstringmicrosoftperformancepath
2855人阅读 评论(1) 收藏 举报

'作者:CSDN 许仙
'Homepage : jjweb.126.com
'MSN :Coderxu#hotmail.com
'QQ:19030300
'转载请保持文章完整,保存以上作者信息 请珍惜他人劳动成果

Option Explicit

Const REG_EXPAND_SZ = 2
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004
Const ERROR_SUCCESS = 0&

Const RegLocation = "software/UnpreXisten/Online Code Browser/"
Const RegKey = HKEY_LOCAL_MACHINE
Dim AppVer As String * 8

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal Hkey As Long) As Long

Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String) As Long

Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal Hkey As Long, ByVal lpValueName As String) As Long

Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal Hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal Hkey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Const REG_SZ = 1
Const REG_DWORD = 4

Public Enum OfficeVer
    Office_97
    Office_2000
    Office_xp
    Office_2003
End Enum
'<CSCM>
'--------------------------------------------------------------------------------
' 工 程 名:  工程1
' 函 数 名: GetString
' 变    量 :  OfKind (OfficeVer)
' 输    入:无
' 输    出:OFFICE 的路径
'我的机器没有OFFICE xp 根据97 200 2003的关系
'可以看出键值为"HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Office/10.0/Common/InstallRoot"
' 日    期 : 2005-7-5
' 作    者 :    许仙
'--------------------------------------------------------------------------------
'</CSCM>
Public Function GetOfficePath(OfKind As OfficeVer) As String

    Dim lValueType
    Dim keyhand As Long, r
    Dim datatype As Long
    Dim lResult As Long
    Dim strBuf As String
    Dim lDataBufSize As Long
    Dim intZeroPos As Integer, StrKeyName$

    Select Case OfKind

        Case 0
            r = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Office/8.0/Common/InstallRoot", keyhand)
            StrKeyName = "OfficeBin"

        Case 1
            r = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Office/9.0/Common/InstallRoot", keyhand)
            StrKeyName = "Path"

        Case 2
            r = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Office/10.0/Common/InstallRoot", keyhand)
            StrKeyName = "Path"

        Case 3
            r = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE/Microsoft/Office/11.0/Common/InstallRoot", keyhand)
            StrKeyName = "Path"

    End Select

    lResult = RegQueryValueEx(keyhand, StrKeyName, 0&, lValueType, ByVal 0&, lDataBufSize)

    If lValueType = REG_SZ Then

        strBuf = String(lDataBufSize, " ")
        lResult = RegQueryValueEx(keyhand, StrKeyName, 0&, 0&, ByVal strBuf, lDataBufSize)

        If lResult = ERROR_SUCCESS Then

            intZeroPos = InStr(strBuf, Chr$(0))

            If intZeroPos > 0 Then

                GetOfficePath = Left$(strBuf, intZeroPos - 1)

            Else

                GetOfficePath = strBuf

            End If

        End If

    End If

End Function

 

 

 

其他人写的方法  很不错的 方法 :)

http://blog.csdn.net/tanaya/archive/2005/04/29/368504.aspx

本函数运行不需要机器上安装过Office

获取当前Office版本的函数

Private Sub Command1_Click()
    MsgBox GetInstalledOfficeVersion()
End Sub

'本函数运行不需要机器上安装过Office
'经典的判断Office版本函数,原创!
'支持到Office 2003
Function GetInstalledOfficeVersion() As String
    On Error Resume Next
    Dim WD
    Dim OfficeVer As String
    OfficeVer = 0
    GetInstalledOfficeVersion = ""
    Set WD = CreateObject("Word.Application.8")
    OfficeVer = CStr(WD.Version)
    WD.quit
    If Not WD Is Nothing Then Set WD = Nothing
    If InStr(OfficeVer, "8") <> 0 Then
       GetInstalledOfficeVersion = "Office 97"
    ElseIf InStr(OfficeVer, "9") <> 0 Then
       GetInstalledOfficeVersion = "Office 2000"
    ElseIf InStr(OfficeVer, "10") <> 0 Then
       GetInstalledOfficeVersion = "Office XP 2002"
    ElseIf InStr(OfficeVer, "11") <> 0 Then
       GetInstalledOfficeVersion = "Office 2003"
    End If
    If Err.Number = 424 Then
       Err.Clear
       GetInstalledOfficeVersion = "没有安装 Microsoft Office"
    End If
End Function


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:56066次
    • 积分:868
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:9篇
    • 译文:0篇
    • 评论:12条
    文章分类