Thursday, September 8, 2011

Toggle the SQL security entry in the registry through vba



You receive the "Opening this will run the following SQL command" message when you open a Word mail merge main document that is linked to a data source - http://support.microsoft.com/?kbid=825765

This linked page explains how to create registry entries to turn off the security message. Some users have been concerned about the security implications of turning off this warning message. The following code was conceived with that issue in mind. The macro creates the registry entry if it is not present and then toggles the setting between 0 and 1 each time the macro is run. It could therefore be adapted for use in a mail merge macro to switch off the warning while the particular merge was run, then switch it back on again on completion.



Sub ToggleSQLSecurity()
Dim WSHShell, RegKey, rKeyWord, wVer
Set WSHShell = CreateObject("WScript.Shell")
If Val(Application.Version) < 10 Then 'The security issue relates to
'Word versions from 10.0 (Word 2002)
     MsgBox "This macro is for Word 2002 and later!", vbOKOnly, "Wrong Word Version"
     Exit Sub
End If
Start:
RegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & wVer & "\Word\Options\"
On Error Resume Next 'The registry key does not exist
rKeyWord = WSHShell.RegRead(RegKey & "SQLSecurityCheck")
If rKeyWord = "" Then
     WSHShell.regwrite RegKey & "SQLSecurityCheck", 1, "REG_DWORD" 'set it at zero
     GoTo Start: 'and read it again
End If
If rKeyWord = 1 Then
     WSHShell.regwrite RegKey & "SQLSecurityCheck", 0, "REG_DWORD"
     MsgBox "SQL Security checking is switched off", vbInformation, "SQL Check"

Else
     WSHShell.regwrite RegKey & "SQLSecurityCheck", 1, "REG_DWORD"
     MsgBox "SQL Security checking is switched on", vbInformation, "SQL Check"
End If
End Sub

 

No comments:

Post a Comment