Thay đổi security level của của các ứng dụng trong bộ MS Office

Liên hệ QC

PhanTuHuong

VBA & VB.NET for Excel & AutoCad
Thành viên danh dự
Tham gia
13/6/06
Bài viết
7,121
Được thích
24,279
Tôi vẫn chưa tìm ra cách để huỷ bỏ cửa sổ Security Warning trong Excel, các macro vẫn hoạt động bình thường. Tôi có post được code này, mà vẫn chưa sử dụng được, các bạn nghiên cứu rồi phản hồi nhé:

The following code demostrates how to turn on/off macro virus protection for Office 2003/XP/2000 and Excel 97.
Option Compare Text
Option Explicit

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lhKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal lhKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal lhKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Const REG_HEX As Long = 4 'Hex Key
Private Const HKEY_CURRENT_USER As Long = &H80000001

Public Enum eApplication
eExcel
eOutlook
eWord
ePowerPoint
ePublisher
End Enum

Public Enum eVersion
eOffice2003
eOffice2000
eOfficeXP
End Enum



'Purpose : Enables or disables the macro virus alert.
'Inputs : bDisableVirusChecking True, disables macro security protection
' False, enables macro security protection
'Outputs : Returns True on success
'Author : Andrew Baker (www.vbusers.com)
'Date : 25/11/2000 03:33
'Notes : Requires Excel 97
'Revisions :

Function Excel97MacroSecurity(bDisableVirusChecking As Boolean) As Boolean
Dim lData As Long, lRet As Long
Const csPath = "Software\Microsoft\Office\8.0\Excel\Microsoft Excel", csValue = "Options6"

On Error GoTo ErrFailed
If bDisableVirusChecking Then
lData = 0 'Disabled
Else
lData = 8 'Enabled
End If
RegCreateKey HKEY_CURRENT_USER, csPath, lRet
RegSetValueEx lRet, csValue, 0, REG_HEX, lData, 4
RegCloseKey lRet
Excel97MacroSecurity = True

Exit Function

ErrFailed:
Debug.Print Err.Description
Excel97MacroSecurity = False
End Function



'Purpose :Enables or disables the macro virus alert by altering the security level in the registry.
'Inputs :lSecurityLevel 1, sets security to "Low" (disable virus alerts)
' 2, sets security to "Medium"
' 3, sets security to "High"
' 4, sets security to "High" and disables access to VB Object Model
' AppType The application to set the security for.
' OfficeVersion The installed version of Office.
'Outputs : Returns True on success
'Author : Andrew Baker (www.vbusers.com)
'Date : 25/11/2000 03:33
'Notes : Requires Excel 2000
'Revisions :

Function OfficeMacroSecurity(lSecurityLevel As Long, AppType As eApplication, OfficeVersion As eVersion) As Boolean
Dim sData As String, lRet As Long, sAppKey As String
Const csKeyGen As String = "Software\Microsoft\Office\[VERSION]\[APPLICATION]\Security"
Const csKeyVBOM As String = "AccessVBOM", csKey As String = "Level"

If lSecurityLevel <= 4 And lSecurityLevel > 0 Then
On Error GoTo ErrFailed
Select Case OfficeVersion
Case eOffice2000
sAppKey = Replace$(csKeyGen, "[VERSION]", "9.0")
Case eOfficeXP
sAppKey = Replace$(csKeyGen, "[VERSION]", "10.0")
Case eOffice2003
sAppKey = Replace$(csKeyGen, "[VERSION]", "11.0")
Case Else
Debug.Print "Invalid version"
Debug.Assert False
OfficeMacroSecurity = False
Exit Function
End Select

Select Case AppType
Case eExcel
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Excel")
Case eOutlook
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Outlook")
Case ePowerPoint
sAppKey = Replace$(sAppKey, "[APPLICATION]", "PowerPoint")
Case ePublisher
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Publisher")
Case eWord
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Word")
Case Else
Debug.Print "Invalid application"
Debug.Assert False
OfficeMacroSecurity = False
Exit Function
End Select

RegCreateKey HKEY_CURRENT_USER, sAppKey, lRet
'Set the registry key macro security reg key
If lSecurityLevel = 4 Then
'Set to high
RegSetValueEx lRet, csKey, 0, REG_HEX, 3, 4
Else
'Set to the specified level
RegSetValueEx lRet, csKey, 0, REG_HEX, lSecurityLevel, 4
End If

If lSecurityLevel < 4 Then
'Enable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 1, 4
Else
'Disable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 0, 4
End If

RegCloseKey lRet

OfficeMacroSecurity = True
Else
Debug.Print "Invalid Security Level"
Debug.Assert False
End If

Exit Function

ErrFailed:
Debug.Print Err.Description
OfficeMacroSecurity = False
End Function
 
Tôi vẫn chưa tìm ra cách để huỷ bỏ cửa sổ Security Warning trong Excel, các macro vẫn hoạt động bình thường. Tôi có post được code này, mà vẫn chưa sử dụng được

Thông thường thì người ta sửa registry của các ứng dụng trong bộ Office tùy theo mỗi loại ứng dụng và version của ứng dụng đó.
csKeyGen = "Software\Microsoft\Office\[VERSION]\[APPLICATION]\Security"

1/ Ở đó [VERSION] có thể là:
Case eOffice2000
sAppKey = Replace$(csKeyGen, "[VERSION]", "9.0")
Case eOfficeXP
sAppKey = Replace$(csKeyGen, "[VERSION]", "10.0")
Case eOffice2003
sAppKey = Replace$(csKeyGen, "[VERSION]", "11.0")

2/ [APPLICATION] có thể là

sAppKey = Replace$(sAppKey, "[APPLICATION]", "Excel")
Case eOutlook
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Outlook")
Case ePowerPoint
sAppKey = Replace$(sAppKey, "[APPLICATION]", "PowerPoint")
Case ePublisher
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Publisher")
Case eWord
sAppKey = Replace$(sAppKey, "[APPLICATION]", "Word")

3/ Và đoạn này là cái mấu chốt của vấn đề:
---------------------------------------------------
RegCreateKey HKEY_CURRENT_USER, sAppKey, lRet
'Set the registry key macro security reg key
If lSecurityLevel = 4 Then
'Set to high
RegSetValueEx lRet, csKey, 0, REG_HEX, 3, 4
Else
'Set to the specified level
RegSetValueEx lRet, csKey, 0, REG_HEX, lSecurityLevel, 4
End If

If lSecurityLevel < 4 Then
'Enable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 1, 4
Else
'Disable access to VB Object Model
RegSetValueEx lRet, csKeyVBOM, 0, REG_HEX, 0, 4
---------------------------------------------------

Thông thường để Disable Sercurity thì việc này thì nên thực hiện trước khi chạy ứng dụng (nghĩa là trước khi chạy file Excel thì đã nên Disable rồi). Ở VNUNI A-Excel thì chúng tôi làm việc Set Sercurity = Low ngay từ lúc cài đặt chương trình.

Vấn đề ở đây chỉ là biết cách thiết lập Registry như thế nào thôi.

Chiều nay nếu rảnh (hy vọng thế) tôi sẽ dùng VB để test đoạn code của PhanTuHuong

À, có 2 cái pictures tôi muốn show cho mọi người xem để thấy mọi chuyện sáng như ban ngày mà chưa gửi được. Để tôi send sang chỗ khác vậy.

i606_ExcelSecurityLevelMonitorbyRegmon.JPG

i607_ExcelSecurityLevelReg.JPG
 
Lần chỉnh sửa cuối:
Upvote 0
Dear all,
--------
Theo em mấu chốt là ở mấy cái Declare này:
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lhKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal lhKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal lhKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Thực chất khai báo này tham chiếu đến các thư viện liên kết động advapi32.dll nhằm chọt ngoắy vào Regetry bởi hàm OfficeMacroSecurity. Nếu nắm bắt được cấu trúc và cú pháp các chương trình con bên trong advapi32.dll, chúng ta hoàn toàn điểu khiển được "cái REG huyền bí bấy lâu nay" theo ý muốn.

Cám ơn các anh chị đã cho em học hỏi rất nhiều!
 
Lần chỉnh sửa cuối:
Upvote 0
Đào Việt Cường đã viết:
Dear all,
--------
Theo em mấu chốt là ở mấy cái Declare này:

Thực chất chuyện khai báo APIs là hiển nhiên rồi mà. Thiết tưởng khỏi cần nói đến vấn đề đó chứ. Nhưng dùng mấy cái đó để sửa vào đâu trong Reg mới là quan trọng.

Còn code VB cho Registry thì chỉ cần Google 1 phát là ra cả đống ngay mà.

Nhưng mà, nhìn vào 2 cái hình nói trên thì ... sáng như ban ngày rồi.

Và phải công nhận cái code của Andrew Baker (www.vbusers.com) là rất sáng sủa đấy. Nhìn vào thấy cũng được được.

@Admin, Mod: Tách bài này ra thành 1 topic có chủ đề: "Thay đổi Security Level của các ứng dụng trong MS Office"
 
Upvote 0
Web KT
Back
Top Bottom