Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Aprire Workbook tramite VBA

1,592 views
Skip to first unread message

bizkit82

unread,
Apr 30, 2008, 9:22:00 AM4/30/08
to
Ciao a tutti,
ho un file in Excel che tramite una macro apre un'altro file Excel:
Workbooks.Open Percorso & Nome, ReadOnly:=True

per verificare alcuni dati, e poi richiuderlo senza salvarlo:
Workbooks(Nome).Close SaveChanges:=False

Il tutto in una frazione di secondo. In teoria.
Il problema è che il file da aprire in sola lettura contiene anch'esso delle
macro, che partono all'avvio tramite l'evento Workbook_Open().

Dato che lo apro e lo chiudo senza salvare, queste macro sono inutili, e
vorrei poter dirgli "aprimi il file senza attivarmi le macro" nella
istruzione Workbooks.Open ecc...

E' possibile?

Grazie mille

Ciao
bizkit82

Norman Jones

unread,
Apr 30, 2008, 10:02:14 AM4/30/08
to
Ciao |Bizkit82,

=============

E' possibile?
=============

Prova qualcosa del genere:

'===========>>
Public Sub Tester()
Dim WB As Workbook
Dim myFolder As String
Dim myDrive As String
Dim sSeparator As String
Dim sStr As String
Const sPath As String = _
"C:\Users\Norman\Documents" '<<=== da CAMBIARE
Const sFile As String = "Pippo.xls" '<<=== da CAMBIARE

sSeparator = Application.PathSeparator

If Right(sPath, 1) <> sSeparator Then
sStr = sPath & sSeparator & sFile
Else
sStr = sPath & sFile
End If

myFolder = CurDir
myDrive = CurDir

On Error GoTo XIT
With Application
.ScreenUpdating = False
.EnableEvents = False
Set WB = Workbooks.Open( _
Filename:=sStr, _
ReadOnly:=True)

' Tuo codice '<<============
XIT:
WB.Close SaveChanges:=False
.ScreenUpdating = True
.EnableEvents = True
End With
ChDrive sPath
ChDir sPath
End Sub
'<<===========

---
Regards.
Norman

bizkit82

unread,
Apr 30, 2008, 10:36:04 AM4/30/08
to
Thank you! Funziona alla grande!!

Ciao
bizkit82

Norman Jones

unread,
Apr 30, 2008, 10:59:39 AM4/30/08
to
Ciao Bizkit82,

=============

Thank you! Funziona alla grande!!
=============

Infatti, il codice era inteso come:

'===========>>
Public Sub Tester()
Dim WB As Workbook
Dim myFolder As String
Dim myDrive As String
Dim sSeparator As String
Dim sStr As String
Const sPath As String = _
"C:\Users\Norman\Documents" '<<=== da CAMBIARE
Const sFile As String = "Pippo.xls" '<<=== da CAMBIARE

sSeparator = Application.PathSeparator

If Right(sPath, 1) <> sSeparator Then
sStr = sPath & sSeparator & sFile
Else
sStr = sPath & sFile
End If

myFolder = CurDir
myDrive = CurDir

ChDrive sPath
ChDir sPath



On Error GoTo XIT
With Application
.ScreenUpdating = False
.EnableEvents = False
Set WB = Workbooks.Open( _
Filename:=sStr, _
ReadOnly:=True)

' Tuo codice '<<============
XIT:
WB.Close SaveChanges:=False
.ScreenUpdating = True
.EnableEvents = True
End With

ChDrive myFolder
ChDir myFolder

bizkit82

unread,
Apr 30, 2008, 11:28:01 AM4/30/08
to
Ehm... ad essere sincero io non ho copiato il tuo codice... (non volermi male).
Mi bastava sapere la parola magica: Application.EnableEvents = False

Per questo ho detto che funziona alla grande.

Ciao e grazie ancora
bizkit82

Norman Jones

unread,
Apr 30, 2008, 12:37:11 PM4/30/08
to
Ciao Bizkit82,

=============


Ehm... ad essere sincero io non ho copiato il tuo codice... (non volermi
male).
Mi bastava sapere la parola magica: Application.EnableEvents = False

Per questo ho detto che funziona alla grande.

=============

Per quanto riguarda l'istruzione:

Application.EnableEvents = False

E' necessario assicurati che lo
settaggio sia riattivato - anche
nel caso di un errore non previsto
nel codice. Quindi, nel mio codice
di esempio, ho preceduto
l'istruzione con:

On Error GoTo XIT

e, succesivamente:

XIT:
[...]
.EnableEvents = True
[...]
End Sub.


---
Regards.
Norman

bizkit82

unread,
May 2, 2008, 2:36:01 AM5/2/08
to
E' vero, Norman. Non ci avevo pensato.
Grazie 1000.

Ciao
bizkit82

0 new messages