Excel--如何使用EXCEL連續列印快遞單

JackWu發表於2015-06-06

在現在物流快遞業務越來越多,填寫快遞單是一個非常痛苦的事。同時快遞業務也需要記錄跟蹤,在現實工作中我們常常會用excel進行記錄跟蹤。有了excel我們就想到用excel的程式設計功能實現連續列印快遞單,具體步驟如下:

1、根據快遞單幅面,在excel裡增加一個sheet,並制定好excel的同版面的格式,這個工作非常需要耐心(sheet命名為:快遞單);

2、在本機上安裝針式印表機,並自己定好特定紙張(按照快遞的大小設定好自定義紙張),並設定好印表機的預設紙張;

3、填寫excel的通訊頁,根據快遞單的需要填寫的內容,準備好內容(sheet命名為:通訊地址);

4、編寫巨集程式碼如下:

Sub PrintToPrint()

Dim i As Integer, j As Integer
Dim wsh As Worksheet
Set wsh = Application.Worksheets("快遞單")
'remove all page breaks
wsh.ResetAllPageBreaks
'enable autoajusting
wsh.PageSetup.Zoom = False
'set new area
wsh.PageSetup.PrintArea = "A2:K11"
'set paper orientation
wsh.PageSetup.Orientation = xlLandscape
wsh.PrintOut
'wsh.PrintPreview
Set wsh = Nothing

End Sub


Sub PrintMacro()

  Dim cn As New ADODB.Connection
  Dim rsdt As New ADODB.Recordset
  Dim sql As String
  Dim mybook As String

  Dim mResult As Boolean
  
  On Error GoTo Errorhandler
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
 
  mybook = Application.ThisWorkbook.FullName
  With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "extended properties=""Excel 12.0 Xml;HDR=YES;"";data source=" & mybook
    .Open
  End With
  sql = "select * from [通訊地址$]"
  Set rsdt = cn.Execute(sql)
  If Not rsdt.BOF Then
      rsdt.MoveFirst
  End If
  If rsdt.EOF Then
     Exit Sub
  End If

  '彈出印表機機選擇視窗,讓使用者選擇印表機
  mResult = Application.Dialogs(xlDialogPrinterSetup).Show
  If mResult = False Then
     Exit Sub
  End If
  '彈出印表機機選擇視窗,讓使用者選擇印表機
  Application.Dialogs(xlDialogPrinterSetup).Show
  Do Until rsdt.EOF
    '將對應內容填寫到設計好快遞單的相應欄位
    sht.Cells(2, 3).Value = rsdt.Fields.Item("客服").Value
    sht.Cells(7, 3).Value = rsdt.Fields.Item("客服電話").Value
    sht.Cells(2, 7).Value = rsdt.Fields.Item("收件人").Value
    sht.Cells(3, 7).Value = rsdt.Fields.Item("地址").Value
    sht.Cells(6, 7).Value = rsdt.Fields.Item("客戶名稱").Value
    sht.Cells(7, 7).Value = rsdt.Fields.Item("電話").Value
    rsdt.MoveNext
    '迴圈呼叫列印,打出快遞單
    Call PrintToPrint

  Loop
Errorhandler:
  'clear up
  If Err.Number <> 0 Then
     MsgBox Err.Source & "->" & Err.Description & "->" & Err.Number
  End If
  If Not rsdt Is Nothing Then
    If rsdt.State = adStateOpen Then rsdt.Close
  End If
  Set rsdt = Nothing
 
  If Not cn Is Nothing Then
    If cn.State = adStateOpen Then cn.Close
  End If
  Set cn = Nothing
  ActiveWindow.ScrollRow = 1
  ActiveWindow.ScrollColumn = 1
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True

End Sub

 

5、增加一個按鈕,並在事件裡呼叫PrintMacro即可,連續列印了。

 

 

相關文章