Live-Forum - Die aktuellen Beiträge
Datum
Titel
24.04.2024 19:29:30
24.04.2024 18:49:56
24.04.2024 17:19:09
Anzeige
Archiv - Navigation
1236to1240
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Kontextmenü für Textboxen
KLE
Hi, habe mal wieder eine Problem mit einer laufenden Datei.
Ich nutze für eine Userform, welche verschieden Textboxen etc. enthält den unten aufgeführten Code, um den Textboxen ein "Kontext-Menü" zu geben, damit der User einfacher mit Kopieren, Einfügen, Ausschneidern etc. arbeiten kann.
Bis gestern lief alles super - doch heute die Datei gestartet und jetzt erhalte ich eine Fehlermeldung:
Fehler: -2147467259(80004005):
Die Methode "ShowPopup" für das Objekt 'CommandBar' ist fehlgeschlagen.
Hat jemand eine Idee? Warum läuft es Wochenlang und ann aufeinmal nich mehr ?!?
Gruß und Danke
Kay
Modul1:
Option Explicit
Private WithEvents e_TextBox As MSForms.TextBox
Private m_objForm As Object
Private m_objCBar As Office.CommandBar
Private Sub Class_Terminate()
Set m_objCBar = Nothing
Set e_TextBox = Nothing
Set m_objForm = Nothing
End Sub

Public Property Set Form(ByVal objForm As Object)
Set m_objForm = objForm
End Property
Public Property Set TextBox(ByVal objTextBox As MSForms.TextBox)
Set e_TextBox = objTextBox
End Property
Public Property Set CBar(ByVal objCBar As Office.CommandBar)
Set m_objCBar = objCBar
End Property
Private Sub e_TextBox_MouseUp(ByVal Button As Integer, ByVal _
Shift As Integer, ByVal x As Single, ByVal y As Single)
On Error Resume Next
If Button = 2 And Shift = 0 Then
Set m_objForm.ActiveTextBox = e_TextBox
With m_objCBar
.Controls(1).Enabled = CBool(e_TextBox.SelLength  0)
.Controls(2).Enabled = CBool(e_TextBox.SelLength  0)
.Controls(3).Enabled = e_TextBox.CanPaste
.Controls(4).Enabled = CBool(Len(e_TextBox.Text)  0)
.Controls(5).Enabled = CBool(Len(e_TextBox.Text)  0)
End With
m_objCBar.ShowPopup
End If
End Sub

& Modul2:
Option Explicit
Private mcol_TextBoxes As Collection
Private m_objForm As Object
Private m_objCBar As Office.CommandBar
Private WithEvents e_cbbCut As Office.CommandBarButton
Private WithEvents e_cbbCopy As Office.CommandBarButton
Private WithEvents e_cbbPaste As Office.CommandBarButton
Private WithEvents e_cbbDelete As Office.CommandBarButton
Private WithEvents e_cbbSelect As Office.CommandBarButton
Private Sub Class_Initialize()
Set mcol_TextBoxes = New Collection
End Sub
Private Sub Class_Terminate()
Set mcol_TextBoxes = Nothing
Set m_objForm = Nothing
Set e_cbbCut = Nothing
Set e_cbbCopy = Nothing
Set e_cbbPaste = Nothing
Set e_cbbDelete = Nothing
Set e_cbbSelect = Nothing
If (Not m_objCBar Is Nothing) Then
m_objCBar.Delete
Set m_objCBar = Nothing
End If
End Sub
Public Function CreateCommandBar() As Boolean
On Error GoTo err_CreateCBar
Set m_objCBar = Application.CommandBars.Add( _
Position:=msoBarPopup, Temporary:=True)
Set e_cbbCut = m_objCBar.Controls.Add(Type:=msoControlButton)
With e_cbbCut
.Style = msoButtonIconAndCaption
.Caption = "Ausschneiden"
.FaceId = 21
End With
Set e_cbbCopy = m_objCBar.Controls.Add(Type:=msoControlButton)
With e_cbbCopy
.Style = msoButtonIconAndCaption
.Caption = "Kopieren"
.FaceId = 19
End With
Set e_cbbPaste = m_objCBar.Controls.Add(Type:=msoControlButton)
With e_cbbPaste
.Style = msoButtonIconAndCaption
.Caption = "Einfügen"
.FaceId = 22
End With
Set e_cbbDelete = m_objCBar.Controls.Add(Type:=msoControlButton)
With e_cbbDelete
.BeginGroup = True
.Style = msoButtonIconAndCaption
.Caption = "Alles löschen"
End With
Set e_cbbSelect = m_objCBar.Controls.Add(Type:=msoControlButton)
With e_cbbSelect
.Style = msoButtonIconAndCaption
.Caption = "Alles markieren"
End With
CreateCommandBar = True
exit_Func:
On Error GoTo 0
Exit Function
err_CreateCBar:
MsgBox "Fehler: " & Err.Number & vbCrLf & _
Err.Description, vbOKOnly + vbCritical
If (Not m_objCBar Is Nothing) Then
m_objCBar.Delete
Set m_objCBar = Nothing
End If
Resume exit_Func
End Function

Public Property Set Form(ByVal objForm As Object)
Set m_objForm = objForm
End Property
Public Function Add(ByVal objTextBox As MSForms.TextBox) _
As CTextBoxPopUp
Dim objCTextBox As CTextBoxPopUp
On Error GoTo err_Add
Set objCTextBox = New CTextBoxPopUp
With objCTextBox
Set .Form = m_objForm
Set .TextBox = objTextBox
Set .CBar = m_objCBar
End With
mcol_TextBoxes.Add objCTextBox
Set Add = objCTextBox
Set objCTextBox = Nothing
exit_Func:
On Error GoTo 0
Exit Function
err_Add:
MsgBox "Fehler: " & Err.Number & vbCrLf & _
Err.Description, vbOKOnly + vbCritical
Resume exit_Func
End Function

Public Property Get Item(ByVal Index As Variant) As CTextBoxPopUp
Set Item = mcol_TextBoxes.Item(Index)
End Property
Public Property Get Count() As Long
Count = mcol_TextBoxes.Count
End Property
Public Sub Remove(ByVal Index As Variant)
mcol_TextBoxes.Remove Index
End Sub
Private Sub e_cbbCut_Click(ByVal Ctrl As Office. _
CommandBarButton, CancelDefault As Boolean)
m_objForm.ActiveTextBox.Cut
End Sub
Private Sub e_cbbCopy_Click(ByVal Ctrl As Office. _
CommandBarButton, CancelDefault As Boolean)
m_objForm.ActiveTextBox.Copy
End Sub
Private Sub e_cbbPaste_Click(ByVal Ctrl As Office. _
CommandBarButton, CancelDefault As Boolean)
m_objForm.ActiveTextBox.Paste
End Sub
Private Sub e_cbbDelete_Click(ByVal Ctrl As Office. _
CommandBarButton, CancelDefault As Boolean)
m_objForm.ActiveTextBox.Text = vbNullString
End Sub
Private Sub e_cbbSelect_Click(ByVal Ctrl As Office. _
CommandBarButton, CancelDefault As Boolean)
With m_objForm.ActiveTextBox
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: Kontextmenü für Textboxen
18.11.2011 06:45:37
Luschi
Hallo Kay,
so wie Du das beschreibst und in welchen Modulen der Vba-Code steht (Modul1, Modul2) kann ich mir garnicht vorstellen, daß es je funktioniert hat. Der Vba-Code gehört in in das Klassenmodul des Formulars und in ein spezielles Klassenmodul.
Ein funktionierendes Beispiel findest Du hier:
http://www.vb-fun.de/cgi-bin/loadframe.pl?ID=vb/tipps/tip0434.shtml
Vielleicht hast Du auch nur den erforderlichen Vba-Verweis 'Microsoft Office x.0 Object Library' entfernt.
Gruß von Luschi
aus klein-Paris
Anzeige
AW: Kontextmenü für Textboxen
21.11.2011 13:30:32
KLE
Hallo,
Sorry, kam nicht früher dazu Dir zu antworten. Nun, ich habe da einen Fehler gemacht - es steht bei mir auch im Klassenmodul und nicht Modul1/2...
Habe link von Dir genauer angeschaut und auch bei mir den Code verglichen. Scheint alles zu passen. Alles da wo es sein soll und auch der Inhalt ist identisch.
Nach einem erneuten Start der Datei stand das Kontextmenü heut wieder zur Verfügung...
Mal schauen wie lange ;o)
Vermutlich war wirklich vielleicht der Verweis nicht geladen oder so...
Danke aber für Deine Hifle!
Gruß nach klein-Paris
Kay
AW: Problem tauchte wieder auf...
22.11.2011 09:44:49
KLE
Hallo,
habe nun heut erneut die Datei gestartet und es kam wieder zu folgender Fehlermeldung:
Userbild
Wer kann mir hier helfen ? Denn scheinbar geht es mal und dann wieder nicht ?!?
Dei Verweise sind alle vorhanden und der Code ist dem vom Link von Luschi übernommen...
Gruß und Danke für jeden Hinweis!
Kay
Anzeige

300 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige