Microsoft KB Archive/285884

= How to determine whether VBA is enabled for Office applications =

Article ID: 285884

Article Last Modified on 1/29/2007

-

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 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition

-



This article was previously published under Q285884



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



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 &quot;Msi&quot; Alias &quot;MsiQueryFeatureStateA&quot; (ByVal Product As String, ByVal Feature As String) As Long

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

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

If (x = 1) Or (x = 3) Or (x = 4) Then MsgBox &quot;VBA is installed&quot; Else MsgBox &quot;VBA is NOT installed&quot; 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

 Microsoft Office XP



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 &quot;advapi32.dll&quot; Alias &quot;RegOpenKeyA&quot; (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegQueryValueEx Lib &quot;advapi32.dll&quot; Alias &quot;RegQueryValueExA&quot; (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 &quot;advapi32.dll&quot; (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 &quot;10.0&quot; to &quot;11.0&quot; for 2003 versions. sKeyName = &quot;Software\Microsoft\Office\10.0\Common&quot; ret = RegOpenKey(hkeyid, sKeyName, hKey) If ret = 0 Then Dim dwValue As Long Dim dwLen As Long dwLen = 4 ret = RegQueryValueEx(hKey, &quot;VBAOff&quot;, 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 &quot;VBA disabled for all users&quot; ElseIf VBADisabled(HKEY_CURRENT_USER) Then MsgBox &quot;VBA disabled for current user only&quot; Else MsgBox &quot;VBA is not disabled&quot; End If

End Sub

