AW: VBA - Formularnamen einer Datei auslesen
21.07.2017 11:31:32
fcs
Hallo Fujin,
es geht, aber du musst ggf. die Makro-Schutzeinstellungen unter Optionen anpasen.
LG
Franz
Sub aaTest_VBA_omponents()
Dim varVBA_Components, strMsgText As String, intC As Integer, intCount As Integer
varVBA_Components = fncVBA_Components(wkb:=ActiveWorkbook, strMsg:=strMsgText, bolWkbTabs:= _
False)
If IsArray(varVBA_Components) Then
intCount = 0
' alle gefundenne Module anzeigen
MsgBox strMsgText, vbOKOnly, "VBA-Module in Datei " & ActiveWorkbook.Name
'Ergebnisliste auf bestimmte Modul-Namen prüfen
For intC = LBound(varVBA_Components, 1) To UBound(varVBA_Components, 1)
Select Case varVBA_Components(intC, LBound(varVBA_Components, 2))
Case "UserForm1", "VBA_Code"
intCount = intCount + 1
Case Else
'do nothing
End Select
Next
If intCount >= 2 Then
MsgBox """UserForm1"" und ""VBA_Code"" in Datei vorhanden"
Else
MsgBox """UserForm1"" und ""VBA_Code"" nicht beide vorhanden"
End If
Else
MsgBox strMsgText, vbOKOnly, "VBA-Module in Datei " & ActiveWorkbook.Name
End If
End Sub
'Function erstellt unter Excel Excel 2010
Function fncVBA_Components(wkb As Workbook, strMsg, _
Optional bolWkbTabs As Boolean = False) As Variant
'Gibt eine Liste der VBA_module in der Arbeitsmappe zurück
'Zur Ausführung der Prozedur muss:
'1. Im VBA-Editor unter Extras--> Verweise... _
der Verweis auf die _
Microsoft Visual Basic for Applications Extensibility x.x _
aktiviert werden
'2. In Excel unter Extras --> Optionen --> Sicherheit --> Makrosicherheit _
die Option "Zugriff auf das VB-Projekt vertrauen" aktiviert werden.
Dim myVBComponent As VBComponent, varFolderName
Dim intK As Integer, intCount As Integer
Dim arrList
On Error GoTo Fehler
If wkb Is Nothing Then Set wkb = ActiveWorkbook
With wkb.VBProject
'Komponenten zählen
intCount = 0
For Each myVBComponent In .VBComponents
With myVBComponent
Select Case .Type
Case 1: intCount = intCount + 1 'Allgemeines Modul
Case 2: intCount = intCount + 1 'Klassenmodul
Case 3: intCount = intCount + 1 'Userform
Case 100: If bolWkbTabs Then intCount = intCount + 1 'Tabelle oder _
DieseArbeitsmappe
End Select
End With
Next
If intCount > 0 Then
intK = 0
ReDim arrList(1 To intCount, 1 To 3)
For Each myVBComponent In .VBComponents
With myVBComponent
Select Case .Type
Case 1: intK = intK + 1 'Allgemeines Modul
arrList(intK, 1) = .Name: arrList(intK, 2) = .Type
arrList(intK, 3) = "Allgemeines Modul"
Case 2: intK = intK + 1 'Klassenmodul
arrList(intK, 1) = .Name: arrList(intK, 2) = .Type
arrList(intK, 3) = "Klassenmodul"
Case 3: intK = intK + 1 'Userform Modul
arrList(intK, 1) = .Name: arrList(intK, 2) = .Type
arrList(intK, 3) = "Userform"
Case 100 'Tabelle oder DieseArbeitsmappe
If bolWkbTabs Then
intK = intK + 1
arrList(intK, 1) = .Name: arrList(intK, 2) = .Type
arrList(intK, 3) = "DieseArbeitsmappe/Tabelle"
End If
End Select
If intK > 0 Then
strMsg = strMsg & IIf(strMsg = "", "", vbLf) & arrList(intK, 3) _
& " - " & arrList(intK, 1)
End If
End With
Next
fncVBA_Components = arrList
Else
strMsg = "Keine VBA-Componenten vorhanden"
fncVBA_Components = 0
End If
End With
Fehler:
With Err
If .Number 0 Then
Select Case .Number
Case 1004
MsgBox "Fehler: " & .Number & vbLf & .Description & vbLf _
& "Vor Start des Makros unter Optionen ""Sicherheit --> " _
& "Makrosicherheit"" Option " _
& """Zugriff auf das VB-Projekt vertrauen"" aktivieren!"
Case Else
MsgBox "Fehler: " & .Number & vbLf & .Description
End Select
End If
End With
End Function