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
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章