Automatic Print to File VB.NET

Tech-Archive recommends: Speed Up your PC by fixing your registry



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

.



Relevant Pages

  • Re: Trouble with getting latest file
    ... Dim FolderName As String ... Dim FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Re: Trouble with getting latest file
    ... I also need to check that I will get the filename of the file with the ... Dim The_FileName As String ... If CurrentDateStamp> LatestDateStamp Then ...
    (microsoft.public.access.modulesdaovba)
  • Need Help with Inserting in SQL
    ... Public logoTable As String ... Dim logoTable As String ... Function InsertLogoToDataBase(ByVal FileName As String) As Variant ... Function GetAllFilesInDir(ByVal strDirPath As String) As Variant ...
    (microsoft.public.excel.programming)
  • Re: Dateidownload per WebRequest (async)
    ... ByVal DestinationDirectory As String, _ ... Dim Response As WebResponse = Request.GetResponse ... If ContentDisposition IsNot Nothing Then ... If Param Is "filename" Then ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: Automatic Print to File VB.NET
    ... which you need to specify the path and filename. ... Public Function SetFileToPrintPort(ByVal filename As String) As Boolean ... Dim regkey As Microsoft.Win32.RegistryKey ... Dim GenPr As Boolean = False ...
    (microsoft.public.dotnet.framework.drawing)