Hi,
I need to edit the value in an SP Rich Text field via the WinForms web browser control. Most other controls (input tags) are easy to get and we can change the value quite simply. However, not so simple with Rich Text. I headed over to: http://blog.drisgill.com/2007_05_01_archive.html
and got some ideas. At first, I tried creatign a javascript function and adding it to the page:
function GetRichTextRange(strBaseElementID)
var docEditor=RTE_GetEditorDocument(strBaseElementID);
if (docEditor == null)
{ return; }
var selection = docEditor.selection;
var range = selection.createRange();
return range;
}
However, every time I call this, I always get a null value back. So I tried this instead:
object docEditor = document.InvokeScript("RTE_GetEditorDocument", new object[] { fieldName });
IHTMLDocument2 doc = (IHTMLDocument2)docEditor;
IHTMLSelectionObject selection = doc.selection;
IHTMLTxtRange textRange = (IHTMLTxtRange)selection.createRange();
textRange.pasteHTML(value);
Well, now I am getting an error on the second line: "Unable to cast object of type 'System.DBNull' to type 'mshtml.IHTMLDocument2"
I am not even sure if I am casting to the correct object type anyway, but in any case, it seems what I am getting back from the RTE_GetEditorDocument function is of System.DBNull.
All I want to do is say something like myRichTextHtmlElement.SetAttribute("value", html); but that obviously cannot be done.
To make things worse, I am completely new to javascript and I'm more of a WinForms guy, so my HTML is not exactly hot stuff. Below is the HTML for my RichText field:
<tr>
<td nowrap="true" valign="top" width="190px" class="ms-formlabel">
<h3 class="ms-standardheader">
<nobr>RichText</nobr>
</h3>
</td>
<td valign="top" class="ms-formbody">
<!-- FieldName="RichText"
FieldInternalName="RichText"
FieldType="SPFieldNote"
-->
<span dir="none">
<div class='ms-rtestate-field ms-rtefield' style=''>
<div id='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte_label'
style='display:none'>Rich text editor
</div>
<div class=' ms-rtestate-write ms-rteflags-0'
id='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte'
style='min-height:84px'
aria-labelledby='ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_inplacerte_label'
contentEditable='true' >
<div class="ExternalClassD74B4D64D01941CDB34619757AAA30D8">
<html>
<body>
<h4>A Definition List:</h4>
<dl>
<dt>Coffee</dt>
<dd>Black hot drink</dd>
<dt>Milk</dt>
<dd>White cold drink</dd>
</dl>
</body>
</html>
</div>
</div>
<div style="clear:both;"></div>
</div>
<span dir="ltr">
<input name="ctl00$m$g_29d60052_5630_4981_8452_850a87a50b56$ctl00$ctl05$ctl07$ctl00$ctl00$ctl04$ctl00$ctl00$TextField_spSave"
type="HIDDEN"
id="ctl00_m_g_29d60052_5630_4981_8452_850a87a50b56_ctl00_ctl05_ctl07_ctl00_ctl00_ctl04_ctl00_ctl00_TextField_spSave" />
</span>
</span>
</td>
</tr>
Anyone got any ideas? Thanks!