如何抑制使用中的文件警报消息

时间:2018-03-19 14:23:16

标签: excel-vba vba excel

我在excel VBA中使用以下代码从excel表单更新数据库。使用double times[99];Application.Displayalert = False后,我仍然会收到正在使用的文件警告消息。

Application.ScreenUpdating = False

为什么我得到它?我怎么能抑制它?

1 个答案:

答案 0 :(得分:1)

而不是你的代码

Workbooks.Open Filename:=path & wbnam, WriteResPassword:="Magellan7", ReadOnly:=False

Set Dest = ActiveWorkbook

If Dest.ReadOnly Then
Dest.Close
MsgBox "Someone currently updating UM. Please try again later.", vbInformation
Exit Sub
End If

你会写

   Path = "Z:\General\"
    wbnam = "Data Base_2018.xlsb"
    fileName = Path & wbnam

    If IsFileOpen(fileName) Then
        MsgBox "Someone currently updating UM. Please try again later.", vbInformation
        Exit Sub
    End If

    Set dest = Workbooks.Open(fileName:=fileName, WriteResPassword:="Magellan7", ReadOnly:=False)

您需要从here

添加函数IsFileOpen
Function IsFileOpen(fileName As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open fileName For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function