Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with VBS Script 1

Status
Not open for further replies.

CBTguy

Technical User
Dec 16, 2007
3
US
I sent this email to Microsoft's VBS Scripting guy, but I never heard back. I want to grab multiple documents (jpegs,word etc,) and automatically insert them into another Word document. The Script guy provided a script to do so, but it didn't work. Thought maybe someone could eye ball the problem. Here's the email I sent to the script guy:

I tried to follow the instructions for inserting multiple files into an MS Word document, but I encountered a problem (actually I found the article in a Nov. '05 article, which also referred to a May '05 article about this).

[ Here's the link: ]

The script ( which is named: mergewrd.vbs) did run, it then opened up MS Word, but then it stops. Nothing happens.

The command line, after running, shows this:

C:\Documents and Settings\Administrator.LAPSYCH\mergewrd.vbs(15, 1) (null): 0x8004103A

The folder's name is 'sun.' In this folder are around 10 jpg's. I know they can be inserted, because I can manually open the jpg's and then insert each one, one by one into MS Word. But obviously, that's tedious, which is why I'm trying this script. I'm not as familiar with the variables that I might need to be changing to fit my computer, but I couldn't identify any unique variables based on your example.

Here's the code, directly taken from your article, but only the file name altered:

Const wdPageBreak = 7

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:\sun} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile in FileList
objSelection.InsertFile(objFile.Name)
objSelection.InsertBreak(wdPageBreak)
Next


____________________________

So... any idea what's going wrong here???

Thanks for any help,

Dan
 
("ASSOCIATORS OF {Win32_Directory.Name='C:\sun[!]'[/!]} Where " _


Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
OK, PHV,

Thanks! That worked. Sort of.
I have a bunch of jpegs which I want to transfer automatically into a word document.
The script now 'works'(always amazed at a how a small apostrophe can fix things).
HOWEVER, while the script is now correctly inserting other -WORD- documents, it is inserting jpeg's as scrambled characters (probably hexadecimals). Any idea how to make sure this script keeps the jpegs AS jpegs when it inserts them into word??

I bow to the Tek Tip genius.

Dan

 
If you're going to be looping through files in a folder I would use FileSystemObject instead of WMI to begin with.

See if this works for you...

Code:
Option Explicit

Const wdPageBreak = 7

Dim strDir : strDir = "C:\temp"

Dim objWord : Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Dim objDoc : Set objDoc = objWord.Documents.Add()
Dim objSelection : Set objSelection = objWord.Selection

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder : Set objFolder = objFSO.GetFolder(strDir)
Dim objFile, strFileName
For Each objFile In objFolder.Files
	strFileName = UCase(objFile.Name)
	If Right(strFileName, 3) = "DOC" Then
		objSelection.InsertFile(objFile.Path)
		objSelection.InsertBreak(wdPageBreak)
	ElseIf Right(strFileName, 3) = "JPG" Then
		objSelection.InlineShapes.AddPicture objFile.Path, False, True
		objSelection.InsertBreak(wdPageBreak)
	End If
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Dear DM4Ever,

You d'man!

It worked like a charm!!!

Amazing. A beauty to behold.

Thanks to both of you (PHV) for your input!

Dan
 
I'm glad that worked for you.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top