Web Browser, HTML selection

Hi,

I have implemented a simple Web Browser using the inbuilt Web Browser component. I now need to be able to highlight some text on the web page and for this to then be selected by the J# program. This is possible in Java by using the getSelectionSrat and getSelectionEnd methods linked to the JEditorPane, and it is possible in C# by using
IHTMLSelectionObject and IHTMLTxtRange, but now I need to find the J# version!

Regards
Richard


Answer this question

Web Browser, HTML selection

  • Sudheer_sgk

    anyone !
  • Jeff Schindler

    Hi,
    Great to know that you could solve your initial problem.
    Can you please post here the error message you are getting and also the type of webBrowser1 and IHTMLDocument2.

    Thanks.

  • Mark Watkin

    Hi,
    These errors are genuine one.
    In first case looks like an instance of System.Windows.Forms.HtmolDocument is being typecasted to 'com.ms.wfc.html.om.IHTMLDocument2'. These two are entirely different interfaces (belonging to different namespaces) and can't be typecasted.

    1. System.Windows.Forms.HtmolDocument  can be typecasted to  System.Windows.Forms.HtmolDocument2.
    2.   com.ms.wfc.html.om.IHTMLDocument can be typecasted to com.ms.wfc.html.om.IHTMLDocument2.
    3. System.Windows.Forms.HtmolDocument  can't be  typecasted to com.ms.wfc.html.om.IHTMLDocument2.
    4. com.ms.wfc.html.om.IHTMLDocument can't be typecasted to System.Windows.Forms.HtmolDocument2.

    Same reason is valid for second case also. Here a object of type mshtml.HTMLDocumentClass is being converted to com.ms.wfc.html.om.IHTMLDocument2.

    Actually com.ms.wfc.html.om is a entirely seperate hierarchy. Object which don't implement interfaces defined in com.ms.wfc.html.om can't be typecasted to those interfaces. This is true for any object and any interface (whether defined in com.ms.wfc.html.om or not). You need to make sure that the object you are typecasting implements the required interface.

    Hope it helps. Please post back if you face issues.
    Thanks.


  • Simeao

    Hi Richard,< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    You can do it in J#, exactly as you had done in C# - using Microsoft.mshtml.dll.

    << 

    IHTMLDocument2 HTMLDocument2 = (IHTMLDocument2)axWebBrowser1.get_Document();

                IHTMLSelectionObject selection = HTMLDocument2.get_selection();

                IHTMLTxtRange range = (IHTMLTxtRange)selection.createRange();

                MessageBox.Show(range.get_text());

    >> 

     

    Note : You do not need types in the package “com.ms.wfc.html.om”. So please do not reference the assembly vjswfc.dll. Use types in Microsoft.mshtml.dll as you would have done in C#.

     

    Let us know if you still see any issues.

     

    Thanks,

    Diganta Roy.



  • bhayes

    Hi Richard,

    If you need the entire text within the current tag use the following code

    IHTMLDocument2 HTMLDocument1=(IHTMLDocument2)axWebBrowser1.get_Document()

    IHTMLSelectionObject selection = HTMLDocument1.get_selection();

    IHTMLTxtRange range = (IHTMLTxtRange)selection.createRange();

    IHTMLElement ele = range.parentElement();

    range.moveToElementText(ele);

    MessageBox.Show("html text:"+range.get_htmlText());

    If you want to exclude html tag you can replace the last line with

    MessageBox.Show("text :"+range.get_text());

    Thanks,

    Noorul Ameen.


  • 50322899

    Error 1 Cannot cast from 'System.Windows.Forms.HtmlDocument' to 'com.ms.wfc.html.om.IHTMLDocument2'

    I then read something about putting a get_DomDocument thing on the end:

    IHTMLDocument2 HtmlDoc = (IHTMLDocument2)webBrowser1.get_Document().get_DomDocument();

    This will run, but when i run the method with this in it returns the following error:

    System.InvalidCastException was unhandled
      Message="Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'com.ms.wfc.html.om.IHTMLDocument2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{ADF69903-7DF3-3D87-A584-6B57EB1AFCEC}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

    webBrowser1 is just a Web Browser component and IHTMLDocument2 I understand as just being a component you can stick the details of a web Browser into to use its data.

    Richard


  • bforst

    Hi Richard,< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

                       I understand that you need html text to be returned instead of plain text.

                       IHTMLTxtRange.get_text() will return plain text.

                       If you need html to be returned use the following  function

                       IHTMLTxtRange.get_htmlText()

                       Please post back if you still have any issues.

    Thanks,

    Noorul.


  • DaveKoch

    Hi,
    In J# you can do it as following...
    1. Refer vjswfchtml.dll.
    2. import com.ms.wfc.html.om package.
    3. Importing this package will expose IHTMLSelectionObject and IHTMLTxtRange objects. Consume them as per your need.

    Please post back if it doesn't meet your requirement.

    Thanks.


  • tachikoma

    Ok, have managed to implement it, but it gives an error due to the casting of a HTMLDocument onto a IHTMLDocument2

    IHTMLDocument2 HtmlDoc = (IHTMLDocument2)webBrowser1.get_Document();

    IHTMLSelectionObject selection = HtmlDoc.getSelection();

    IHTMLTxtRange range = (IHTMLTxtRange)selection.createRange();

    MessageBox.Show(range.getText());

    can you tell me what i am doing wrong!

    Regards
    Richard


  • Manojk.kothari

    where does the axWebBrowser come from the web browser you drag and drop appears to be just a windows form thing

    Richard


  • vishwas_udy

    the IHTMLTxtRange returns the selected text. Is this taken from the parsed or the unparsed html

    What i need to be able to do is, when i have selected the text in the web browser window, return the html code immediately surrounding this.

    Any Ideas

    Richard


  • monztre

    right, ignore that. done it.

    I now have a program that i can highlight a word or set of words and click select and a prompt window will tell me what i have highlighted. Now, if the selected text is a link or part of one, it returns from <a> ... to ... </a> but if it is just text in the middle of a paragraph, it just returns the text. How can i get it to return everything within the current tagset

    Richard


  • Exellon

    Ok, I see what you are saying.

    Problem now is - I have no idea of how to do this! Any help most welcome. I need:

    A web broswer component, from which when a user highlights some text and presses a button, the selected text is stuck into a string or into a messagebox (doesnt really matter where it goes at the moment, just got to actually get it!)

    I was using the IHTML methods as they have the selection stuff, but the only method to get the document from the web browser seems to be a HTMLDocument, which is not compatable, so what do i do !!

    Many thanks

    Richard

  • Geze

    Any Ideas


  • Web Browser, HTML selection