2009-08-19 2 views
4

VBA가 잠겨 있는지보고 할 수있는 wkb.VBProject.Protection을 비롯하여 다양한 "준수 기준"에 대한 Excel 파일을 문서화하는보고 도구를 작성 중입니다.VBA에서 Excel 통합 문서의 VBA 존재 여부를 테스트하는 방법은 무엇입니까?

그러나 통합 문서에 프로젝트가 있다면 어떻게 찾을 수 있습니까?

내가 나에게 모듈 + 클래스 모듈 + 형태의 수를 줄 것이다,하지만 난 여전히 시트 뒤에 몇 가지 코드를 가질 수있는

wkb.VBProject.VBComponents.Count - wkb.Worksheets.Count - 1 '(for the workbook) 

을 계산합니다.

Access에서 frm.HasModule과 같은 방법으로 통합 문서에 VBA 코드가 있는지 확인할 수 있습니까?

답변

5

다음은 프로젝트의 총 라인 수를 계산하기 위해 다음을 사용했습니다. ThisWorkbook, 코드 모듈, 클래스 모듈 및 양식에서 코드를 선택합니다.

Private Sub countCodeLines() 
    Dim obj As Object 
    Dim VBALineCount As Long 
    For Each obj In ThisWorkbook.VBProject.VBComponents 
     VBALineCount = VBALineCount + obj.CodeModule.CountOfLines 
    Next obj 
    Debug.Print VBALineCount 
End Sub 

참고 그러나 통합 문서가있는 경우 Option Explicit 다음 강제하는 것이이 두 개체 당 라인 (Option Explicit과 줄 바꿈)로 계산됩니다. 이 사실을 알고 있고 다른 프로젝트에서 LOC를 확인하는 경우 객체 수를 계산하여 두 배로하고 VBALineCount이이 수를 초과하지 않는지 테스트 할 수 있습니다.

+0

감사 Lunatik는, 그 완벽 해. –

4

Lunatik의 힌트 후, 여기 내 마지막 기능은 (누구 것이 도움이 될 수 있습니다)입니다 :

 
Function fTest4Code(wkb As Workbook) As Boolean 
    'returns true if wkb contains VBA code, false otherwise 
    Dim obj As Object 
    Dim iCount As Integer 
    For Each obj In wkb.VBProject.VBComponents 
     With obj.CodeModule 
      '# lines - # declaration lines > 2 means we do have code 
      iCount = iCount + ((.CountOfLines - .CountOfDeclarationLines) > 2) 
     End With 
     If iCount 0 Then Exit For 'stop when 1st found 
    Next obj 
    fTest4Code = CBool(iCount) 
End Function 
7

엑셀 2007 이상은 문의 할 수 있습니다 ".HasVBProject"라는 새 통합 문서 속성이 있습니다.

Excel 2003 및 이전 버전에서는 통합 문서의 VBComponents 중 하나의 CodeModule에있는 코드 줄에 대한 위의 솔루션 테스트가 적절합니다.

".CountOfDeclarationLines"를 통해 얻은 코드 모듈의 선언 구역에있는 코드 줄은 Excel에서 "매크로 코드"로 간주되므로 매크로에 저장해야하기 때문에 ".CountOfLines"속성을 모두 테스트해야합니다 사용 가능한 형식.

Public Function HasVBProject(Optional pWorkbook As Workbook) As Boolean 
' 
' Checks if the workbook contains a VBProject. 
' 
On Error Resume Next 
    Dim wWorkbook As Workbook 
    Dim wVBComponent As VBIDE.VBComponent ' As Object if used with Late Binding 

    ' Default. 
    ' 
    HasVBProject = False 

    ' Use a specific workbook if specified, otherwise use current. 
    ' 
    If pWorkbook Is Nothing _ 
    Then Set wWorkbook = ActiveWorkbook _ 
    Else Set wWorkbook = pWorkbook 
    If wWorkbook Is Nothing Then GoTo EndFunction 

    If (VBA.CInt(Application.Version) >= 12) _ 
    Then 
     ' The next method only works for Excel 2007+ 
     ' 
     HasVBProject = wWorkbook.HasVBProject 
    Else 
     ' Signs the workbook has a VBProject is code in any of the VBComponents that make up this workbook. 
     ' 
     For Each wVBComponent In wWorkbook.VBProject.VBComponents 
      If (wVBComponent.CodeModule.CountOfLines > 0) _ 
      Then 
       ' Found a sign of programmer's activity. Mark and quit. 
       ' 
       HasVBProject = True: Exit For 
      End If 
     Next wVBComponent 
    End If 

EndFunction: 
    Set wVBComponent = Nothing 
    Set wWorkbook = Nothing 
End Function 

네덜란드

관련 문제