从Access中以编程方式打开Excel工作表中删除受保护的视图

我有一个电子表格,我使用Access中的VBA以编程方式打开:

Set xl = CreateObject("Excel.Application") With xl Call RunASCFormatting(xl, wb, strPath) 'More code Sub RunASCFormatting(xl As Excel.Application, wb As Excel.Workbook, strPath As String) With xl If .ProtectedViewWindows.count > 0 Then .ActiveProtectedViewWindow.Edit End If Set wb = .Workbooks.Open(Trim(strPath) & "ASC.xls", True, False) wb.Sheets(1).Rows("1:1").Delete Shift:=xlUp .ActiveWorkbook.SaveAs FileName:=Trim(strPath) & "ASC.xlsx" _ , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False End With End Sub 

我已经在子文件的“If”语句中添加了,因为我希望它会删除“受保护的视图 – 由于您信任中心中的文件阻止设置,不推荐编辑此文件types”消息。 我想要实现的是取消“启用编辑”button,所以这个macros可以启用编辑和按计划运行。

目前,代码位于“Set wb”行。 什么是正确的方式来实现我所追求的?

一种可能性是在打开Excel工作簿之前以编程方式将macros安全设置更改为最低值。 操作数据后,重新启用macros安全性的以前设置。

以下是我在http://www.mrexcel.com/forum/excel-questions/631545-change-trust-center-settings-visual-basic-applications.html上find的一些修改过的代码:

 Public Sub MySubroutine() Dim lSecurity As Long lSecurity = Application.AutomationSecurity Application.AutomationSecurity = msoAutomationSecurityLow ''''''''''''''''''''' ' Run code here ' ''''''''''''''''''''' Application.AutomationSecurity = lSecurity End Sub 

作为一个方面的评论,VBA实现Integer为Long,所以实际上可能会略微更高的性能降低来声明Integervariables,因为它必须重新解释Integer关键字。 当我了解到这一点时,我开始声明一个Integer为Long。 实际上我在一些微软的文档中看到了这个,但是我几年前就失去了它的链接。

您可以尝试closures信任中心中的受保护视图设置

http://office.microsoft.com/en-us/excel-help/what-is-protected-view-HA010355931.aspx#BM5

http://www.howtogeek.com/60310/enable-editing-for-all-office-2010-documents-by-disabling-protected-view/

这可能是有害的。

另外你应该设置受信任的位置。

Sub fileBlock(value As Long)Const HKEY_CURRENT_USER =&H80000001

 Dim oRegistry Dim sParentKey Dim vers As Variant Dim item As String: item = filetype_to_change_fileblock 

'确定用户的MyDocuments文件夹的位置/path'************************************** ***************************************** Set oRegistry = GetObject(“winmgmts:\ \ ROOT \默认:的StdRegProv“)

 vers = Application.Version sParentKey = "Software\Microsoft\Office\" & vers & "\Excel\Security\FileBlock" oRegistry.SetDWORDValue HKEY_CURRENT_USER, sParentKey, item, value End Sub 
 Sub trusted_locations(path_to_add) Const HKEY_CURRENT_USER = &H80000001 Dim oRegistry Dim sDescription 'Description of the Trusted Location Dim bAllowSubFolders 'Enable subFolders as Trusted Locations Dim bAllowNetworkLocations 'Enable Network Locations as Trusted ' Locations Dim bAlreadyExists Dim sParentKey Dim iLocCounter Dim arrChildKeys Dim sChildKey Dim sValue Dim sNewKey Dim vers As Variant 'Determine the location/path of the user's MyDocuments folder '******************************************************************************* Set oRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") bAllowSubFolders = True bAlreadyExists = False vers = Application.Version sParentKey = "Software\Microsoft\Office\" & vers & "\Excel\Security\Trusted Locations" iLocCounter = 0 oRegistry.EnumKey HKEY_CURRENT_USER, sParentKey, arrChildKeys For Each sChildKey In arrChildKeys oRegistry.GetStringValue HKEY_CURRENT_USER, sParentKey & "\" & sChildKey, "Path", sValue If sValue = spath Then bAlreadyExists = True If CInt(Mid(sChildKey, 9)) > iLocCounter Then iLocCounter = CInt(Mid(sChildKey, 9)) End If Next 'Uncomment the following 4 linesif your wish to enable network locations as Trusted ' Locations bAllowNetworkLocations = True If bAllowNetworkLocations Then oRegistry.SetDWORDValue HKEY_CURRENT_USER, sParentKey, "AllowNetworkLocations", 1 End If If bAlreadyExists = False Then sNewKey = sParentKey & "\Location" & CStr(iLocCounter + 1) oRegistry.CreateKey HKEY_CURRENT_USER, sNewKey oRegistry.SetStringValue HKEY_CURRENT_USER, sNewKey, "Path", path_to_be_added oRegistry.SetStringValue HKEY_CURRENT_USER, sNewKey, "Description", description_of_path If bAllowSubFolders Then oRegistry.SetDWORDValue HKEY_CURRENT_USER, sNewKey, "AllowSubFolders", 1 End If End If End Sub