Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printer object in vbscript

1,030 views
Skip to first unread message

PC

unread,
Oct 16, 2006, 8:57:21 AM10/16/06
to
how can i print to the default printer in vbscript
example (in vb6):
For i = 0 To 1000 Step 100
Printer.Line (i, 0)-(i, 1000)
Next


Tom Lavedas

unread,
Oct 16, 2006, 4:42:19 PM10/16/06
to
In short, there is no 'print' object available to script.

However, if your default printer is attached to the LPT1 (or another
local port) you may be able to send simple, unformatted output to the
printer by opening that port and writing to it. For example, ...

with createobject("scripting.filesystemobject")
set printer = .opentextfile("LPT1",2)


For i = 0 To 1000 Step 100

Printer.WriteLine i
Next
end with

Newer printers may not support this and the output will be ugly.

Another common is to write the data to a temporary file and then use
notepad to send the data to the printer, as in ...

const wait = true
hideWindow = 0
tempName = "tempfile.txt"
with createobject("scripting.filesystemobject")
set tempfile = .opentextfile(tempName,2)


For i = 0 To 1000 Step 100

tempfile.WriteLine i
Next
tempfile.close
createobject(wscript.shell").run "notepad /p " &
"tempfile.txt",hideWindow ,Wait
tempfile. delete
end with

There are other techniques documented - try a google.groups search of
this group or better yet "microsoft.public.scripting.*".

Tom Lavedas
=============
http://members.cox.net/tglbatch/wsh

mr_unreliable

unread,
Oct 20, 2006, 4:15:24 PM10/20/06
to
hi PC,

If you have a (vb) compiler available, it is a simple
matter to write an actX object which will pass the
visual basic printer object across to your script.

In fact here is some (air) code:

Dim m_Printer As Printer ' (at module level)

Public Property Get vbPrinterObject() As Object

' select a printer (in this case the default printer)...
Set m_Printer = Printer

' pass the default printer back to vbs...
Set vbPrinterObject = m_Printer
End Property

Thanks to the magic of com interfaces you can exercise
_most_ of the printer methods and properties directly
from script (i.e., without writing separate interfaces
for them).

Unfortunately, the one you asked about, i.e., the "Line"
method is not one that can be accessed directly from
script. The reason is that the parameters are "non-
standard", those crazy parentheses which are part of
the parameters, and which will cause vbs to throw up
when it sees them.

And so, "line" will need to be "wrapped":

Public Sub DrawLine(X1 As Variant, Y1 As Variant, _
X2 As Variant, Y2 As Variant)

m_Printer.Line (CSng(X1), CSng(Y1))-(CSng(X2), CSng(Y2))

End Sub


cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)

Reventlov (see signature)

unread,
Oct 22, 2006, 4:18:42 PM10/22/06
to
Il giorno Mon, 16 Oct 2006 14:57:21 +0200, "PC" <On...@pandora.be> ha scritto:
>how can i print to the default printer in vbscript

Create your text in a hidden MSWord window and print it.

set oWord=createobject("word.application")
' Costanti di word per l'allineamento del testo.
const wdAlignParagraphCenter=1
const wdAlignParagraphLeft=0
const wdAlignParagraphRight=2
const wdAlignParagraphJustify=3
with oWord
.documents.add
.Selection.Font.Name = "Arial"
.Selection.Font.Size = 20
.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Selection.TypeText "Title"
.Selection.TypeParagraph
.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
.Selection.Font.Name = "Garamond"
.Selection.Font.Size = 12
.Selection.TypeText "asfasdfasdfasdfasdf"
.Selection.TypeParagraph
.Selection.TypeText "asfasdfasdfasdfasdf"
.Selection.TypeParagraph
.Application.PrintOut
'.ChangeFileOpenDirectory "c:\data"
'.ActiveDocument.SaveAs "temp.doc"
'.ActiveDocument.Close
end with
wscript.quit


--
Giovanni Cenati (Aosta, Italy)
Write to user "Reventlov" and domain at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--

0 new messages