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

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.

Related

How to use VBA to open a file in a new window?

I have a OSI PI Processbook file that I am using VBA on to open a new instance of PI Processbook. Basically I have a text element (Text35) on which I have a vba click event:
Private Sub Text35_Click(ByVal lvarX As Long, ByVal lvarY As Long)
Dim filePathAndName As String
Dim exeLocation As String
Dim PID As Variant
On Error GoTo errHandle
filePathAndName = "C:\Users\myuser\Desktop\TEST.PDI"
exeLocation = "C:\Program Files (x86)\PIPC\Procbook\Procbook.exe"
PID = Shell("""" & exeLocation & """", vbNormalFocus)
'How can I use the above process id to open a PDI file??
Exit Sub
errHandle:
End Sub
Basically I want to open the TEST.PDI file on my desktop in an entirely new copy of PI Processbook. I've tried pplication.Displays.Open(filePathAndName, True) but this opens my TEST.PDI into the same instance of Processbook, not a new application instance like I want.
Is there some Shell command or command like switch argument I can use to both open a new instance of the .exe and open up a file at the same time? I, at least, have the process id of the new instance stored in the "PID" variable, so I am thinking this might help.

Writing to Command Line Not Working

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:
Public Class MyClass
Dim StreamWriteUtility As System.IO.StreamWriter
Dim StreamReadUtility As System.IO.StringReader
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process
Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
Dim ProcessInfo As ProcessStartInfo
Dim Process As New Process
ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
ProcessInfo.RedirectStandardInput = True
Process = Process.Start(ProcessInfo)
SWUtility = Process.StandardInput
While True
SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
End While
End Sub
End Class
This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.
Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

Upload and replace the same image file if it already exist

I have a picturebox named PB_Company_Logo and I have a button named btn_Save and within this button I have this function which saves the image in PB_Company_Logo to current_directory/images
Public Sub save_PB(PB_Name As PictureBox)
Dim filename As String = "company_logo.png"
Dim path As String = Directory.GetCurrentDirectory() & "\images"
Dim filename_path As String = System.IO.Path.Combine(path, filename)
If (Not System.IO.Directory.Exists(path)) Then
System.IO.Directory.CreateDirectory(path)
PB_Name.Image.Save(filename_path)
Else
PB_Name.Image.Save(filename_path)
End If
End Sub
The problem is, there are cases where the user will upload a new company_logo.png. I want the system to treat the uploading of new image as replacing the former company_logo.png.
I think the error in this line of code means that the file is currently in used (locked) and therefore cannot be replaced.
Else
PB_Name.Image.Save(filename_path)
End If
When you load a pictureBox control with an image file the ide (vs) puts a lock on the file. This happens when you set the image property of the pictureBox control to a file at design time.
You can use the FileStream object.
Example:
Dim fs As System.IO.FileStream
' Specify a valid picture file path on your computer.
fs = New System.IO.FileStream("C:\WINNT\Web\Wallpaper\Fly Away.jpg",
IO.FileMode.Open, IO.FileAccess.Read)
PictureBox1.Image = System.Drawing.Image.FromStream(fs)
fs.Close()
After you have done the workaround you can then use the System.IO.File.Exists(img) namespace to check wether or not a picture exists.
Example: This checks if the image passed through already exists and if it does then it will replace it.
Dim imageStr As String = "~/images/name.jpg"
If System.IO.File.Exists(imageStr) Then
Image1.ImageUrl = "~/images/name.jpg"
End If

Printing a document from VB

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

how to open selected files through the default application of the file in VB 2010?

I have Windows application written in VB 2010.Here, user may select any file from open dialog.So, I want to open the file in corresponding application.for example, suppose user selecting docx file then i have to open the file using msword,suppose,if it is a pdf file, then i have to open with adobe reader or available pdf reader(default application).
Is this possible to do?
Shell and the Windows API CreateProcess() are for starting executable files.
If you're loading a document/file then these are handled by ShellExecute() and can be initiated in .NET using the Process.UseShellExecute property:
Private Function ShellExecute(ByVal File As String) As Boolean
Dim myProcess As New Process
myProcess.StartInfo.FileName = File
myProcess.StartInfo.UseShellExecute = True
myProcess.StartInfo.RedirectStandardOutput = False
myProcess.Start()
myProcess.Dispose()
End Function
Taken from the #VB wiki.
Try this:
now with openfiledialog
Dim OpenFileDlg as new OpenFileDialog.
OpenFileDlg.FileName = "" ' Default file name
OpenFileDlg.DefaultExt = ".xlsx" ' Default file extension
OpenFileDlg.Filter = "Excel Documents (*.XLSX)|*.XLSX"
OpenFileDlg.Multiselect = True
OpenFileDlg.RestoreDirectory = True
' Show open file dialog box
Dim result? As Boolean = OpenFileDlg.ShowDialog()
' Process open file dialog box results
for each path in OpenFileDlg.Filenames
Try
System.Diagnostics.Process.Start(Path)
Catch ex As Exception
MsgBox("Unable to load the file. Maybe it was deleted?")
End Try
If result = True Then
' Open document
Else
Exit Sub
End If
next
This will work if the file is registered with the OS. Use Try catch because it can throw errors if the file is in use.
Edit: It uses always the default application.