8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel VBA(Win/Mac)でクリップボードにテキストをコピー

Last updated at Posted at 2015-09-05

Windows版・Mac版のExcel VBAでクリップボードにテキストをコピーする方法。

DataObjectでクリップボードにテキストをコピーすると、Mac版だと文末にゴミ(¥0¥0)が追加されたりするのでMacScriptを使う。

クリップボード設定

クリップボードを使うためにダミーの標準フォームを追加

※参照設定で「C:\WINDOWS\System32\FM20.DLL」を追加して「Microsoft Forms 2.0 Object Library」を参照設定したらMacのExcelで開けなかった

VBAソース

クリップボードにコピー
    text = "test"
    If Application.OperatingSystem Like "*Mac*" Then
        ' Is Mac.
        text = Replace(text, "\", "\\")
        text = Replace(text, Chr(34), "\" & Chr(34))
        MacScript ("set the clipboard to " & Chr(34) & text & Chr(34))
    Else
        ' Is Windows.
        Dim CB As New DataObject
        With CB
            .SetText text
            .PutInClipboard
        End With
    End If

参考

Office TANAKA:クリップボードを操作する(1)
Office TANAKA:MacScript関数
Excel for Windows および Excel for the Mac でプログラムによってファイルを選択する

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?