Aprire file tramite vba



  • Aprire file tramite vba
    di Totum (utente non iscritto) data: 14/10/2014 19:23:10

    Apro diversi file su cui lavorare con una macro, vorrei che la macro mi dica che il file che voglio aprire è già aperto, vorrei inoltre sapere se è possibile, sempre tramite macro, chiudere tutti i file aperti tranne uno.
    Grazie per l'aiuto



  • di Zer0Kelvin data: 14/10/2014 22:52:11

    Salve.
    Per sapere se unfile è gia aperto puoi usare la function riportata sotto.
    Per quanto riguarda la chiusura di tutti i files tranne uno, intendi tutti tranne il file che contiene la macro?
     
    ' This function checks to see if a file is open or not. If the file is
    ' already open, it returns True. If the file is not open, it returns
    ' False. Otherwise, a run-time error occurs because there is
    ' some other problem accessing the file.
    
    Public Function IsFileOpen(filename As String) As Boolean
        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



  • di Totum (utente non iscritto) data: 15/10/2014 00:09:27

    Si intendo proprio questo, la chiusura di tutti i file aperti tranne il file dal quale lancio la macro.
    Tra l'altro grazie della macro.



  • di lepat (utente non iscritto) data: 15/10/2014 08:45:38

    prova così
     
    Sub ChiudiWbAperti()
    Application.ScreenUpdating = False
    For Each wb In Application.Workbooks
      If wb.Name <> ThisWorkbook.Name Then wb.Close True
    Next
    End Sub



  • di Totum (utente non iscritto) data: 15/10/2014 12:49:40

    Funziona, grazie !