Condividi tramite

verificare se un file excel xlms è aperto o chiuso , aprirlo solo se e chiuso

Anonimo
2016-08-27T14:26:00+00:00

Sub TestFileOpened()

    ' ---------- testo se il file e aperto ------------

    If IsFileOpen("C:\Users\Giorgio\Desktop\prove\calendario.xlms") Then

        MsgBox "Il file è in uso !"

    Else

        MsgBox "Il file adesso è libero!"

        ' --- e apro il file ----------

        Workbooks.Open "C:\Users\Giorgio\Desktop\prove\calendario.xlms"

    End If

End Sub

' ------------------------------------------ function ----------------------

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

Questo codice è chiaro , ma a me non funziona più , lo usavo per excel 2007

c'è qualcuno che può dirmi come verificare se un file e aperto o chiuso ? e decidere di aprirlo solo se e chiuso

grazie

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2016-08-28T10:45:44+00:00

Ciao Giorgio,

ho semplicemente inserito il modulo tuo e provato .

non funziona perchè non trova il percorso , strano 

Credo che il tuo problema non sia un problema di codice ma anzi sia  una questione di fatto! Il percorso **C:\Users\Giorgio\Desktop\prove**"  

esiste o non esiste e il file Calendario.xlms  si trova a quel percorso o meno!

Sostituendo il percorso e il nome del file, il codice funziona a me

senza alcun problema.

Tuttavia, credo sia probabile che tu abbia fatto un errore di ortografia nel nome del tuo file. Quindi, riprova il codice sostituendo Calendario.xlms

con: Calendario.xlsm

                   .

===

Regards,

Norman

La risposta è stata utile?

0 commenti Nessun commento

4 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2016-08-28T13:34:40+00:00

    Ciao Giorgio,

    Grazie Norman , è stato un piacere .

    Mi fa piacere che tu abbia risolto il problema e ti ringrazio per il cortese riscontro.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-08-28T12:36:01+00:00

    Grazie Norman , è stato un piacere .

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2016-08-28T09:56:15+00:00

    Ciao Norman , grazie per la risposta 

    ho semplicemente inserito il modulo tuo e provato .

    non funziona perchè non trova il percorso , strano 

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2016-08-27T19:56:06+00:00

    Ciao Giorgio,

    Prova qualcosa del genere:

    '=========>>

    Option Explicit

    '--------->>

    Public Sub Demo()

        Dim WB As Workbook

        Dim sPath As String, sStr As String

        Const sNome As String = "Calendario.xlms"           '<<===Modifica

        Const sPercorso As String = _

                        "C:\Users\Giorgio\Desktop\prove**"       '<<=== Modifica**

        If Not IsWorkbookOpen(sNome) Then

            sStr = Application.PathSeparator

            If Right(sPercorso, 1) = sStr Then

                sPath = sPercorso

            Else

                sPath = sPercorso & sStr

            End If

            Set WB = Workbooks.Open(sPath & sNome)

        Else

                Set WB = Workbooks(sNome)

        End If

    End Sub

    '--------->>

    Public Function IsWorkbookOpen(sName As String) As Boolean

        On Error Resume Next

        IsWorkbookOpen = Not (Workbooks(sName) Is Nothing)

        On Error GoTo 0

    End Function

    '<<=========

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento