Control Word through Excel

G

Guest

1. I am opening Word through Excel (No problem)
2. Adding a new document in Word (No problem)
3. Trying to insert Wordfiles stored on disc into new created document by:

oDoc.Selection.InsertFile FileName:=MyFile, Link:=False

The program does not accept the line above. Why?

Here is the total program:
Sub InsertWordFiles()
'Inserts all Wordfiles in "C:\Temp" into a new created Worddocument
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim MyFile As String

'Open Word and add a new document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Add

'Add all Wordfiles in "C\Temp" into new created Wordfile
ChDir "C:\Temp"
MyFile = Dir("*.doc")
Do
oDoc.Selection.InsertFile FileName:=MyFile, Link:=False
MyFile = Dir
Loop Until MyFile = Empty
End Sub
 
D

Dick Kusleika

Jocke

What do you mean by "not accept"

This works

Sub test()

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim sPath As String
Dim sFile As String

sPath = "C:\Dick\NG\01 Jan\"

Set wdApp = New Word.Application
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add

sFile = Dir(sPath & "*.doc")

Do While Len(sFile) > 0
wdDoc.Range.InsertFile sPath & sFile, , , False
sFile = Dir
Loop

End Sub

Maybe it has something to do with the default path. Try storing the path in
a variable like I do instead of using ChDir.
 

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