VBA & Macro Problems

Copy a Text File to the Clipboard with Excel VBA

Anum MushtaqAnum Mushtaq Nov 14, 2026 868 views

Question details

Use an Excel VBA macro to select a text file and copy its contents to the clipboard.

Copy a Text File to the Clipboard with Excel VBA
Product
Microsoft Excel
Device & OS
Windows
Scenario
A user wants to select a text file from an Excel macro and paste its contents into another application.
Observed behavior
The desired result is for the selected file's text to be available on the Windows clipboard.
Before you start

Use Excel for Windows and save the workbook as a macro-enabled .xlsm file. Make sure macros are enabled for the workbook and have a plain-text file ready to select.

Solution 1Recommended

Use MSForms DataObject to copy the selected file

Add the Microsoft Forms library reference, then run a macro that reads a selected text file and places its contents on the clipboard.

This example is intended for plain text in the system's default encoding. UTF-8 or Unicode files may require an encoding-aware reader instead.

1
Load the Forms library

Press Alt+F11 to open the Visual Basic Editor. Choose Insert > UserForm to load the Microsoft Forms library, then check Tools > References for Microsoft Forms 2.0 Object Library.

2
Add the macro

Choose Insert > Module and paste this code: Sub CopyTextFileToClipboard() Dim filePath As Variant Dim fileNumber As Integer Dim fileIsOpen As Boolean Dim fileText As String Dim clipboard As MSForms.DataObject filePath = Application.GetOpenFilename("Text Files (*.txt), *.txt") If VarType(filePath) = vbBoolean Then Exit Sub On Error GoTo HandleError fileNumber = FreeFile Open CStr(filePath) For Input As #fileNumber fileIsOpen = True If LOF(fileNumber) > 0 Then fileText = Input$(LOF(fileNumber), #fileNumber) End If Close #fileNumber fileIsOpen = False Set clipboard = New MSForms.DataObject clipboard.SetText fileText clipboard.PutInClipboard MsgBox "File contents copied to the clipboard." Exit Sub HandleError: If fileIsOpen Then Close #fileNumber MsgBox "Could not copy the file: " & Err.Description, vbExclamation End Sub

3
Run and paste

Return to Excel, press Alt+F8, select CopyTextFileToClipboard, and click Run. Select a .txt file, then use Ctrl+V in the destination application.

Use MSForms DataObject to copy the selected file
Library reference: The macro requires the Microsoft Forms 2.0 Object Library. If it is unavailable, the MSForms.DataObject declaration cannot compile.
Free Microsoft Office alternative

Work with spreadsheets in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for working with spreadsheet files. Open compatible Excel workbooks to make migration easier, but use Excel for the MSForms clipboard macro described above.

Open and edit compatible Microsoft Excel .xlsx and .xlsm filesUse a familiar spreadsheet interface for everyday workbook tasksWork with a free, lightweight office suiteMove existing Office documents into a familiar workspace
microsoft office alternative - wps office

Frequently Asked Questions

Why does VBA report that MSForms.DataObject is not defined?

In the Visual Basic Editor, choose Insert > UserForm, then check Tools > References for Microsoft Forms 2.0 Object Library. The macro needs that library reference to compile.

What happens if I cancel the file picker?

GetOpenFilename returns False when you click Cancel. The example checks for a Boolean result and exits without changing the clipboard.

Will the macro read UTF-8 or Unicode text correctly?

Not necessarily. The Input function uses the system's default text handling, so files in other encodings may need an encoding-aware reader before their contents are passed to SetText.

How do I keep the macro in my workbook?

In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Enable macros when you reopen the workbook if prompted.