//</SNIPPET3> //<SNIPPET4> private void CreateHyperlinkFromSelection() { if (webBrowser1.Document != null) { mshtml.IHTMLDocument2 iDoc = (mshtml.IHTMLDocument2)webBrowser1.Document.DomDocument; if (iDoc != null) { mshtml.IHTMLSelectionObject iSelect = iDoc.selection; if (iSelect == null) { MessageBox.Show("Please select some text before using this command."); return; } mshtml.IHTMLTxtRange txtRange = (mshtml.IHTMLTxtRange)iSelect.createRange(); // Create the link. if (txtRange.queryCommandEnabled("CreateLink")) { Object o = null; txtRange.execCommand("CreateLink", true, o); } } } }
/// <summary> /// Executes the execCommand on the selected range (given the range) /// </summary> private void ExecuteCommandRange(mshtmlTextRange range, string command, object data) { try { if (range != null) { // ensure command is a valid command and then enabled for the selection if (range.queryCommandSupported(command)) { if (range.queryCommandEnabled(command)) { // mark the selection with the appropriate tag range.execCommand(command, false, data); } } } } catch (Exception ex) { // Unknown error so inform user throw new HtmlEditorException("Unknown MSHTML Error.", command, ex); } }