Cannot paste to Excel

Posted on 2013-10-01
Medium Priority
Last Modified: 2013-10-22
I have a Visual Basic 2008 application which copies a RTF stream to the clipboard. I am trying to paste this into Excel programmatically. But it is not working.

When the code executes nothing is placed in Excel. The information is successfully copied into the Clipboard. I know this because I can paste the RTF document manually into Word with Ctrl-V. Oddly, the PASTE option is not available in Excel when the RTF document is programmatically copied into the clipboard.

However, if I copy the RTF document from Word with Ctrl_C and then go to Excel, Paste is available.

Summary                                Paste into Word                             Paste into Excel
Programmatic Copy                               Y                                                  N
Manual Copy from Word                        Y                                                  N

I have included the code (4 line) in the attached Word document. Also in the Word document is the Rtf document I am trying to copy and paste. Notice the Greek and subscript characters.

The variable sExportString represents the RTF document converted into a String. This is in the Word document also.

I am puzzled. Thank you for your assistance.

Question by:mpdillon
  • 3
  • 2
LVL 10

Expert Comment

ID: 39537033
Hi Pat,

Have you tried using Clipboard.GetText and setting the range value to it?

edit: Also, note that there is no file attached.

Author Comment

ID: 39537680
Here is the file. I will try the GetText in a minute.
LVL 10

Accepted Solution

FamousMortimer earned 1500 total points
ID: 39537745
You should be able to change

Open in new window

objExcel.worksheets(WorksheetNameString).range(CellAddressString).Value = Clipboard.GetText

Open in new window


Author Comment

ID: 39537863
Progress! But not quite there. The Get text method returns nothing without an argument. If I place "TextDataFormat.Rtf" as the argument to GetText, the paste method place the text into the cell. However, the Paste method returns the text and not the RTF formatted text.

I looked at GetDataObject but it did not place any text into Excel either.

Any ideas?


Author Closing Comment

ID: 39591482
Excel will not accept RTF data from the Clipboard. But you can copy foramatted data from another office application and paste it into Excel. So the work around is past RTF data into WORD. Copy from Word using default settings and paste into Excel.

Please see my code below.
(Word is referenced in the project because I wanted the intellisense help. Excel is late bound and not referenced to avoid version conflicts.)

Dim objWord As New Microsoft.Office.Interop.Word.Application
objWord.Documents.Add(Template:="Normal", NewTemplate:=False, DocumentType:=0)

On Error Resume Next
objExcel = GetObject(, "Excel.Application")

If Err.Number = 429 Then
    objExcel = CreateObject("Excel.application")
End If

On Error GoTo 0
'Remove Worksheets 1 & 2
For i = 1 To objExcel.Worksheets.Count - 1
    objExcel.Application.DisplayAlerts = False
Next i
objExcel.Application.DisplayAlerts = True
WorksheetNameString = "Sheet1"
objExcel.Worksheets(objExcel.Worksheets.Count).name = WorksheetNameString

My.Computer.Clipboard.SetText(sExcelString, TextDataFormat.Rtf)


CellAddressString = objExcel.worksheets(WorksheetNameString).cells(r, 3).address(False, False)
objExcel.worksheets(WorksheetNameString).range(CellAddressString).value = My.Computer.Clipboard.GetData(0)

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post, we will learn to set up the Group Naming policy and will see how it is going to impact the Display Name and the Email addresses of the Group.
Read this tutorial to learn how to fix repeating password error prompts when setting up Gmail IMAP with Microsoft Outlook. The entire process is described with step by step, illustrated instructions. Enjoy...
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question