|
-
Nov 7th, 2008, 01:43 PM
#1
Thread Starter
New Member
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
-
Nov 7th, 2008, 01:49 PM
#2
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.
-
Nov 7th, 2008, 03:21 PM
#3
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
-
Nov 8th, 2008, 02:44 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 8th, 2008, 03:27 PM
#5
Thread Starter
New Member
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
-
Nov 8th, 2008, 03:38 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 8th, 2008, 03:42 PM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Nov 9th, 2008, 12:02 AM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|