|
|
|
|
Strumenti |
04-07-2009, 11:25 | #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 |
04-07-2009, 12:38 | #2 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3208
|
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 !" |
05-07-2009, 17:30 | #3 |
Senior Member
Iscritto dal: Jul 2007
Messaggi: 1092
|
Grazie infinite per la "millantesima" volta.
|
05-07-2009, 20:09 | #4 |
Senior Member
Iscritto dal: Dec 2004
Messaggi: 3208
|
|
21-06-2020, 16:51 | #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 |
|
24-06-2020, 11:40 | #6 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3594
|
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.
|
01-07-2020, 15:46 | #7 | |
Senior Member
Iscritto dal: Jul 2008
Città: Roma
Messaggi: 541
|
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: 11:03.