How Can I Insert Files into a Word Document?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Awhile back you showed people how they could combine several text files into a single file. I’d like to do the same thing in Word. How can I insert files into a Word document?

— HK

SpacerHey, Scripting Guy! AnswerScript Center

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):

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

objSelection.TypeText “Hardware Inventory” objSelection.TypeParagraph() objSelection.InsertFile(“C:\Scripts\Hardware.txt”)

objSelection.TypeParagraph()

objSelection.TypeText “Software Inventory” objSelection.TypeParagraph() objSelection.InsertFile(“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:

Set objWord = CreateObject(“Word.Application”)
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
Set objSelection = objWord.Selection

That was easy, but now it gets even easier. We want a heading for our hardware inventory so we type he heading Hardware Inventory followed by a paragraph return; that’s what we do here:

objSelection.TypeText “Hardware Inventory”
objSelection.TypeParagraph()

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:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set objWord = CreateObject(“Word.Application”) objWord.Visible = True Set objDoc = objWord.Documents.Add() Set objSelection = objWord.Selection

Set FileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Scripts\Archive’} Where ” _ & “ResultClass = CIM_DataFile”)

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.)

0 comments

Discussion is closed.

Feedback usabilla icon