Trouble Printing Word Documents from Folder using VBS

T

Thereal Robeen

I am trying to automate the printing of documents from a specified folder on
our server. I am trying to write a VB Script to do this and run it as a
scheduled task to check the folder every minute for any files in there, print
them, then delete them. Here's what I have so far, I have only begun testing
the Word doc portion.

...............<BEGIN CODE>.................
Set objShell = CreateObject ("Shell.Application")
Set objFolder = objShell.Namespace ("M:\IMSU\PrintFolder")

For Each strFileName in objFolder.Items
If objFolder.GetDetailsOf (strFileName, 2) = "Microsoft Word Document" Then
Set objWord = CreateObject("Word.Application")
set docWord = objWord.Document.Open(strFileName)
objWord.ActiveDocument.Printout Background:=False
objWord.ActiveDocument.Close saveChanges:=wdDoNotSaveChanges
objWord.Quit
elseif objFolder.GetDetailsOf (strFileName, 2) = "Microsoft Excel
Workbook" Then
'Future Code
elseif objFolder.GetDetailsOf (strFileName, 2) = "Microsoft PowerPoint
Presentation" Then
'Future Code
else 'Print other files if not office docs
objFolder.Item("M:\IMSU\PrintFolder\" &
strFileName).InvokeVerbEx("Print")
end if

Next

Const DeleteReadOnly = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("M:\IMSU\PrintFolder\*.*"), DeleteReadOnly
....................<END CODE>...................

The Delete portion works OK, but I keep getting a Windows Script Host
'Expected Statement' Compile Error on the PrintOut line. Any help would be
greatly appreciated. Thank you.
 
J

Jay Freedman

I think the problem is that VBScript doesn't support named parameters or the
use of the := operator. You'll have to change any statements that use
parameters to pass their values by position. In the case of the PrintOut and
Close methods, the parameters you want to pass are the first on for each
method, so you can just include the value.

Also, unless you define them within the script, VBScript knows nothing about
the values of constants defined in the Word object model. 'False' is the
same in both languages, but you need to use 0 for wdDoNotSaveChanges. I
think you'll be OK with these two lines substituted:

objWord.ActiveDocument.Printout(False)
objWord.ActiveDocument.Close(0)
 
T

Thereal Robeen

Thanks Jay, that worked perfectly.

Jay Freedman said:
I think the problem is that VBScript doesn't support named parameters or the
use of the := operator. You'll have to change any statements that use
parameters to pass their values by position. In the case of the PrintOut and
Close methods, the parameters you want to pass are the first on for each
method, so you can just include the value.

Also, unless you define them within the script, VBScript knows nothing about
the values of constants defined in the Word object model. 'False' is the
same in both languages, but you need to use 0 for wdDoNotSaveChanges. I
think you'll be OK with these two lines substituted:

objWord.ActiveDocument.Printout(False)
objWord.ActiveDocument.Close(0)
 

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