Printing a document from VB - vb.net

I have an app which monitors a network location for new documents.
They can be word documents, PDF files, spreadsheets etc.
When a document is detected, it is copied to a local folder within c:\Temp.
What I need is for the document, once copied, to be sent to a specified (not default) printer.
Has anyone got any ideas on how I can achieve this?
Thanks!!

You may need to create a variety of functions to print your document. Like using LPR to print PDF or PostScript, Microsoft.Office.Interop for Word and Excel documents, and so on...
If it were me, I would use System.IO.Path.GetExtension(filename) to determine the file type and then call whichever function was most appropriate...or log that the file was not printable if the format was not handled.
Microsoft.Office.Interop
Using Microsoft.Office.Interop.Excel, you can call the PrintOutEx method on a Workbook or Worksheet and specify which printer to use.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.worksheets.printoutex.aspx
and
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._workbook.printoutex.aspx
With Microsoft.Office.Interop.Word you can set the ActivePrinter property of the Application and then use the PrintOut method on the Document.
See:
https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._application.activeprinter.aspx
and https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.printout.aspx
LPR
I wrote a tool once that prints PDF and PostScript files. The code was something like:
'Set the IP address of the printer to use.
If printer1 Then
printserver = printer1Address
ElseIf printer2 Then
printserver = printer2Address
ElseIf printer3 Then
printserver = printer3Address
End If
'Use LPR to print the file.
Dim lprProcess As New Process()
With lprProcess.StartInfo
.UseShellExecute = False
.FileName = "CMD"
.Arguments = "/c LPR -s " & printserver & " -P P3 " & myNewFileName
End With
lprProcess.Start()
LPR is not a included by default in Windows 7 and above, but it is a cake-walk to turn on. See steps 1-3 on http://campus.mst.edu/cis/desktop/documentation/pc/win7_x64/lpr_printer/install.htm

Related

How to print PDF with Not default printer with code (VB Net)

I´m trying to print a pdf file, there is no problem to print it with the default printer but when I want to do it with a secondary printer it still printing with the default one.
This is my code:
Dim MyProcess As New Process
MyProcess.StartInfo.CreateNoWindow = False
MyProcess.StartInfo.Verb = "print"
'HERE IS WHERE I WANT TO CHANGE THE PRINTER (BUT THIS COMMAND IS IGNORED)
MyProcess.StartInfo.Arguments = "Canon MG3500 series"
MyProcess.StartInfo.UseShellExecute = True
MyProcess.StartInfo.FileName = My.Application.Info.DirectoryPath & "\Copias digitales\Temp.pdf"
MyProcess.Start()
MyProcess.WaitForExit(10000)
MyProcess.CloseMainWindow()
MyProcess.Close()
How can I make it?
Thank you all.
Use Printer Settings.Printername under the Namespace System.Drawing.Printing
then specify the name of your Printer ( "Canon MG3500 series")
The PrinterSettings control is used to configure how a document is printed by specifying the printer.source
This should do it.
note: This also allows you to use your applied settings on your printer (
i.e. Paper size
I use this everytime I need to use two printers
i.e one for the O.R. then another for a document
Here is a good example of using this along with a combobox to choose any printer in the network.
I found the solution replacing the bad line with:
Shell(String.Format("rundll32 printui.dll,PrintUIEntry /y /n ""{0}""", "Printer name"))

Printing a .ZPL file to a zebra printer in vb.net. Visual Studio 2015 version

I already have the raw ZPL file ready to go, I just don't know how to set the printer I want to send it to and then send it. How would I do this?
Note: I have a batch script on my computer that I default all ZPL files to, which is a shell script that sends the file to the thermal printer on my computer. I want to get away from that and have all the commands within my application so I don't have to use an external script like that.
This is the code I have now that when ran it auto opens with my batch script:
Sub SaveLabel(ByRef labelFileName As String, ByRef labelBuffer() As Byte)
' Save label buffer to file
Dim myPrinter As New PrinterSettings
Dim LabelFile As FileStream = New FileStream(labelFileName, FileMode.Create)
LabelFile.Write(labelBuffer, 0, labelBuffer.Length)
LabelFile.Close()
' Display label
DisplayLabel(labelFileName)
End Sub
Sub DisplayLabel(ByRef labelFileName As String)
Dim info As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo(labelFileName)
info.UseShellExecute = True
info.Verb = "open"
info.WindowStyle = ProcessWindowStyle.Hidden
info.CreateNoWindow = True
System.Diagnostics.Process.Start(info)
End Sub
And this is my batch script:
copy %1 \\%ComputerName%\Zebra
To replicate the exact functionality of the batch file in VB.net:
Dim filename As String = System.IO.Path.GetFileName(labelFileName)
System.IO.File.Copy(
labelFileName,
Environment.ExpandEnvironmentVariables("\\%ComputerName%\Zebra\" & filename))
This copies the file using the method provided by the System.IO namespace. It also expands the %COMPUTERNAME% environment variable. This replaces all the code in the DisplayFile subroutine.

Manipulate visio shapes with data passed from vb.net

I have the below code which is opening visio, opening a file in visio, printing the file and then closing; this all works fine.
However, now my task is to pass information over to the visio document page called 'Ticket Task', bind that information to some shapes and then print it.
I know this is possible with vb6 (that is what the outdated code is written in), however is there a way to do this in vb.net?
Thanks!
Code:
''Set up the file path
Dim docPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\" + splFileData(0) + "\" + splFileData(1) + ".vsd"
'Set up the attributes for the opening/printing of the document.
psi.UseShellExecute = True
psi.Verb = "print"
'psi.EnvironmentVariables.Add("Orders", "Hello")
psi.Arguments = printer
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = docPath
Console.WriteLine("Printing: " + docPath)
'Start the process (open visio document, print, close)
Process.Start(psi)
You'll have to change the way you're opening and printing Visio documents. This tutorial shows how to use VB.NET to work with documents. The Open function returns an object of type Microsoft.Office.Interop.Visio.Document. You can use this object to attach information to shapes as discussed in here. In fact, the VB.NET code is very similar to VB6. If you want Visio to be invisible, you can open the document as follows:
Microsoft.Office.Interop.Visio.InvisibleApp application = new Microsoft.Office.Interop.Visio.InvisibleApp();
application.Visible = false;
Microsoft.Office.Interop.Visio.Document doc = application.Documents.Open...

How to send string variable to commands prompt using vba excel?

I'm trying to send a string variable to command prompt using vba excel.
I'm using following code:-
code_name = "xyz.c"
version = "def"
label = "1XDFO"
'open command prompt
Dim oWsc As Object
Set oWsc = CreateObject("WScript.Shell")
Dim oExec As Object
'save config spec in a text file
Set oExec = oWsc.Exec("perl F:\My_work\hello_world.pl code_name version label")
While oExec.Status <> 1 ' Wait for process
Sleep 1000
Wend
While calling perl script at line
"oWsc.Exec("perl F:\My_work\hello_world.pl code_name version label")"
i want to send original contents of string variable code_name,version,label; but its sending the variable names as it is and not the content; Can anyone help me with this?
Thanks in advance.
How can an external script (Perl or any other one) understand that what you are sending are variables and read their content? You have to send just the content:
Set oExec = oWsc.Exec("perl F:\My_work\hello_world.pl " & code_name & " " & version & " " & label)
Clarification 1: VBA or .NET or any other Microsoft language, do not "read variables inside strings", like other languages do, PHP for example.
Clarification 2: even a language capable of reading variables inside strings cannot manage what is being done "outside its framework". If you call a PHP script from an external program, you cannot call it as you are doing now because PHP does not have any control on what the external program does and thus on its variables.

How to concatenate .pdf files with VBA?

I am trying to concatenate .pdf files with VBA. Nothing fancy, literally sticking the pages one after each other. I did numerous web searches but was unable to find any solutions that are even close to working. Has anyone done this before? Thanks!
If a GPL library is a valid option for you, you could use ghostscript as proposed in this SO question. You can do this by calling the ShellExecute function from Windows API or by using the class WScript.Shell if you are creating a vbscript file.
If a commercial library is an option, I recommend Amyuni PDF Creator ActiveX or Amyuni PDF Converter, both have an Append function that will do the work. The code for Amyuni PDF Converter for example would look like this:
Set PDFDoc = CreateObject("CDintfEx.Document.4.5")
PDFdoc.SetLicenseKey "your company", "your license code"
PDFDoc.Open "test_append1.pdf"
PDFDoc.Append "test_append2.pdf"
PDFDoc.Save "result_append.pdf"
Set PDFdoc = Nothing
Usual disclaimer applies for the latest suggestion
I found this topic while having the same task, with a customer using amyuni. Thanks to yms for a good approach. I found Acces crashing on "Set PDFdoc = Nothing". This one works fine for me:
Public Sub fctPDO_Concatenate_pdf_with_Amyuni_Document_6_0()
' PDO: Usage of .append: Crashes on destruction of pdfdoc-Object. pdf-file is created properly. But usind .append , MS Access crashes - without it's okay.
' Solution: Build second pdfdoc2 - object , and concatenate using .AppendEx(Object) .
On Error Resume Next
Dim PDFdoc As Object
Dim PDFdoc2 As Object
Const strLibraryVersion As String = "CDintfEx.Document.6.0"
' PDO: Examples
'Set PDFdoc = CreateObject("CDintfEx.Document.6.0") ' PDO: See Object catalog
'Set PDFdoc = CreateObject("CDintfEx.Document") ' PDO: Not sufficient w/o version
'Set PDFdoc = CreateObject("CDintfEx.Document.4.5") ' PDO: Older version
Set PDFdoc = CreateObject(strLibraryVersion)
Set PDFdoc2 = CreateObject(strLibraryVersion)
'PDO: Open first file
PDFdoc.Open "D:\PDO_test\Beratungsprotokoll_2018.pdf"
'PDFdoc.Append "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf" ' PDO: Crashes on set PDFdoc = nothing
' PDO: Open and append second file (as Object, not as file)
PDFdoc2.Open "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf"
PDFdoc.AppendEx PDFdoc2
' PDO: Open and append third file (as Object, not as file). Re-use of second Object possible
PDFdoc2.Open "D:\PDO_test\result_append_sammel.pdf"
PDFdoc.AppendEx PDFdoc2
'PDO: Save with a new name
PDFdoc.Save "D:\PDO_test\result_append_sammelsammel.pdf"
'PDFdoc.Close => Not existing.
Set PDFdoc = Nothing '=> Access crashed, with PDFdoc.Append
Set PDFdoc2 = Nothing
Debug.Print "Done: " & Now() & " Error: " & Err.Number
End Sub
If you prefer Ghostscript you can use a single line:
C:\PROGRA~2\gs\gs9.19\bin\gswin32c.exe -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOwnerPassword=pass2 -sUserPassword=pass1 -dCompatibilityLevel=2.0 -sOutputFile="D:\PDO_test\Beratungsprotokoll_2018_Sammel.pdf" "D:\PDO_test\Beratungsprotokoll_2018.pdf" "D:\PDO_test\GV_0093Z0_Einzelantrag.pdf"
This concatenates the two latter files into the (new) first one and applies a password (see security details before applying). The short path can be obtained with a FileScripting Object "fso" using
fso.GetFolder(strFolder).ShortPath
I run sedja-console and add my pdf's as parameters. Quite easy to implement. Do not forget to check before starting Sedja-console if the readonly flag of the possible previous created destination pdf isn't set to yes, as there is no feedback of this external process.