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

时间:2014-09-17 11:47:25

标签: excel vba excel-vba ms-access

我有一个电子表格,我使用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

我添加了"如果" sub中的语句,因为我希望它会删除"受保护的视图 - 由于信任中心的文件阻止设置,建议不要编辑此文件类型。信息。我想要实现的目标是获得"启用编辑"按钮已删除,因此该宏可以启用编辑并按计划运行。

目前,代码属于"设置wb"线。实现我之后的正确方法是什么?

4 个答案:

答案 0 :(得分:5)

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

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

Public Sub MySubroutine()
    Dim lSecurity As Long

    lSecurity = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityLow

    '''''''''''''''''''''
    '   Run code here   '
    '''''''''''''''''''''

    Application.AutomationSecurity = lSecurity
End Sub

作为旁注,VBA将Integer实现为Long,因此声明Integer变量实际上可能会稍微降低性能,因为它必须重新解释Integer关键字。当我了解到这一点时,我开始将整数声明为Long。我实际上是在一些Microsoft文档中读到过这个,但几年前我失去了它的链接。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

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/

这可能有害。

此外,您应该设置受信任的位置。

答案 3 :(得分:0)

子文件块(值为长)  Const HKEY_CURRENT_USER =& H80000001

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

'确定用户的MyDocuments文件夹的位置/路径 “******************* ******************************     设置oRegistry = GetObject(“winmgmts:\。\ root \ default:StdRegProv”)

vers = Application.Version

sParentKey = "Software\Microsoft\Office\" & vers & "\Excel\Security\FileBlock"

oRegistry.SetDWORDValue HKEY_CURRENT_USER, sParentKey, item, value


End Sub