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

Automatic Print to File VB.NET

227 views
Skip to first unread message

kraeven

unread,
Jan 17, 2008, 8:25:09 AM1/17/08
to
Hi guys,

I have been looking for a few days, but no one seems to know how to print to
a file with automatic file renaming.

I made an application in VB.NET 2005 that prints pdf visio and project files.
Select the files or folders, select a printer and the applications are
executed with the correct print command.

We have a Generic PostScript Printer, which prints to a ps file in a certain
folder.
This folder is watched by a service that converts this ps file to a pdf file.

But when printing to the Generic PostScript Printer, a dialog box pops up in
which you need to specify the path and filename. To avoid this box from
popping up and changing the filename from within the code, after digging
around I managed to accomplish this.

Imports System.Runtime.InteropServices
Imports System.ServiceProcess

Public Function PrintToFile(ByVal filename As String) As Boolean
filename = Microsoft.VisualBasic.Right(filename, filename.Length -
InStrRev(filename, "\"))
filename = Microsoft.VisualBasic.Left(filename, InStrRev(filename, ".
"))
filename = PSin + "\" + filename + "ps" 'PSin is a variable (L:\in)
PostScriptPort = filename
End Function


Public Function SetFileToPrintPort(ByVal filename As String) As Boolean
Dim regkey As Microsoft.Win32.RegistryKey
regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\
Microsoft\Windows NT\CurrentVersion\Ports", True)
regkey.SetValue(filename, "", Microsoft.Win32.RegistryValueKind.
String)
regkey.Close()
myController = New ServiceController("Spooler")
Dim sStatus As String
myController.Refresh()
sStatus = myController.Status.ToString

If myController.CanStop Then
myController.Stop()
Else
MsgBox("Spooler service cannot be stopped")
End If

Try
myController.Start()
Catch exp As Exception
MsgBox("Could not start spooler service")
End Try
myController.Refresh()

myController.WaitForStatus(ServiceControllerStatus.Running)

Shell("rundll32.exe printui.dll,PrintUIEntry /Xs /n " + Chr(34) +
"Generic PostScript Printer" + Chr(34) + " PortName " + Chr(34) + filename +
Chr(34), AppWinStyle.Hide, True)

End Function


Public Function RestoreGeneric(ByVal filename As String) As Boolean
Dim rk As Microsoft.Win32.RegistryKey

Shell("rundll32.exe printui.dll,PrintUIEntry /Xs /n " + Chr(34) +
"Generic PostScript Printer" + Chr(34) + " PortName " + Chr(34) + "FILE:" +
Chr(34), AppWinStyle.Hide, True)

rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\
Microsoft\Windows NT\CurrentVersion\Ports", True)
rk.DeleteValue(filename, True)
rk.Close()
End Function

Private Sub Startup()
Dim item As String
Dim GenPr As Boolean = False
For Each item In ListbVisio.SelectedItems
If CbPrinters.SelectedItem = "Generic PostScript Printer" Then
GenPr = True
End If

If GenPr Then
PrintToFile(item)
SetFileToPrintPort(PostScriptPort)
End If

Shell(VisioAppPath + " /pt " + Chr(34) + item + Chr(34) +
"," + printer, AppWinStyle.Hide, True) 'MS Visio commandline Print commando

If GenPr Then
RestoreGeneric(PostScriptPort)
End If
Next
End Sub

kraeven

unread,
Jan 17, 2008, 9:04:51 AM1/17/08
to
Left out something

Dim myController As ServiceController (in the SetFileToPrintPort function)
srry ;-)

0 new messages