简体   繁体   中英

Ho to copy text to clipboard and paste into MS Word table?

I need to copy some values from textBox1 and textBox2 to clipboard by clicking on button "Copy To Clipboard". After that I open Word document, place the cursor into the table and press "Ctrl+V".

See image here

This is my code:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.TextLength > 0 Then
            Clipboard.SetText(TextBox1.Text & TextBox2.Text)
        Else
            Clipboard.Clear()
        End If
    End Sub
End Class

I want to paste the values into the table cells, but I can't do this, because it paste into one cell all the values.

If you separate the values with a tab, they will paste into separate cells in Word, but only if you select multiple cells before pasting. If you have a single cell selected, it will paste the tab separated values into that single cell (Word 2010, others may be different).

Clipboard.SetText(TextBox1.Text & vbTab & TextBox2.Text)

Hopefully someone else knows a cleaner way. For example, if you copy cells from Excel, there are additional paste options in Word (eg Overwrite Cells seems to be what you are looking for), and presumably these are triggered by Excel putting the data onto the clipboard in different formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM