none
WebBrowser vs. Internet Explorer issue

    Question

  • Hi,

    I have been googling for 2 days with no luck.

    I am using the following code with webBrowser Control to copy it's actual contents (NOT the html code behind):

    private void button4_Click(object sender, EventArgs e)
            {
                webBrowser1.Document.ExecCommand("SelectAll", false, null);
                webBrowser1.Document.ExecCommand("Copy", false, null);
            }
    

    And this code is working perfectly.

    The problem is that I need to do the same thing exactly on any web page that is opened in my IE.

    I have been looking at some articles about hooking and getting a handle for IE and so but with no actual working example.

    Does any one knows how or is doing such a thing actually possible.

    Any help is appreciated and so many thanks in advance

    Monday, October 31, 2011 8:54 PM

Answers

  • > But what I need is to do the copying action from my c# app , where I have a button that should do it onClick.

    in this case you need to connect to the running instances of web browsers.
    this is possible with Shell.Application COM object. below is an example.
     
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                this.Size = new System.Drawing.Size(1000, 500);
                var rtb = new RichTextBox();
                rtb.Parent = this;
                rtb.Dock = DockStyle.Fill;
    
                foreach (var ie in FindAllIE())
                {
                    var url = (string) ie.Document.location.href;
                    rtb.AppendText(url + Environment.NewLine);
    
                    // returns slightly more (for example CDATA) than selectAll + copy
                    rtb.AppendText((string)ie.Document.body.innerText + Environment.NewLine);
                }
            }
    
            IEnumerable<dynamic> FindAllIE()
            {
                var t = Type.GetTypeFromProgID("Shell.Application");
                dynamic o = Activator.CreateInstance(t);
                try
                {
                    var ws = o.Windows();
                    for (int i = 0; i < ws.Count; i++)
                    {
                        var ie = ws.Item(i);
                        if (ie != null && System.IO.Path.GetFileName((string)ie.FullName) == "iexplore.exe")
                            yield return ie;
                    }
                }
                finally
                {
                    Marshal.FinalReleaseComObject(o);
                }
            }
        }
    }
    
    
     
    • Proposed as answer by Malobukv Tuesday, November 01, 2011 5:24 PM
    • Marked as answer by MyPuplicIdentity Wednesday, November 02, 2011 7:07 PM
    Tuesday, November 01, 2011 5:04 PM

All replies

  • Hello guys,

    I am re posting this because I believe you have missed seeing it some how.

    If there is something wrong with my question please point me to the right direction.

    And, again, thanks in advance.

    Tuesday, November 01, 2011 2:18 PM
  • Hi,

    The ExecCommand should be available in IE through JavaScript. It's a method on the DOM. The WebBrower control is IE.

    http://msdn.microsoft.com/en-us/library/ms536419(VS.85).aspx

    That might help but do a search for JavaScript and ExecCommand and you'll find loads of links.

     


    "The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination." - Fred Brooks
    Tuesday, November 01, 2011 2:41 PM
  • > to copy it's actual contents [...] I need to do the same thing exactly on any web page that is opened in my IE.

     
    you can paste following string into the Address bar of IE (make sure that "javascript:" exists after paste) and press enter.

    javascript:(function() {document.execCommand("selectAll"); document.execCommand("copy");})()
    

    Tuesday, November 01, 2011 3:23 PM
  • Thanks for reply, Derek , I will do some search for those points and see where it leads.

    Also Thanks for reply Malobukv , But what I need is to do the copying action from my c# app , where I have a button that should do it onClick.

    I know this might seem strange as I am able to do it using webBrowser control easily, but I have to do it on IE since it is the program of choice in work.

    I hope this clarification gives some help so you can help ffurther , if you will, and thanks for all your help here and there......

    Thanks again for you all


    Tuesday, November 01, 2011 3:33 PM
  • > But what I need is to do the copying action from my c# app , where I have a button that should do it onClick.

    in this case you need to connect to the running instances of web browsers.
    this is possible with Shell.Application COM object. below is an example.
     
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                this.Size = new System.Drawing.Size(1000, 500);
                var rtb = new RichTextBox();
                rtb.Parent = this;
                rtb.Dock = DockStyle.Fill;
    
                foreach (var ie in FindAllIE())
                {
                    var url = (string) ie.Document.location.href;
                    rtb.AppendText(url + Environment.NewLine);
    
                    // returns slightly more (for example CDATA) than selectAll + copy
                    rtb.AppendText((string)ie.Document.body.innerText + Environment.NewLine);
                }
            }
    
            IEnumerable<dynamic> FindAllIE()
            {
                var t = Type.GetTypeFromProgID("Shell.Application");
                dynamic o = Activator.CreateInstance(t);
                try
                {
                    var ws = o.Windows();
                    for (int i = 0; i < ws.Count; i++)
                    {
                        var ie = ws.Item(i);
                        if (ie != null && System.IO.Path.GetFileName((string)ie.FullName) == "iexplore.exe")
                            yield return ie;
                    }
                }
                finally
                {
                    Marshal.FinalReleaseComObject(o);
                }
            }
        }
    }
    
    
     
    • Proposed as answer by Malobukv Tuesday, November 01, 2011 5:24 PM
    • Marked as answer by MyPuplicIdentity Wednesday, November 02, 2011 7:07 PM
    Tuesday, November 01, 2011 5:04 PM
  • Tuesday, November 01, 2011 5:40 PM
  • I will try to do what you have just suggested, and I will update as it goes. Thanks a lot

    Tuesday, November 01, 2011 6:03 PM
  • Hi, Malobukv,,,  As I am testing I realized that the code you have just provided is .NetFramework 4 compatible.

    As I am using 3.5 version. This is gonna take some time until I update my code to version 4.

    I am working on it currently, and I will post the end result uphere for public use.

    I am some how certain that your code is perfect. Thanks a lot

    Tuesday, November 01, 2011 6:29 PM
  • The code provided is just above super. It works like a miracle.

    There is one thing I think I am gonna work on a little bit, which is that if multiple tabs are open it copies the contents of all open tabs. I guess this can be solved by specifying the titlebar text for example, correct me if needed.

    Any way, the code is brilliant, and I am marking it as answered.

    Thanks so much,  Malobukv...

     

     
    Wednesday, November 02, 2011 7:07 PM