how to control word from excel

  • Thread starter Torstein S. Johnsen
  • Start date
T

Torstein S. Johnsen

I have never tried to work with VBA controlling two programs. After good
help form MVP and a lot of testing I'm at the level that things start to
work.

I copy an area from excel, open a document in word based on a template and
like to copy the text to a bookmark in the new document in Word. Everything
works until the pasting. I have tried several things - here is my last trial
that does not work.

I think there is an easy answer to this:

Sub åpneword()
Range("adrbedrift").Copy
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim wordwasnotrunning As Boolean
On Error Resume Next

Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
wordwasnotrunning = True
End If

oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Add("g:\Maler\Excel\Brev som hentes fra
excel.dot")
' oDoc.Range.Text = "hi"

oDoc.Bookmarks("adresse").Select

Selection.PasteSpecial Link:=False, DataType:=wdPasteText, Placement:=
wdInLine, DisplayAsIcon:=False

End Sub
 
T

Tom Ogilvy

Since you are running this from Excel, the unqualified Selection will refer
to the selection in Excel. You need to qualify it with the word
application. Assuming this is where you are having a problem.

Sub åpneword()
Range("adrbedrift").Copy
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim wordwasnotrunning As Boolean
On Error Resume Next

Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
wordwasnotrunning = True
End If

oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Add("g:\Maler\Excel\Brev som hentes fra
excel.dot")
' oDoc.Range.Text = "hi"

oDoc.Bookmarks("adresse").Select

oWord.Selection.PasteSpecial Link:=False, _
DataType:=wdPasteText, Placement:= _
wdInLine, DisplayAsIcon:=False

End Sub
 

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