尊重原创:Vb.net 调用Codesoft-LabelManager2 打印标签
源码下载地址:二次开发
最近在用VB.net 做一个项目,其中一项就是标签打印,
用了好多种方法,都不怎么理想,
1.Zebra zpl 命令这个方法最有效,但2D条形码搞不定。希望能搞定的朋友分享一下...
还有就是同一程序用在多台打印机如果打印机分辨率不同,品牌不同都不能打印。
2.Bartender开发太贵,老板让省钱...
3.zebraDesigner不能在vb.net 下调用开发...
最后决定用Codesoft 做模板,然后用Vb.net 调用LabelManager2.
步骤:
1.找个破解版的Codesoft安装,
2.然后找到Lppx2.tlb文件,在vb.net 下引用
Imports LabelManager2
Public CSapp As LabelManager2.Application
Public CSdoc As LabelManager2.Document
Public CSvars As LabelManager2.Variables
Dim label_dt As DataTable
Public Function ServerStart() As Boolean 'lab_object建立及error检测
Dim LastErr&
'On Error Resume Next ' catch errors
CSapp = New LabelManager2.Application 'implements object
'Set MYDOC = MyApp.ActiveDocument
LastErr = Err.Erl ' store resulting error code
On Error GoTo 0 ' returns to normal error trapping
Select Case LastErr ' depending on error code...
Case 0 ' no error,return true
ServerStart = True
Case 429 ' OLE common error,display special message
MsgBox("Cannot find or start OLE server,please check its registration.",vbCritical)
Case Else ' for other errors,use VB error processing
Err.Raise(LastErr)
End Select
End Function
Public Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Catch
MessageBox.Show("无法释放 " & o & " 物件")
Finally
o = Nothing
End Try
End Sub
Private Sub CS_Print_Label(ByVal label_path As String)
If ServerStart() = True Then
CSdoc = CSapp.Documents.Open(label_path,False) 'lable档的object
CSvars = CSdoc.Variables
If CSvars Is Nothing Then
MessageBox.Show("CS Lalel doesn't exsit")
Exit Sub
End If
Dim Other_str,Lan_Model As String
Other_str = Me.TextBox_Other.Text.ToString.ToUpper
Lan_Model = Me.ComboBox1.Text.ToString
‘这里是给Dt赋值,动态调用数据库数据
sql = "Select * Form tabel_name"
label_dt = Run_sql(sql).Tables(0)
If label_dt.Rows.Count = 0 Then
Info_Msg_Box("数据不存,不能打印!")
Return
End If
'选择打印机,根据你的需要,可省略
CSapp.Dialogs.Item(enumDialogType.lppxPrinterSelectDialog).Show()
'填充器的value
'注意了,上面是给dt赋值,在这里要把dt 的数据传给标签,
'我这里用的是For 循环赋值三行就搞定,切记---dt子段名称必须和label 子段名一致,否则赋值不成功。
'如果你不想用循环也可以,那就这样写吧,CSdoc.Variables.FormVariables.Item("字段名").Value=label_dt.Rows(0)("字段名")
'不过有20行,你就要写20行了!
For i As Integer = 1 To CSdoc.Variables.FormVariables.Count
CSdoc.Variables.FormVariables.Item(i).Value = label_dt.Rows(0)(CSdoc.Variables.FormVariables.Item(i).Name).ToString
Next
'列印一张
CSdoc.PrintDocument(1)
CSdoc.FormFeed() '结束列印
CSdoc.Close(True)
'全部关闭
CSapp.Documents.CloseAll()
CSapp.Quit()'这句很重要,不然会lppa.exe进程关不掉,打印一次多一个
'CSdoc = Nothing
'CSvars = Nothing
NAR(CSvars)
NAR(CSdoc)
NAR(CSapp)
GC.Collect(0)
End If
End Sub