次の例では、 GetFormat、 GetText、 および SetText メソッドを使用して、 DataObject とクリップボードの間でテキストを転送します。
ユーザーは TextBox にテキストを入力し、CommandButton1 をクリックして標準のテキスト形式で DataObject に転送できます。
CommandButton2 をクリックすると、DataObject からテキストが取得されます。
CommandButton3 をクリックすると、テキストが TextBox1 から DataObject にカスタム形式でコピーされます。
CommandButton4 をクリックすると、テキストが DataObject からカスタム形式で取得されます。
この例を使用するには、このサンプル コードをフォームの Declarations 部分にコピーします。 フォームに次が含まれていることを確認してください。
- テキスト ボックス ( TextBox ) コントロール (TextBox1)
- CommandButton1 から CommandButton4 までの 4 つの CommandButton コントロール。
- ラベル ( Label ) コントロール (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
サポートとフィードバック
Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。