Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to determine whether VBA is enabled for Office applications


View products that this article applies to.

Introduction

This article discusses how to determine whether Microsoft Visual Basic for Applications (VBA) is installed and enabled for Microsoft Office applications.

↑ Back to the top


More information

How to determine whether VBA is installed

To determine whether VBA is installed, you can call the MsiQueryFeatureState function from the Microsoft Windows Installer API. The following code sample demonstrates how to do this.

Note The code samples in this article were written by using Microsoft Visual Basic 6.0.
Private Declare Function MsiQueryFeatureState Lib "Msi" Alias "MsiQueryFeatureStateA" (ByVal Product As String, ByVal Feature As String) As Long

Private Sub Command1_Click()
   ' Start Word.
   Dim wdApp as Object
   Set wdApp = CreateObject("Word.Application")
 
   ' Get the Product Code.
   Dim szCode as String
   szCode = wdApp.ProductCode

   ' Get FeatureState for the VBAFiles Feature.
   Dim x as Long
   x = MsiQueryFeatureState(szCode, "VBAFiles")

   If (x = 1) Or (x = 3) Or (x = 4) Then
      MsgBox "VBA is installed"
   Else
      MsgBox "VBA is NOT installed"
   End If

End Sub
				

How to determine whether VBA is enabled

If VBA is installed, it may be disabled. If one of the following DWORD registry keys exists and has a value that is greater than 0, VBA is disabled.
  • Microsoft Office 2003
    HKLM\Software\Microsoft\Office\11.0\Common\VBAOff

    HKCU\Software\Microsoft\Office\11.0\Common\VBAOff
  • Microsoft Office XP
    HKLM\Software\Microsoft\Office\10.0\Common\VBAOff

    HKCU\Software\Microsoft\Office\10.0\Common\VBAOff
The following code sample queries these registry keys and then reports whether VBA is disabled for all users, is disabled for the current user only, or is not disabled.
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByVal lpType As Long, lpData As Long, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002

Private Function VBADisabled(hkeyid As Long) As Boolean
    VBADisabled = False
    
    Dim ret As Long
    Dim hKey As Long
    Dim sKeyName As String
    'In the following line, change "10.0" to "11.0" for 2003 versions.
    sKeyName = "Software\Microsoft\Office\10.0\Common"
    ret = RegOpenKey(hkeyid, sKeyName, hKey)
    If ret = 0 Then
        Dim dwValue As Long
        Dim dwLen As Long
        dwLen = 4
        ret = RegQueryValueEx(hKey, "VBAOff", 0, 0, dwValue, dwLen)
        If ret = 0 Then
            If dwValue > 0 Then VBADisabled = True
        End If
    End If
    
    RegCloseKey hKey


End Function

Private Sub Command1_Click()
    If VBADisabled(HKEY_LOCAL_MACHINE) Then
        MsgBox "VBA disabled for all users"
    ElseIf VBADisabled(HKEY_CURRENT_USER) Then
        MsgBox "VBA disabled for current user only"
    Else
        MsgBox "VBA is not disabled"
    End If

End Sub
				

↑ Back to the top


References

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
287567 Considerations for disabling VBA in Office XP
282847 Some Excel features are unavailable if you disable Visual Basic for Applications and run Excel
145679 How to use the registry API to save and retrieve setting

↑ Back to the top


Keywords: KB285884, kbhowto, kbautomation

↑ Back to the top

Article Info
Article ID : 285884
Revision : 6
Created on : 1/29/2007
Published on : 1/29/2007
Exists online : False
Views : 780