Depurar

28/04/2007 - 17:40 por JAVIER | Informe spam
Hola amigos me gustaria saber la causa del porque cuando la macro llega a la
macro protección ahi me detiene?

Thisworkbook:
Option Explicit
Private Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" (ByVal lpbuffer As String, nSize As Long) _
As Long

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpbuffer As
String, _
nSize As Long) As Long
Public a, UNAME, msj

Private Sub usuariowin()

Dim lpBuff As String * 25
Dim ret As Long, USERNAME As String
ret = GetUserName(lpBuff, 25)
USERNAME = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
UNAME = USERNAME

End Sub

'macro workbook_open
'Hector te recomiendo que sustituyas el usuario xxx por el usuario tuyo de
windows para que aprecies donde se detiene la macro
Private Sub o()
Application.ScreenUpdating = False
QuitarProtección
Windows(ThisWorkbook.Name).Visible = True
ActiveWindow.DisplayWorkbookTabs = False
If Not InStr("Libro1.xls,libro2.xls", ActiveWorkbook.Name) > 0 Then
OcultarHojas
ActiveWorkbook.Close False
End If
usuariowin
'If Date > DateSerial(2006, 6, 24) Then
'ActiveWorkbook.Close False
'End If

'Si no es master cargar el perfil de usuario final
If InStr("Master", UNAME) > 0 Then

MostrarHojas

Else ' perfil de usuario final
Application.ScreenUpdating = False
Application.EditDirectlyInCell = False
Application.DisplayAlerts = False
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = False
Application.DisplayScrollBars = True
'Application.DisplayStatusBar = False
ActiveWindow.DisplayHeadings = False
Application.CommandBars("Drawing").Visible = False
Application.CommandBars("Standard").Visible = False
Application.CommandBars("Formatting").Visible = False
'Application.CommandBars("Reviewing").Visible = False
Application.ScreenUpdating = True
Res 'restringe el menu principal
OcultarHojas
End If

Permisos
'Cuando se valide el permiso inicia en la hoja menu
Sheets("Hoja1").Select

Application.ScreenUpdating = True

End Sub

'Macro Before_closed
Private Sub W()
'(Cancel As Boolean)
Application.ScreenUpdating = False
OcultarHojas

Application.ScreenUpdating = False
Application.EditDirectlyInCell = True
Application.DisplayAlerts = True
Application.DisplayFormulaBar = True
Application.DisplayScrollBars = True
Application.DisplayStatusBar = True
ActiveWindow.DisplayHeadings = True
ActiveWindow.DisplayWorkbookTabs = True
'Application.CommandBars("Drawing").Visible = False
Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
Norm

protegerlibro

ThisWorkbook.Save

End Sub
'acceso

Private Sub Permisos()
usuariowin
If InStr("Master,abc,xxx", UNAME) > 0 Then
'dejarlo que trabaje con el archivo
Sheets("Hoja1").Visible = xlSheetVisible
Else
On Error Resume Next
Application.DisplayAlerts = False
ThisWorkbook.ChangeFileAccess xlReadOnly
Kill ThisWorkbook.FullName
ThisWorkbook.Close False

MsgBox "Usuario de windows no valido", vbOKOnly
End If
'acceso con herramientas especiales de excel
If InStr("Master,abc,xxx", UNAME) > 0 Then

Application.CommandBars("Standard").Visible = True
Application.CommandBars("Formatting").Visible = True
'Norm
End If
End Sub
Private Sub MostrarHojas()
usuariowin
If UNAME = "master" Then

On Error Resume Next
ActiveWindow.DisplayWorkbookTabs = True
Sheets("Hoja2").Visible = xlSheetVisible
Sheets("Hoja3").Visible = xlSheetVisible
Else
OcultarHojas
End If

End Sub

_____________________________
Modulo 1:
Public Sub Norm()
CommandBars("Worksheet Menu Bar").Reset
End Sub
'Restringir comandos del menu general
Public Sub Res()
For Each i In CommandBars("Worksheet Menu Bar").Controls
i.Delete
Next
End Sub
Sub protegerlibro()

Windows(ThisWorkbook.Name).Visible = False ' Primero lo oucltamos, luego
... '

'Application.ScreenUpdating = False ' congelamos la actualización de la
pantalla '
'entrando a ThisWorkbook.protect me para la macro
'A que se debera?
ThisWorkbook.Protect _
Password:=Javier, _
Structure:=True, _
Windows:=True '

End Sub

Sub QuitarProtección() ' Con esto le quitamos la contraseña al libro '
ThisWorkbook.Unprotect Password:=Javier ' Quitamos la protección y ... '
Windows(ThisWorkbook.Name).Visible = True ' ... mostramos la ventana (que
está oculta) '
ThisWorkbook.Sheets(1).Activate ' Selecciona siempre la primer hoja del
libro '
End Sub
Sub OcultarHojas()
On Error Resume Next
Sheets("Hoja2").Visible = xlSheetVeryHidden
Sheets("Hoja3").Visible = xlSheetVeryHidden
End Sub
 

Leer las respuestas

#1 Héctor Miguel
29/04/2007 - 01:17 | Informe spam
hola, Javier !

... me gustaria saber la causa del porque cuando... llega a la macro proteccion ahi me detiene?



1) en las lineas donde re/quitas/pones la proteccion al libro con .Unprotect y .Protect
-> le estas 'pasando' al codigo el argumento 'Password' con una 'variable' [de nombre Javier]
-> pero no se aprecia en el codigo que expones como/cuando/donde/... la inicializas y la estableces -?-
-> p.e. Dim Javier As String y mas adelante... Javier = "mi clave SeCrEtA"
-> entonces... cuando al codigo 'lee'... -> ThisWorkbook.Unprotect Password:=Javier
-> 'que' trae 'Javier' como contenido de la clave ??? coincide con la clave 'real' de la proteccion del libro ???

2) la 'forma' en que usas la funcion InStr(...), al omitir el 'tipo' de comparacion [ultimo argumento]...
podria estar haciendo una comparacion 'binaria' que requiera coincidencia exacta de los caracteres [may/min/usculas] -?-
a) en el evento open tienes... -> If Not InStr("Libro1.xls,libro2.xls", ActiveWorkbook.Name) > 0 Then
b) en la macro Sub Permisos... -> If InStr("Master,abc,xxx", UNAME) > 0 Then
-> [p.e.] que pasa si la variable UNAME devuelve "MASTER" y en la cadena de busqueda solo compara con "Master" -?-

comentas [si encuentras] algun detalle adicional ?
saludos,
hector.

Preguntas similares