INSERTTEXT field help

J

John

In Excel I have a list of file paths for a large number of word documents.

What I would like to do is have the field INSERTTEXT to look at the cells in
excel and then insert the text to word.

I would also like a table of contents to be created after they have been
included.

thanks for any help

John
 
G

Graham Mayor

You can do this with a macro If you have not already done so add a reference
to Microsoft Excel in vba tools > references


Sub ReadExcelWB()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim tCell As String
Dim rCount As Long
Dim xlName As String

xlName = "D:\My Documents\Test\ExcelWB.xls"
Documents.Add
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open(xlName)
rCount = 1
With xlWB.Worksheets(1)
While Cells(rCount, 1).Formula <> ""
tCell = Cells(rCount, 1).Formula
With ActiveDocument
Selection.InsertFile FileName:=tCell, Range:="", _
ConfirmConversions:=False, _
Link:=False, Attachment:=False
End With
rCount = rCount + 1
Wend
End With
xlWB.Close False
xlApp.Quit
Set xlWB = Nothing
Set xlApp = Nothing
End Sub

will read a list of documents from column 1 of "D:\My
Documents\Test\ExcelWB.xls" and insert them in order into a new document.
You can add the code to insert an appropriate TOC where you require it at
the end of the macro e.g.

With Selection
.HomeKey Unit:=wdStory
With .Fields
.Add Selection.Range, wdFieldTOC, "\o ""1-3"" \h \z \u", False
.Update
End With
.InsertBreak Type:=wdSectionBreakNextPage
End With
ActiveWindow.View.ShowFieldCodes = False

..
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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