2

I'm new to WPF and also C# so I'll try to be as specific as possible so you'll understand.

What am I trying to do?

I have a WPF Page with a WebBrowser control on it. I am navigating to a specific URL which displays perfectly in the control. Now, I would like to programmatically select all and copy the content of the webpage to my clipboard.

What have I tried

dynamic doc = webbrowser1.Document;
var htmlText = doc.documentElement.InnerText;

This however removes some formatting like empty tablecolumns so it will not be the same data as CTRL-A / CTRL-C

I have also tried the above with InnerHTML and that gives me the HTML code. When I then paste that into an empty notepad and save it as .html file, externally open in IE and perform the CTRL-A / CTRL-C it gives me the desired result.

Any idea how to get the EXACT same result through code?!

F43G4N
  • 159
  • 1
  • 1
  • 9
  • Did you try to print the content of it? Take a look at the link: http://stackoverflow.com/questions/1298969/printing-the-contents-of-a-wpf-webbrowser – cgon Jul 04 '11 at 11:41
  • @caglar_gonul Sorry, no I did not try to print the content. Should I try that? I have also tried to use doc.ExecCommand("SelectAll",null,null) and "doc.ExecCommand("Copy",null,null) but that doesn't work either. – F43G4N Jul 04 '11 at 11:46
  • Could you please add a reference to Microsoft.mshtml and try the link above. it should take 5 minutes. – cgon Jul 04 '11 at 11:51
  • @caglar_gonul This resulted in: Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'System.Windows.Documents.IDocumentPaginatorSource'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2C0C27DF-282F-3225-ADCD-CEC68F890EEB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). – F43G4N Jul 04 '11 at 12:15

1 Answers1

3

Use the following code:

  dynamic document = browser.Document;
  document.ExecCommand("SelectAll", true, null);
  document.ExecCommand("Copy", false, null);
Anvaka
  • 15,658
  • 2
  • 47
  • 56