Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):

C#
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 ,, and that should be done from my c# application on some button_click event.

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

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

Any help is appreciated and so many thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 1-Nov-11 12:24pm    
Yes, in the instance of Web control working in your application it does make sense, but for IE? Don't you think it's useless? You will do it for IE, but how about all other browsers your customers might want to use?
--SA
BobJanova 1-Nov-11 12:33pm    
If you have to click a button anyway, why not just click 'Right click/View source'?

Hello,

You can do it by using OLE automation.

First add a COM reference to SHDocVw. If you don't have the interop, VS can create it for you automatically, In the tool box, select "Select Items", then in the Com component select Microsoft Web Browser, drag a Microsoft Web Browser on you form and delete it, this make VS creates all the interops and add all the references you will need.

And this is the code:

C#
SHDocVw.ShellWindows shellWindows = new ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
    if (!String.IsNullOrEmpty(ie.LocationURL))
    {
        ie.ExecWB(OLECMDID.OLECMDID_SELECTALL, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
        ie.ExecWB(OLECMDID.OLECMDID_COPY, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
    }
}


Be aware that you will need to test LocationURL has the loop will return ie windows as well as Windows explorer windows.

Valery.
 
Share this answer
 
Comments
BillWoodruff 2-Nov-11 1:38am    
Interesting answer: can I assume the result of a successful execution of the inner code on a valid IE window will result in the source code of the page going to the clipboard ?
Valery Possoz 2-Nov-11 5:04am    
It does not copy the source, it copies the rendered content. But I'm sure you could tweak it a bit to get the source. :)
BillWoodruff 2-Nov-11 5:13am    
Okay, by 'source code' here I did mean the html-whatever driving the currently rendered page. But that does include the header ... right ? And what is copied does go on the clipboard ... right ? :)
Thanks a lot for reply, I am going to test it right away and see what I will end with.
So many thanks again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900