模块的隐藏看起来好像好高深,其实原理和前面的清除VBAProject工程密码的原理是一样的,只需要改写PROJECT数据流。
不过需要注意的是,类模块不能隐藏,另外还必需要有一个可见的模块,实现代码:
代码语言:javascript复制'隐藏某个模块:在VBA工程窗口无法查看
'ModuleName 模块的名称
'Return 返回出错信息
Function HideModule(ModuleName As String) As String
Dim moduleIndex As Long
moduleIndex = GetModuleIndex(ModuleName)
If moduleIndex = -1 Then
HideModule = "CVBAProject: 在ModuleInfo_中没有找到模块[" & ModuleName & "]"
Exit Function
End If
If ModuleInfo_(moduleIndex).IType = ModuleTypeEnum.ClassModule Then
HideModule = "CVBAProject: 类模块不能隐藏"
Exit Function
End If
Dim ret As String
Dim b() As Byte
ret = cf.GetStream(PrePath & "PROJECT", b)
If VBA.Len(ret) Then
HideModule = ret
Exit Function
End If
Dim strSrc As String
strSrc = VBA.StrConv(b, vbUnicode)
If VBA.InStr(strSrc, "Module=" & ModuleName & vbNewLine) = 0 Then
HideModule = "CVBAProject: 模块[" & ModuleName & "]已隐藏"
Exit Function
End If
strSrc = VBA.Replace(strSrc, "Module=" & ModuleName & vbNewLine, "")
b = VBA.StrConv(strSrc, vbFromUnicode)
ret = cf.ReWriteStream(PrePath & "PROJECT", b)
If VBA.Len(ret) Then
HideModule = ret
Exit Function
End If
End Function
'取消隐藏某个模块
'ModuleName 模块的名称
'Return 返回出错信息
Function UnHideModule(ModuleName As String) As String
Dim moduleIndex As Long
moduleIndex = GetModuleIndex(ModuleName)
If moduleIndex = -1 Then
UnHideModule = "CVBAProject: 在ModuleInfo_中没有找到模块[" & ModuleName & "]"
Exit Function
End If
If ModuleInfo_(moduleIndex).IType = ModuleTypeEnum.ClassModule Then
UnHideModule = "CVBAProject: 类模块不能隐藏"
Exit Function
End If
Dim ret As String
Dim b() As Byte
ret = cf.GetStream(PrePath & "PROJECT", b)
If VBA.Len(ret) Then
UnHideModule = ret
Exit Function
End If
Dim strSrc As String
strSrc = VBA.StrConv(b, vbUnicode)
If VBA.InStr(strSrc, "Module=" & ModuleName & vbNewLine) Then
UnHideModule = "CVBAProject: 模块[" & ModuleName & "]未隐藏"
Exit Function
End If
'HelpFile="" 在这个前面添加 Module=moduleNameODOA
strSrc = VBA.Replace(strSrc, "HelpFile=""""", "Module=" & ModuleName & vbNewLine & "HelpFile=""""")
b = VBA.StrConv(strSrc, vbFromUnicode)
ret = cf.ReWriteStream(PrePath & "PROJECT", b)
If VBA.Len(ret) Then
UnHideModule = ret
Exit Function
End If
End Function