Results 1 to 8 of 8

Thread: Retriving selected text from MS Word inside vb application

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    8

    Retriving selected text from MS Word inside vb application

    Hi
    I am a beginner to vb. Actually I have open a word file using OLE container in visual basic 6.0. But i am not able to copy a selected text from that word file into clipboard without pressing ctrl + C, I have also tried sendkeys() function but it is not working. So please, Can anyone give me some idea about or one can also give me sample code.
    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Retriving selected text from MS Word inside vb application

    Welcome to the forums.

    Lets start with the code you have so far, and we will go from there.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Retriving selected text from MS Word inside vb application

    assuming you have created a word object, just use words copy method
    wdobj.selection.copy
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Retriving selected text from MS Word inside vb application

    Seeing how you are using the OLE control, it may make it a bit harder to work with.

    Dont use SendKeys as its flakey and wont work in Vista.

    Use the SendMessage API with the WM_COPY command.


    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
                        ByVal hwnd As Long, _
                        ByVal wMsg As Long, _
                        ByVal wParam As Long, _
                        ByRef lParam As Any) As Long
    
    Private Const WM_COPY As Long = &H301
    
    Private Sub Command1_Click()
        Dim lRet As Long
        Dim sText As String
        lRet = SendMessage(Me.Text1.hwnd, WM_COPY, 0&, 0&)
        If lRet <> 0 Then
            sText = Clipboard.GetText(vbCFText)
            If Len(sText) > 0 Then
                MsgBox sText, vbInformation + vbOKOnly
            End If
        End If
    End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    8

    Re: Retriving selected text from MS Word inside vb application

    hi
    Thanks for sending code sample.

    I have checked, your code is working fine, when retrieving texts from text box, as you have done. But in case of OLE container with M S Word inside vb form, it is not working. I have tried, then what's problem there ? and also i have set focus again to OLE object (named OLE1)

    I just changed
    lRet = SendMessage(Me.Text1.hwnd, WM_COPY, 0&, 0&)
    to
    lRet = SendMessage(Me.OLE1.hwnd, WM_COPY, 0&, 0&).
    Is there something wrong in my approach or it is not working for OLE, Can you please check that whether it is working for OLE - Word file or not ?

    Really, people like you are very helpful to us, because we can not always find all this kind of things in books.

    Thanks

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Retriving selected text from MS Word inside vb application

    Let me double check but what event are you wanting to copy the selected text? A button click or something else?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Retriving selected text from MS Word inside vb application

    Is your document linked or embedded?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    8

    Re: Retriving selected text from MS Word inside vb application

    It is embedded, and opened with edit mode

    Private Sub Form_Load()
    OLE1.CreateEmbed ("D:\temp.doc")
    OLE1.Action = 7
    End Sub

    and i have used command button's click event to copy a selected text, and that button is placed in a same form.

    Thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width