Notizen und Clipboard

Direkt zum Seiteninhalt

Notizen und Clipboard

Tiggi
Veröffentlicht von Martin Küttel in VBA APP · 13 Juni 2022
Tags: vbaCodeNoteClipboard
Teil 1: Code zur Handhabung von Notizen
Oft möchte man per VBA eine Notiz zu einer Zelle eintragen oder die Notiz einer Zelle auslesen.
Z.B. Wozu der Termin ist, oder was hinter einer Bezeichnung steht, oder ein Hinweis wo man mehr Informationen findet.
Ihr kennt vieleicht noch andere praktische Anwendungen.

Variablen:
Dim strNotiz as String ' Der Text der als Notiz zu bearbeiten ist (könnte auch der Inhalt einer Textbox sein)
Dim lngZ as long 'Pointer auf Zelle (Zeile)
Dim lngCol as long 'Pointer auf Zelle (Spalte oder column)

strNotiz = TxtNotiz  'Textbox.Text in die Variable strNotiz eintragen

Public Sub Notiz_eintragen(lngZ As Long, lngCol As Long)
   With ActiveSheet  'Aktuelle Tabelle
       .Cells(lngZ, lngCol).ClearComments  'Notiz löschen
       If strNotiz = "" Then Exit Sub  'kein Text zum Eintragen
       If bolNotiz = True Then
           .Cells(lngZ, lngCol).AddComment strNotiz
           .Cells(lngZ, lngCol).Comment.Shape.TextFrame.AutoSize = True
       End If
   End With
   TxtNotiz = strNotiz
End Sub

Public Sub Notiz_Einlesen(lngZ As Long, lngCol As Long)
   With ActiveSheet
       If .Cells(lngZ, lngCol).Comment Is Nothing Then  'Keine Notiz zum Einlesen
           strNotiz = ""
           bolNotiz = False
       Else
           strNotiz = ActiveSheet.Cells(lngZ, lngCol).Comment.Text
           bolNotiz = True
       End If
   End With
End Sub
TxtNotiz = strNotiz ' Variable strNotiz zu Textbox.Text eintragen

Teil 2: Texaustausch mit Zwischenspeicher (Clipboard)
In vielen Anwendungen möchte man vorhandener Text via Zwischenspeicher in ein anderes Dokument verschieben.
Hier von Webseite zu Excel Textboxen oder umgekehrt.
Auch dazu bietet VBA eine Lösung die Ich Ihnen zur vorstellen möchte.
Das Beispiel benutzt zwei Tasten (cmdTxtToClipboard  &  cmdClipboardToTxt)
und eine Textbox die txtClipboard heisst.

Variablen:
Dim strTextClip as String ' Variable für Text von und zur Zwischenablage
strTextClip = txtClipboard.Text  'Textquelle ist hier die TextBox

Private Sub cmdTxtToClipboard_Click()
   'Text in Zwischenablage speichern
   On Error GoTo ErrorClipboard
   Dim myData As New DataObject
   myData.SetText strTextClip
   myData.PutInClipboard
           MsgBox "Gespeichert!", vbInformation + vbOKOnly, "Zwischenablage"
   Exit Sub 'Erledigt
ErrorClipboard:
           MsgBox "Keine Daten von StrTextClip!", vbInformation + vbOKOnly, "Zwischenablage"
End Sub

Private Sub cmdClipboardToTxt_Click()
   'Einlesen von der Zwischenablage
   Dim myData As New DataObject
   Set myData = New MSForms.DataObject
   On Error GoTo ErrorClipboard 'Wenn kein Text in Zwischenablage
   myData.GetFromClipboard
   strNotiz = myData.GetText(1)
   txtClipboard =strNotiz 'Text zu Textbox
           MsgBox "gelesen", vbInformation + vbOKOnly, "Zwischenablage"
   Exit Sub  'Erledigt
ErrorClipboard:
           MsgBox "Zwischenablage hat keine Daten!", vbInformation + vbOKOnly, "Zwischenablage"
End Sub



Zurück zum Seiteninhalt