OKB-000256 | Alle unbenannten Steuerelemente benennen

Betrifft: ab orgAnice 2008

Frage: Wie kann ich im VBA alle in einem Tabellenlayout vorhandene Steuerelemente benennen?

Lösung:

Public Sub NameAllControls()

' Benennt alle unbenannten Controls in den Tabellenlayouts
Dim i As Long
Dim k As Long
Dim l As Long: l = 0
Dim m As Long

On Error Resume Next

' Alle Tabellenlayouts durchgehen

For i = 0 To Application.TableLayouts.count - 1
	With Application.TableLayouts(i)
		Debug.Print "TableLayout " & .Name
   		' Alle Steuerelemente durchgehen
   		For k = 0 To .FormControls.count - 1
      			With .FormControls(k)
      			' Wenn das Steuerelement unbenannt ist, dann Nach Typ entsprechend benennen
         			If Trim(.Name) = "" Then
            				.Name = NameControl(.Type, l)
            				Debug.Print " Control named " & .Name
            				l = l + 1
         			End If
         			' Unterelemente von Registern durchgehen
         			If .Type = ORGDATA_CONTROL_TABSTRIP Then
            				For m = 0 To .SubForms.count - 1
              					With .SubForms(m)
               						' Wenn das Steuerelement unbenannt ist, dann Nach Typ entsprechend benennen
                  					If Trim(.Name) = "" Then
                     						.Name = NameControl(.Type, l)
                     						Debug.Print " Control named " & .Name
                     						l = l + 1
                  					End If
               					End With
            				Next m
         			End If
      			End With
		Next k
	End With
	Debug.Print ""
Next i
End Sub

Private Function NameControl(psType As OrgDataControlTypesEnum, plNumber As Long) As String
' Benennt ein einzelnes Steuerelement
Const csStaticText = "txt"
Const csLabel = "lbl"
Const csLabel3D = "lbl"
Const csDynamicText = "txt"
Const csFrame = "fra"
Const csTextBox = "txt"
Const csMultiLineTextBox = "txt"
Const csPopUpTextBox = "txt"
Const csEuro = "eur"
Const csCheckBox = "chk"
Const csDate = "dtp"
Const csTime = "dtp"
Const csDateAndTime = "dtp"
Const csLookUpList = "cbo"
Const csLookUpEdit = "cbo"
Const csLookUpUsers = "cbo"
Const csRadioButtons = "opt"
Const csDocument = "doc"
Const csCommandButton = "cmd"
Const csActiveX = "act"
Const csTabStrip = "tab"
Const csLookUpPermissions = "cbo"

On Error GoTo ErrHandler

Select Case psType
	Case ORGDATA_CONTROL_STATICTEXT
		NameControl = csStaticText & plNumber
	Case ORGDATA_CONTROL_LABEL
		NameControl = csLabel & plNumber
	Case ORGDATA_CONTROL_LABEL3D
		NameControl = csLabel3D & plNumber
	Case ORGDATA_CONTROL_DYNAMICTEXT
		NameControl = csDynamicText & plNumber
	Case ORGDATA_CONTROL_FRAME
		NameControl = csFrame & plNumber
	Case ORGDATA_CONTROL_TEXTBOX
		NameControl = csTextBox & plNumber
	Case ORGDATA_CONTROL_MULTILINETEXTBOX
		NameControl = csMultiLineTextBox & plNumber
	Case ORGDATA_CONTROL_POPUPTEXTBOX
		NameControl = csPopUpTextBox & plNumber
	Case ORGDATA_CONTROL_EURO
		NameControl = csEuro & plNumber
	Case ORGDATA_CONTROL_CHECKBOX
		NameControl = csCheckBox & plNumber
	Case ORGDATA_CONTROL_DATE
		NameControl = csDate & plNumber
	Case ORGDATA_CONTROL_TIME
		NameControl = csTime & plNumber
	Case ORGDATA_CONTROL_DATEANDTIME
		NameControl = csDateAndTime & plNumber
	Case ORGDATA_CONTROL_LOOKUPLIST
		NameControl = csLookUpList & plNumber
	Case ORGDATA_CONTROL_LOOKUPEDIT
		NameControl = csLookUpEdit & plNumber
	Case ORGDATA_CONTROL_LOOKUPUSERS
		NameControl = csLookUpUsers & plNumber
	Case ORGDATA_CONTROL_RADIOBUTTONS
		NameControl = csRadioButtons & plNumber
	Case ORGDATA_CONTROL_DOCUMENT
		NameControl = csDocument & plNumber
	Case ORGDATA_CONTROL_COMMANDBUTTON
		NameControl = csCommandButton & plNumber
	Case ORGDATA_CONTROL_ACTIVEX
		NameControl = csActiveX & plNumber
	Case ORGDATA_CONTROL_TABSTRIP
		NameControl = csTabStrip & plNumber
	Case ORGDATA_CONTROL_LOOKUPPERMISSIONS
		NameControl = csLookUpPermissions & plNumber
End Select
Exit Function
ErrHandler:
Err.Raise Err.Number, csErrSource & "NameControl"
End Function
Beitrag bewerten