jueves, mayo 30, 2013

Romper password de Workbook protegido con contraseña en Excel

Agregar un módulo con el siguiente código a el workbook que queramos ejecutar y nos mostrará la password.

'Muestra el password del Workbook a desproteger y lo desprotege
Sub passwordBreaker()

Dim i As Integer, j As Integer, k As Integer
Dim l As Integer, m As Integer, n As Integer
Dim i1 As Integer, i2 As Integer, i3 As Integer
Dim i4 As Integer, i5 As Integer, i6 As Integer
On Error Resume Next
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
   For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
      For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
         For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
         ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If ActiveSheet.ProtectContents = False Then
MsgBox "El password es " & Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next

End Sub

lunes, enero 21, 2013

Animaciones en VBA


Como crear una animación en Excel,Access, etc:
Se puede usar application.wait para realizar la espera, pero esto nos limita a un mínimo de un segundo entre imagenes por lo que la animación no será creible.
application.Wait (Now + TimeValue("0:00:01"))

Mejor, se puede usar la clase Timer ya que cuenta el tiempo en milisegundos y se puede crear un efecto mas fluido.
Aquí mi código que redimensiona un formulario con un efecto deslizamiento.

Option Explicit

Private Sub showAdminOptions()
   Dim x As Single
   Dim i As Integer
   If (Me.Height = 267) Then ' Is hidden
      For i = 1 To 9
         Me.Height = Me.Height + i
         x = Timer
         While Timer - x < 0.05
            DoEvents
         Wend
      Next i
   End If
End Sub

Private Sub hideAdminOptions()
   Dim x As Single
   Dim i As Integer
   If (Me.Height = 312) Then ' Is shown
      For i = 1 To 9
         Me.Height = Me.Height - i
         x = Timer
         While Timer - x < 0.05
            DoEvents
         Wend
      Next i
   End If
End Sub


viernes, enero 18, 2013

Usar logs en VBA


Si queremos crear un log en archivo de texto al estilo log4j podemos hacerlo con el siguiente procedimiento. El archivo se creará en el mismo directorio donde se encuentre nuestro archivo y tendrá el mismo nombre. Podemos abrirlo con tail si tenemos cygwin instalado. Si no tenemos o no queremos instalar cygwin se puede usar alguna de las muchas aplicaciones gratuitas que existen para hacer esto, como por ejemplo mTail.
Option Explicit

Sub log(LogMessage As String)

   Dim LogFileName As String
   Dim FileNum As Integer
   LogFileName = ThisWorkbook.Path & "\" & ThisWorkbook.Name & ".log"
   FileNum = FreeFile ' next file number
   Open LogFileName For Append As #FileNum ' creates the file if it doesn't exist
   Print #FileNum, Now & " - " & LogMessage ' write information at the end of the text file
   Close #FileNum ' close the file
   DoEvents
End Sub

Funciona en Office 97/2000/2003/2007/2010