Copy word text to clipboard...

C

Co

Hi All,

I want to get the text from the first paragprahs of a word document
and copy it to the clipboard.
But my code stops when it should copy to the clipboard.

Function ReadWordDocument(ByVal sPathName As String, ByVal sDocName
As String) As String

Dim wdToepassing As New Word.Application
Dim wdDoc As Word.Document
Dim parra As Word.Paragraph

wdDoc = wdToepassing.Documents.Open(sPathName & sDocName)
With wdDoc
parra = .Paragraphs(1)
parra.Range.Select()
.Selection.Copy() --> code stops, no error message!!
End With

ReadWordDocument = _
System.Windows.Forms.Clipboard.GetDataObject.GetData
(DataFormats.Text)
wdDoc = Nothing
wdToepassing.ActiveWindow.Close()
wdToepassing = Nothing

End Function

Marco
 
O

OmegaSquared

Hello, Marco,

Maybe it's because of differences in versions, but to make your code work I
had to make a couple of minor changes.

In my case, I find that Paragraphs does not have a default property, so I
had to use Paragraphs.Item(1) instead. Also, Word.Document does not have a
"Selection" method. Word.Application does, so I used that instead.

With those changes, your code worked successfully for me. (The code with my
changes is pasted below.) Btw, I always recommend keeping "Option Strict
On", so in addition, there are a couple of explicit conversions required by
this.)

Function ReadWordDocument(ByVal sPathName As String, ByVal sDocName As
String) As String

Dim wdToepassing As New Word.Application
Dim wdDoc As Word.Document
Dim parra As Word.Paragraph

wdDoc = wdToepassing.Documents.Open(sPathName & sDocName)
With wdDoc
parra = .Paragraphs.Item(1)
parra.Range.Select()
wdToepassing.Selection.Copy()
End With

ReadWordDocument = _

CStr(System.Windows.Forms.Clipboard.GetDataObject.GetData(DataFormats.Text))
wdDoc = Nothing
wdToepassing.ActiveWindow.Close()
wdToepassing = Nothing

End Function

Groetjes,
Randy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top