简体   繁体   English

在 Excel VBA 中关闭受保护的视图

[英]Turn off Protected View in Excel VBA

I need to extract data from a file ('DatabaseExport.xlsx') that has been generated and newly opened from an intranet site.我需要从已从 Intranet 站点生成并新打开的文件 ('DatabaseExport.xlsx') 中提取数据。 The file opens in Protected View (yellow banner at top of screen).该文件在受保护的视图中打开(屏幕顶部的黄色横幅)。 I think it's necessary to disable the protected view so that I can extract data from the file.我认为有必要禁用受保护的视图,以便我可以从文件中提取数据。 My VBA is contained in a separate file 'GetAndAnalyseData.xlsm':我的 VBA 包含在一个单独的文件“GetAndAnalyseData.xlsm”中:

NameOfNewFile = "ooo"
Do
    On Error Resume Next
        Application.ActiveProtectedViewWindow.Edit  'This never works first time
        NameOfNewFile = Left(ActiveWorkbook.Name, 14)
    On Error GoTo 0

    If NameOfNewFile = "ooo" Then
        ttt = MsgBox("this should not be possible!", vbOKCancel)
        If ttt = vbCancel Then Stop
    End If
Loop While NameOfNewFile = "ooo"

When I run the code:当我运行代码时:

  1. I get a message box saying "this should not be possible!"我收到一个消息框说“这不应该是不可能的!”
  2. If I click OK, I keep getting the same messagebox, and the file remains in Protected View.如果我单击“确定”,我会继续收到相同的消息框,并且文件仍保留在受保护的视图中。
  3. If I click Cancel and then F5 (to continue execution) the loop works as it should (protected view is cancelled, the value of TEMP is set to the filename, the loop exits and the macro continues.如果我单击取消,然后按 F5(继续执行),循环会按原样工作(受保护的视图被取消,TEMP 的值设置为文件名,循环退出,宏继续。

The line where I try to assign a value to NameOfNewFile returns an error if I don't use 'Resume Next':如果我不使用“继续下一步”,我尝试为 NameOfNewFile 分配值的行将返回错误:

Run-time error '91': Object variable or With block variable not set运行时错误“91”:未设置对象变量或块变量

I have tried several methods to fix this:我尝试了几种方法来解决这个问题:

  1. Running this line before downloading the file, and also immediately before attempting to turn off Protected View, but still the behaviour is the same.在下载文件之前以及在尝试关闭受保护的视图之前立即运行此行,但行为仍然相同。

     Application.AutomationSecurity = msoAutomationSecurityForceDisable
  2. Making the folder (where the downloaded file is stored) into a trusted location, but this is not allowed by my user settings.将文件夹(存储下载文件的位置)设置为受信任的位置,但我的用户设置不允许这样做。

  3. Unchecking the "Enable Protected View for files originating from the Internet" box in Trust Center, but with this box unchecked I can't open the file.取消选中信任中心中的“为源自 Internet 的文件启用受保护的视图”框,但取消选中此框后我无法打开该文件。

  4. Inserting this code (but the "Set wbPV line" causes Run-time error '424': Object Required):插入此代码(但“设置 wbPV 行”会导致运行时错误“424”:需要对象):

     Dim wbPV As Workbook If Application.ProtectedViewWindows.Count > 0 Then Set wbPV = ActiveProtectedViewWindow.Edit End If

How can I turn off Protected View programmatically for this type of downloaded file?如何以编程方式关闭此类下载文件的受保护视图? Or is there some other way to get data out of the file?或者有没有其他方法可以从文件中获取数据?

Option Explicit选项显式

    Sub Auto_edit_Workbooks()

    Dim wb As Workbook
    Dim wbPV As ProtectedViewWindow
        If Application.ProtectedViewWindows.Count > 0 Then
            For Each wbPV In Application.ProtectedViewWindows
            wbPV.Activate
            Set wb = wbPV.Edit()
            Next wbPV
        Else
        End If
    Set wb = Nothing
    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM