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?
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?