Hey, HK. Believe it or not it’s actually easier to insert files into a Microsoft Word document than it is to insert a text file into another text file. That’s because the Word Selection object has a method - InsertFile - that does one thing and one thing only: it opens up a specified file and inserts it into the current document. And InsertFile isn’t limited to opening just text files: if it’s a type of file that Word can handle, then InsertFile can open it and insert it in your document.

Let’s take a look at a script that imports a pair of text files (C:\Scripts\Hardware.txt and C:\Scripts\Software.txt):

Like we said, way easier than trying to combine multiple files in a single text file. We begin by creating an instance of the Word.Application object and setting the Visible property to True; we then add a document and create an instance of the Selection object. That’s what these lines of code are for:

And now we’re ready to insert a file. That takes just one line of code:

objSelection.InsertFile("C:\Scripts\Hardware.txt")

That’s it: you call the InsertFile method followed by the path of the file being inserted into the document. We then add another paragraph return and the heading Software Inventory, and then insert our second file, C:\Scripts\Software.txt. It’s so easy we could sit here all day inserting files into Word documents!

Before we do that, however, let’s show you a handy little variation on the preceding script. This one creates a new Word document and then inserts all the files found in the folder C:\Scripts\Archive. We won’t bother explaining the code line-by-line, but you should be able to figure out most of it yourself:

For Each objFile In FileList
objSelection.InsertFile(objFile.Name)
objSelection.TypeParagraph()
Next

That is cool, isn’t it? For more exciting scripts that use Microsoft Office applications take a peek at the Office Space column located elsewhere in the Script Center. (The preceding advertisement was paid for by the Committee to Get More People to Read the Office Space Column.)