using ExecCommand to copy in MSHTML

I am working on an editor using mshtml. I just cannot get cut / copy / paste to work with ExecCommand. All other command identifiers seem to be working well. Here is my code:

public partial class Form1 : Form {
private static HtmlDocument htmldoc;
private bool fireonce = false; // fire previewkeydown only once

public Form1( ) {
InitializeComponent( );
}

private void Form1_Load(object sender, EventArgs e) {
webBrowser1.Navigate("about:blank");
webBrowser1.Select( );
}

private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
// the event is always fired twice; likely a .net bug
if ( fireonce ) {
fireonce = false; // prevent firing twice
return;
} // end if
// process key pressed
switch ( e.KeyCode ) {
case Keys.N:
if ( e.Alt ) {
htmldoc.ExecCommand("SelectAll", false, null);
htmldoc.ExecCommand("Italic", false, null);
htmldoc.ExecCommand("Cut", false, null); // does not work
//htmldoc.ExecCommand("Delete", false, null); // works
fireonce = true; // prevent firing twice
} // end if
break;
} // end switch
return;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
htmldoc = webBrowser1.Document;
htmldoc.ExecCommand("EditMode", true, null);
}

}

I am using a webBrowser control in the form. When alt-N is pressed, all texts are selected and turned italic but not cut. If I changed the "Cut" to a "Delete", all texts can be deleted. "Cut", "Copy" & "Paste" are not working. Any idea how I can put them to work



Answer this question

using ExecCommand to copy in MSHTML

  • mobile-fedot

    I was just able to fix this on my machine by changing my security settings in IE. Tools > Options > Security > Custom Level > Enable "Allow paste operations via script." Now I have to remember what caused me to turn that off in the first place...

    Hope this helps.

  • Nick__A.

    Hi, just would like to know if anyone is looking into this. Many thanks in advance.

  • Sirkku Willie MSFT

    Hi,

    Does anyone have any idea on this

  • jeromaqui

    Thank you very much Francis Shum for the great reply!

    This is very helpfull!


  • paukp06

    But that is not working on my system. In fact i dont have that entry at all. I am trying this with outlook express. I am able to use Bold but not Copy
    I wonder what the problem is!!!




  • Mentz

    I have been having the same problem on my machine. I'm using an identical call to execCommand, but neither "cut", "copy", or "paste" works. The strangest thing is that the code is working for every other developer on the team.

  • Keith WIlliams

    Hi PJ,

    My test code is like this:
    IHTMLDocument2 ihtmldoc2 = (IHTMLDocument2) htmldoc.DomDocument;
    IHTMLSelectionObject ihtmlselection = ihtmldoc2.selection;
    IHTMLTxtRange range = (IHTMLTxtRange) ihtmlselection.createRange( );
    if ( range.queryCommandSupported("Cut") ) {
    range.execCommand("Cut", false, null);
    MessageBox.Show("Cut is supported");
    } // end if

    I tried both queryCommandSupported & queryCommandEnabled on ihtmldoc2 & range respectively. The messagebox was shown but the "cut" was not done. This looks really strange.


  • Alex Aidar

    Many thanks for the reply. I will give a try.

  • mooremedia

    The purpose of my asking is that I would like to perform cut / copy / paste programmatically.

    Since the ExecCommand method does not work for some reasons and I found that manually doing ctrl-X ctrl-C ctrl-V works perfectly, may I ask how to programmatically perform the underlying actions of the above control keys. I have tried sendkeys and that does not work either.

    Many thanks in advance.

  • Nuno Castro

    I can't test here, but you can try the queryCommandSupported method to check if the command is supported.

  • CarlMalden

    Hi Ian,

    In fact, I have solved the problem already. I was just too lazy to dig back this thread from the forum to post the answer.

    I am using XP sp2 and the code works immediately after I removed this registry entry:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\ProtocolDefaults]
    "about"=dword:00000004

    Hope that the info is useful to you & others who come across the same problem.


  • using ExecCommand to copy in MSHTML