ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Control Word through Excel (https://www.excelbanter.com/excel-programming/332742-control-word-through-excel.html)

Jocke

Control Word through Excel
 
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

Dick Kusleika[_4_]

Control Word through Excel
 
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.

--
Dick Kusleika
Excel MVP
Daily Dose of Excel
www.dicks-blog.com

Jocke wrote:
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





All times are GMT +1. The time now is 04:20 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com