dcsimg
Results 1 to 8 of 8

Thread: Get the selected text from webbrowser, n show it in text box...?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Get the selected text from webbrowser, n show it in text box...?

    Hi all

    Its very urgent, plz tell me how to show the selected text from webbrowser to the text box?

    Plz help me.

    Kind Regards
    Seema

  2. #2
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Get the selected text from webbrowser, n show it in text box...?

    Click Project > Add References and browse to the Microsoft.mshtml.dll which is normally located in the c:\Program Files (x86)\Microsoft.Net\Primary Interop Assemblies folder (if you're using a 32-bit OS look inside your Program Files folder instead of the Program Files (x86) folder). Now import the mshtml namespace and use code similar to this:
    Code:
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim domDocument As IHTMLDocument2 = DirectCast(WebBrowser1.Document.DomDocument, IHTMLDocument2)
        Dim selection As IHTMLSelectionObject = domDocument.selection
        If selection IsNot Nothing Then
          Dim range As IHTMLTxtRange = DirectCast(selection.createRange, IHTMLTxtRange)
          If range IsNot Nothing Then
            TextBox1.Text = range.text
          End If
        End If
      End Sub
    As an alternative you could simply copy the selection to the clipboard and get it from there.
    Code:
        WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
        If Clipboard.ContainsText Then
            TextBox1.Text = Clipboard.GetText()
        End If
    However this would obviously remove anything the user has put on the clipboard.
    Last edited by Joacim Andersson; Jan 20th, 2014 at 09:01 AM.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Re: Get the selected text from webbrowser, n show it in text box...?

    Hi Joacim,

    I tried with all the steps u gave, but its giving the error. -------Object reference not set to an instance of an object. at this line below -----
    WebBrowser1.Document.ExecCommand("Copy", False, Nothing)


    I have added a webbrowser control.
    I am loading a .pdf file to it.
    Then i will select some text with my mouse. then i will click on the button in which i have pasted ur code.

    Code:
    Imports System.Text
    Imports System.IO
    Imports SHDocVw
    Imports mshtml
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            WebBrowser1.Navigate("C:\Users\aseema\Desktop\UGSI - URL Access Request Process.pdf")
            ' WebBrowser1.Navigate("http://www.nablasoft.com/")
            ' WebBrowser1.DocumentText += WebBrowser1_DocumentCompleted()
    
        End Sub
    
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          
    
            'Dim domDocument As IHTMLDocument2 = DirectCast(WebBrowser1.Document.DomDocument, IHTMLDocument2)
            'Dim selection As IHTMLSelectionObject = domDocument.selection
            'If selection IsNot Nothing Then
            '    Dim range As IHTMLTxtRange = DirectCast(selection.createRange, IHTMLTxtRange)
            '    If range IsNot Nothing Then
            '        TextBox1.Text = range.text
            '    End If
            'End If
    
    
            WebBrowser1.Document.ExecCommand("Copy", False, Nothing)
            If Clipboard.ContainsText Then
                TextBox1.Text = Clipboard.GetText()
            End If
    
        End Sub

    Plese help me out dear friend...

    Thanks in advance
    Seems

  4. #4
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Get the selected text from webbrowser, n show it in text box...?

    If you've loaded a PDF file then you obviously can't use the HTML DOM to do anything. The WebBrowser is in this case using an add-in to display the PDF. Why aren't you simply using the Adobe PDF reader control instead of the WebBrowser?
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Re: Get the selected text from webbrowser, n show it in text box...?

    Hi Joacim,

    Yes i now i added the Adobe PDF reader, but here also i am getting the same error, Can plz 1 help to me, If u have a small program created for this, then plz send me here, or email me -
    <Personal e-mail information removed by moderator>
    Help me out friend.

    Will be waiting for your reply.

    Kind Regards
    Seem
    Last edited by Joacim Andersson; Jan 21st, 2014 at 09:46 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Re: Get the selected text from webbrowser, n show it in text box...?

    Hi Joacim,

    Please tell me, how to do it with the pdf adobe reader?

    Its very high priority....

    Will be waiting

    Regards
    Seema

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Re: Get the selected text from webbrowser, n show it in text box...?

    Hi,

    I wrote the code to show the .pdf file as below.
    Code:
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
            Dim dlg As OpenFileDialog = New OpenFileDialog()
            ' set file filter of dialog
            dlg.Filter = "pdf files (*.pdf) |*.pdf;"
            dlg.ShowDialog()
            If Not dlg.FileName Is Nothing Then
    
            End If
            ' use the LoadFile(ByVal fileName As String) function for open the pdf in control
            AxAcroPDF1.src = dlg.FileName
    
        End Sub
    Now my question is how to get the selected text to the text box control.

    Please help me out.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2014
    Posts
    64

    Re: Get the selected text from webbrowser, n show it in text box...?

    Can any 1 plz help me. i have to submit it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width