View Single Post
Old 04-07-2009, 11:25   #1
john_revelator
Senior Member
 
L'Avatar di john_revelator
 
Iscritto dal: Jul 2007
Messaggi: 1092
[vba excel] verificare se un file è aperto o meno

Salve a tutti. Sono ancora qui a rompere.
Tramite word dovrò importare in excel dei dati ma prima devo verificare che il file di excel sia aperto o meno.

Il codice che ho scritto finora è questo. Anche se il file è chiuso mi dice che in realtà è già aperto. Potete darmi un consiglio? Grazie ancora.

Codice:
Sub cicla()

On Error GoTo apriFile:

Dim strPath As String
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

strPath = "C:\mio_path\2009-06-19.xls"

xlApp.Visible = True


If Dir(strPath) <> "" Then
    'MsgBox "Il file esiste"
        
        If Not Workbooks(strPath) Is Nothing Then
            MsgBox "file aperto"
        Else
            MsgBox "file chiuso"
        End If

Workbooks(strPath).Activate

Else
    MsgBox "Il file non esiste"
    Exit Sub
End If


apriFile:
If Err.Number = 9 Then
    xlApp.Workbooks.Open FileName:=strPath
    Resume
End If

End Sub
john_revelator è offline   Rispondi citando il messaggio o parte di esso