Méthodes GetFormat, GetText, SetText – Exemple

L’exemple suivant utilise les méthodes GetFormat, GetText et SetText pour transférer du texte entre un DataObject et le Presse-papiers.

L’utilisateur tape du texte dans une zone de texte , puis peut le transférer vers un DataObject dans un format de texte standard en cliquant sur CommandButton1.

Cliquer sur CommandButton2 extrait le texte de DataObject.

Cliquer sur CommandButton3 copie le texte de TextBox1 dans DataObject dans un format personnalisé.

Cliquer sur CommandButton4 extrait le texte de DataObject dans un format personnalisé.

Pour utiliser cet exemple, copiez le code de l’exemple dans la partie Déclarations d’un formulaire. Vérifiez que le formulaire contient :

  • un contrôle TextBox nommé TextBox1 ;
  • Quatre contrôles CommandButton nommés CommandButton1 à CommandButton4.
  • Une étiquette nommée Label1.
Dim MyDataObject As DataObject 
 
Private Sub CommandButton1_Click() 
'Put standard format on Clipboard 
 If TextBox1.TextLength > 0 Then 
 Set MyDataObject = New DataObject 
 MyDataObject.SetText TextBox1.Text 
 Label1.Caption = "Put on D.O." 
 CommandButton2.Enabled = True 
 CommandButton4.Enabled = False 
 End If 
End Sub 
 
Private Sub CommandButton2_Click() 
'Get standard format from Clipboard 
 If MyDataObject.GetFormat(1) = True Then 
 Label1.Caption = "Std format - " _ 
 & MyDataObject.GetText(1) 
 End If 
End Sub 
 
Private Sub CommandButton3_Click() 
'Put custom format on Clipboard 
 If TextBox1.TextLength > 0 Then 
 Set MyDataObject = New DataObject 
 MyDataObject.SetText TextBox1.Text, 233 
 Label1.Caption = "Custom on D.O." 
 CommandButton4.Enabled = True 
 CommandButton2.Enabled = False 
 End If 
End Sub 
 
Private Sub CommandButton4_Click() 
'Get custom format from Clipboard 
 If MyDataObject.GetFormat(233) = True Then 
 Label1.Caption = "Cust format - " _ 
 & MyDataObject.GetText(233) 
End If 
End Sub 
 
Private Sub UserForm_Initialize() 
 CommandButton2.Enabled = False 
 CommandButton4.Enabled = False 
End Sub

Assistance et commentaires

Avez-vous des questions ou des commentaires sur Office VBA ou sur cette documentation ? Consultez la rubrique concernant l’assistance pour Office VBA et l’envoi de commentaires afin d’obtenir des instructions pour recevoir une assistance et envoyer vos commentaires.