|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
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
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3210
|
Mmm, quanto codice...
Meglio così ? Codice:
Private Function FileAperto(pathNomeFile As String) As Boolean
On Error Resume Next
Open pathNomeFile For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
FileAperto = True
Err.Clear
End If
End Function
Codice:
Dim nomeFile As String
nomeFile = "C:\Test.xls"
If FileAperto(nomeFile) = True Then MsgBox "File già aperto !"
|
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jul 2007
Messaggi: 1092
|
Grazie infinite per la "millantesima" volta.
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3210
|
|
|
|
|
|
|
#5 | |
|
Junior Member
Iscritto dal: Jun 2020
Messaggi: 1
|
quasi perfetto...
Quote:
se lo uso per verificare se è già aperto un file che è in SOLA LETTURA mi dice (giustamente visto che prova bloccarlo anche in scrittura e non ci riesce) che è aperto anche se non lo è... c'è un modo di risolvere questa particolarità? Grazie |
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3736
|
per tagliare la testa al toro con una sporca: io proverei a rinominarlo aggiungendo semplicemente un carattere, se l'SO non si arrabbia, il file non è aperto e lo si riporta al nome precedente.
|
|
|
|
|
|
#7 | |
|
Senior Member
Iscritto dal: Jul 2008
Città: Roma
Messaggi: 542
|
Quote:
http://forum.masterdrive.it/visual-b...tml#post377488 ma non hai creato nessun nuovo thread per avere la risposta ... |
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 22:29.




















