Clipboard is empty or not valid

J

JJ

I am attempting to automate word from .NET, inserting rtf by copying it into
the Clipboard from a string and pasting it into a word range. However, I get
the following COMException about half the time that I try pasting using the
Range.Paste() method.

"This method or property is not available because the Clipboard is empty or
not valid."

The code to copy/paste is as follows:

string rtf; //contains the rtf that I want to insert
Range range; //the range to paste to

try
{
lock(OfficeConstants.RTF_CONTROL) //this is a .NET
RichTextBox
{
OfficeConstants.RTF_CONTROL.Set(rtf);

OfficeConstants.RTF_CONTROL.CopyToClipboard();
}
}
catch
{
Clipboard.Clear();

throw;
}

Options opts = range.Application.Options;

bool origSmartPaste = opts.SmartCutPaste;

try
{
opts.SmartCutPaste = false; //disable smart pasting

range.Paste(); //THIS IS THE LINE THAT THROWS THE EXCEPTION
}
finally
{
opts.SmartCutPaste = origSmartPaste;
}

The copy part works, as the value is in the clipboard, even when the error
occurs. I can also eliminate the error by sleeping for a few milliseconds
between copying and pasting. This makes me think it is some sort of timing
issue with the value not being ready when trying to paste. Does anyone know
anthing more about the cause of this, or a fix?
 
R

Russ

Inline reply
I am attempting to automate word from .NET, inserting rtf by copying it into
the Clipboard from a string and pasting it into a word range. However, I get
the following COMException about half the time that I try pasting using the
Range.Paste() method.

"This method or property is not available because the Clipboard is empty or
not valid."

The code to copy/paste is as follows:

string rtf; //contains the rtf that I want to insert
Range range; //the range to paste to

An observation and not necessarily a fix.
Normally in programming, it is best not to use variable names with program
language syntax meaning.
You define a range with the name of range. Try naming it objRange instead.
http://support.microsoft.com/kb/173738
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top