這裏遲到了,但這是一個常見的煩惱:您需要定義一個'可信位置'。
大部分開發商遇到時,他們的代碼試圖打開一個電子表格文件,你所看到的問題,他們得到這種無益的錯誤信息:
「辦公室已檢測到該文件中的問題爲幫助保護您的計算機。此文件無法打開。「
如果你是中級到專家的VBA編碼器(或與任何常見的腳本語言)查找由丹尼爾Pineault上DevHut.net在2010年公佈的受信任位置代碼:
DevHut code example: Trusted Location using VBScript
爲了您的方便,這是我在Excel中實現它:
Public Sub TrustThisFolder(Optional FolderPath As String, _
Optional TrustSubfolders As Boolean = True, _
Optional TrustNetworkFolders As Boolean = False, _
Optional sDescription As String)
' Add a folder to the 'Trusted Locations' list so that your project's VBA can
' open Excel files without raising errors like "Office has detected a problem
' with this file. To help protect your computer this file cannot be opened."
' Ths function has been implemented to fail silently on error: if you suspect
' that users don't have permission to assign 'Trusted Location' status in all
' locations, reformulate this as a function returning True or False
' Nigel Heffernan January 2015
'
' Based on code published by Daniel Pineault in DevHut.net on June 23, 2010:
' www.devhut.net\2010\06\23\vbscript-createset-trusted-location-using-vbscript\
' **** **** **** **** THIS CODE IS IN THE PUBLIC DOMAIN **** **** **** ****
' UNIT TESTING:
'
' 1: Reinstate the commented-out line 'Debug.Print sSubKey & vbTab & sPath
' 2: Open the Immediate Window and run this command:
' TrustThisFolder "Z:\", True, True, "The user's home directory"
' 3: If "Z:\" is already in the list, choose another folder
' 4: Repeat step 2 or 3: the folder should be listed in the debug output
' 5: If it isn't listed, disable the error-handler and record any errors
'
On Error GoTo ErrSub
Dim sKeyPath As String
Dim oRegistry As Object
Dim sSubKey As String
Dim oSubKeys ' type not specified. After it's populated, it can be iterated
Dim oSubKey ' type not specified.
Dim bSubFolders As Boolean
Dim bNetworkLocation As Boolean
Dim iTrustNetwork As Long
Dim sPath As String
Dim sDate As String
Dim sDesc As String
Dim i As Long
Const HKEY_CURRENT_USER = &H80000001
bSubFolders = True
bNetworkLocation = False
If FolderPath = "" Then
FolderPath = FSO.GetSpecialFolder(2).Path
If sDescription = "" Then
sDescription = "The user's local temp folder"
End If
End If
If Right(FolderPath, 1) <> "\" Then
FolderPath = FolderPath & "\"
End If
sKeyPath = ""
sKeyPath = sKeyPath & "SOFTWARE\Microsoft\Office\"
sKeyPath = sKeyPath & Application.Version
sKeyPath = sKeyPath & "\Excel\Security\Trusted Locations\"
Set oRegistry = GetObject("winmgmts:\.\root\default:StdRegProv")
' Note: not the usual \root\cimv2 for WMI scripting: the StdRegProv isn't in that folder
oRegistry.EnumKey HKEY_CURRENT_USER, sKeyPath, oSubKeys
For Each oSubKey In oSubKeys
sSubKey = CStr(oSubKey)
oRegistry.GetStringValue HKEY_CURRENT_USER, sKeyPath & "\" & sSubKey, "Path", sPath
'Debug.Print sSubKey & vbTab & sPath
If sPath = FolderPath Then
Exit For
End If
Next oSubKey
If sPath <> FolderPath Then
If IsNumeric(Replace(sSubKey, "Location", "")) Then
i = CLng(Replace(sSubKey, "Location", "")) + 1
Else
i = UBound(oSubKeys) + 1
End If
sSubKey = "Location" & CStr(i)
If TrustNetworkFolders Then
iTrustNetwork = 1
oRegistry.GetDWORDValue HKEY_CURRENT_USER, sKeyPath, "AllowNetworkLocations", iTrustNetwork
If iTrustNetwork = 0 Then
oRegistry.SetDWORDValue HKEY_CURRENT_USER, sKeyPath, "AllowNetworkLocations", 1
End If
End If
oRegistry.CreateKey HKEY_CURRENT_USER, sKeyPath & "\" & sSubKey
oRegistry.SetStringValue HKEY_CURRENT_USER, sKeyPath & "\" & sSubKey, "Path", FolderPath
oRegistry.SetStringValue HKEY_CURRENT_USER, sKeyPath & "\" & sSubKey, "Description", sDescription
oRegistry.SetDWORDValue HKEY_CURRENT_USER, sKeyPath & "\" & sSubKey, "AllowSubFolders", 1
End If
ExitSub:
Set oRegistry = Nothing
Exit Sub
ErrSub:
Resume ExitSub
End Sub
做什麼,請保持代碼的確認,如果你重用它:這會區分你(和來自其他帖子和其他專家(和其他人)在沒有確認的情況下交換知識的網站。
是您的自我認證選項: http://office.microsoft.com/en-us/access/HP010397921033.aspx? – Fionnuala 2010-06-03 07:19:35
這麼貴嗎? – 2010-06-04 12:46:06