How to determine whether VBA is enabled for Office applications

Article translations Article translations
Article ID: 285884 - View products that this article applies to.
This article was previously published under Q285884
Expand all | Collapse all

On This Page

INTRODUCTION

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

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
				

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

Properties

Article ID: 285884 - Last Review: January 29, 2007 - Revision: 4.2
APPLIES TO
  • Microsoft Office Excel 2003
  • Microsoft Office PowerPoint 2003
  • Microsoft Office Word 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft Word 2002
  • Microsoft Visual Basic 6.0 Professional Edition
Keywords: 
kbautomation kbhowto KB285884

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com