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 :wave:
Re: Retriving selected text from MS Word inside vb application
Welcome to the forums. :wave:
Lets start with the code you have so far, and we will go from there.
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
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
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 :)
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?
Re: Retriving selected text from MS Word inside vb application
Is your document linked or embedded?
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