Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert text from VB 6 app into open MS Word document 2

Status
Not open for further replies.

trollacious

Programmer
Sep 29, 2004
4,046
US
My wife handles medical dictation and stores some blocks of standard dictation in a text file. I wrote a small VB app to make searching through the file easier, but she still has to paste the found text from the clipboard (copying to the clipboard from the app is done with one button) into the document. Is there a way to have the VB app automatically insert text into an open Word document at the point the cursor is?

I searched the FAQs and the post history for something related to this, but didn't find anything that appeared to address this particular thing.

Lee
 
I found a workable technique using AppActivate and SendKeys, though the VB & VBA in a Nutshell says this is a less than optimal method.

If anyone has any better ideas, I'm open to suggestions.

Lee
 
Pretty sure you can do this with OLE Automation for Word, but it's been years since I've done any manipulation of Word documents.

You might find help for this in the Visual Basic for Applications forum, along with sample code.
 
trollacious,

Something along these lines should do it for you;

Private Sub Command1_Click()

Clipboard.Clear
Clipboard.SetText Text1.Text, vbCFText 'Put text on Clipboard


On Error Resume Next
Set wordobj = GetObject(, "Word.Application") 'get existing instance of Word if there is one
If Err Then Set wordobj = CreateObject("Word.Application") ' otherwise create a new one
On Error GoTo 0
With wordobj
.Visible = True
If .Documents.Count = 0 Then 'no docs
.Documents.Add 'so create one
Else
On Error Resume Next
.Documents("Document1").Activate 'activate the special doc if loaded
If Err Then
.Documents.Add 'not loaded so create a new one
End If
On Error GoTo 0
End If
.Selection.Paste 'paste clipboard to word doc
End With


End Sub

hth Hugh,
 
If I use this code - it comes up with an error saying "Object Required" on the following line:

Code:
     Clipboard.SetText Text1.Text, vbCFText                                  'Put text on Clipboard

Just wondering if theres a certain reference I need to enable to make it run?

Cheers
 
Ignore that, stupid error on my part - I was putting label3.caption in, and it was in a module rather than a form, everything works now I've put the form name in beforehand.

D'Oh!

Many thanks :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top