Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 为受保护的工作簿自动弹出跳过窗口_Vba_Excel - Fatal编程技术网

Vba 为受保护的工作簿自动弹出跳过窗口

Vba 为受保护的工作簿自动弹出跳过窗口,vba,excel,Vba,Excel,我现在使用以下命令逐个打开一组工作簿: Workbooks.Open Filename:=fFile, Password:="", UpdateLinks:=xlUpdateLinksNever, ReadOnly:=False 通常,文件是在写启用模式下打开的,这是我想要的。但对于某些文件,前一行会弹出以下窗口: 在这种情况下,我需要按只读打开它。尽管它是只读的,但能够打开它仍然很好 因此,总而言之,我尝试以写启用模式打开文件,如果某些文件不可能,则以只读模式打开它们仍然可以。但由于文件的数

我现在使用以下命令逐个打开一组工作簿:

Workbooks.Open Filename:=fFile, Password:="", UpdateLinks:=xlUpdateLinksNever, ReadOnly:=False
通常,文件是在写启用模式下打开的,这是我想要的。但对于某些文件,前一行会弹出以下窗口:

在这种情况下,我需要按只读打开它。尽管它是只读的,但能够打开它仍然很好

因此,总而言之,我尝试以写启用模式打开文件,如果某些文件不可能,则以只读模式打开它们仍然可以。但由于文件的数量很大,我真的想将其自动化,并避免出现这种弹出窗口。有人能告诉我怎么做吗


一种可能的方法是首先以只读模式打开所有文件,然后如果可能的话将它们转换为写启用模式。你认为这可行吗?

这是你可能的解决办法。其思想是将任何不正确的密码传递给WriteResPassword参数。如果文件受保护,将抛出错误。如果是这样,您将能够识别该文件并以只读模式打开它。或者,其他文件的密码将被忽略,而fill将以读写模式打开

下面代码中的一些附加注释

Sub PossibleWorkaround()

    Dim Pass As String
    'any password
    Pass = "blahblah"

    'file which is write-protected will throw error during opening it _
    with incorrect password
    On Error Resume Next
    Workbooks.Open "c:\users\alpha\desktop\filename.xlsx", , , , , Pass
    If Err.Number = 1004 Then
        'if so, try to open it in read-only mode
        Workbooks.Open "c:\users\alpha\desktop\filename.xlsx", , True
    End If
    'return to standard error handling or set as expected
    On Error GoTo 0



    'the same for file which is not write-protected
    'incorrect password will be ignored
    On Error Resume Next
    Workbooks.Open "c:\users\alpha\desktop\filename A.xlsx", , , , , Pass
    If Err.Number = 1004 Then
        'therefore this if statement will not be called
        Workbooks.Open "c:\users\alpha\desktop\filename.xlsx", , True
    End If
    On Error GoTo 0
End Sub

我也有同样的问题。我疲于在许多公式上找到答案,却一无所获。最后,我在Worksbooks.Open语句的末尾添加了WriteResPassword:=password,它不再提示输入密码


工作簿.Open Filename:=\path to the file\file\u name.xlsm,Password:=Password here,WriteResPassword:=Password here

您可以使用工作簿.Open方法中的notify选项在只读模式下打开而无需提示。IE工作簿.Open Filename:=fFile,Password:=,UpdateLinks:=xlUpdateLinksNever,Notify:=True…Notify mode:如果无法在读/写模式下打开文件,则此参数为True以将文件添加到文件通知列表。Microsoft Excel将以只读方式打开文件,轮询文件通知列表,然后在文件可用时通知用户。如果此参数为False或省略,则不请求通知,任何打开不可用文件的尝试都将失败。@KazJaw-如果该文件因被其他用户打开而被保留,则不会触发密码提示。这里是工作簿所有选项的链接。打开-谷歌是你的选择friend@Sorceri我尝试了工作簿。打开文件名:=fFile,密码:=,UpdateLinks:=xlUpdateLinksNever,ReadOnly:=False,notify:=False和工作簿。打开文件名:=fFile,密码:=,UpdateLinks:=xlUpdateLinksNever,ReadOnly:=False,notify:=True,窗口总是弹出。。。您知道原因吗?请尝试关闭警报:Application.DisplayAlerts=false@Sorceri那没用…谢谢你的回答。。。但问题是,当它运行到工作簿时。打开c:\users\alpha\desktop\filename.xlsx,,,,,Pass,窗口弹出,它不会抛出错误…你能复制你的真实代码来评论这一行吗?你运行的是哪个office版本?工作簿。打开文件名:=fFile,密码:=,UpdateLinks:=xlupdatelinks从来都不是真实的代码。我使用的是Excel 2010…我指的是我的想法——你实施它的方式。你要评论的不是我提出的想法。。。