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

Vba 从Access中以编程方式打开的Excel工作表中删除受保护的视图,vba,excel,ms-access,Vba,Excel,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

我使用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中添加了“If”语句,因为我希望它会删除“受保护的视图-由于信任中心中的文件块设置,不建议编辑此文件类型”消息。我试图实现的是删除“启用编辑”按钮,以便此宏能够启用编辑并按计划运行


目前,代码位于“设置wb”行。实现我的目标的正确方法是什么?

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

这可能是有害的


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

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

以下是我在以下网站上找到的一些修订代码:

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

子文件块(值与长度相同) Const HKEY_当前用户=&H80000001

Dim oRegistry
Dim sParentKey
Dim vers As Variant
Dim item As String: item = filetype_to_change_fileblock
'确定用户MyDocuments文件夹的位置/路径 '******************************************************************************* Set-oRegistry=GetObject(“winmgmts:\。\root\default:StdRegProv”)


删除受保护的视图需要用户权限。如果有一种方法可以在没有用户干预的情况下实用地删除它,这将是一种安全漏洞。我相当肯定我已经看到了它的实现。我不关心这里的安全问题,因为这纯粹是为我和一位同事做的。这真的是万不得已的办法。如果我可以通过编程的方式,仅针对这几个实例,这将是我的首选。此代码可以更改文件块设置…该值给出了安全级别。no security=0 security for open=1 security for open and save=2此代码可以添加受信任的位置…..如果要添加网络位置,请与您的管理员核实有关网络位置的安全性。下面是支持您声明的链接:它清楚地表明:使用整数变量不再有性能优势;事实上,长变量可能会稍微快一些,因为VBA不必转换它们。
Dim oRegistry
Dim sParentKey
Dim vers As Variant
Dim item As String: item = filetype_to_change_fileblock
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