今天的收获---实现excel工资单的自动发送

每个公司每月发工资的同时要给每个员工一张工资单。而发工资单的主要目的是让员工知道自己一个月来工资组成的详细情况,确认财务结算是否有误。一般的处理方式是直接在原来全员工资表的基础上给每个员工记录前插入表头等整张打印出来后一条一条的剪裁出来发给各个员工。这种方式对小公司来说还可以,但当公司达到好几十上百或上千的员工时,财务人员光每个月剪裁着工资单就要花不少时间,而且还要花费很多纸张

我们的解决方式是将每个人的表头及工资记录提取出来重新生成一个xls文件,同时启动outlook将它作为附件发送给相应的个人。这样每个月公司的财务人员做好了全员工资报表后只要点击一下一个按钮就可完成。这样既节省了财务人员的时间降低了他们的工作强度使他们有更多精力去做专业的事,同时也为公司节约了纸张。----别看小小纸张,在好多公司打印纸的消耗量有多大大家心里应该都有数,这可是一笔不小的开销啊。

该文就以上面的思想为主线来实现。具体代码如下:

Private Sub CommandButton1_Click()
    Dim outFile
    Dim Email
    Dim cn As New ADODB.Connection
    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim oOutlookApp As New Outlook.Application
    Dim oItemMail As Outlook.MailItem
    Set oItemMail = oOutlookApp.CreateItem(olMailItem)
   
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
               "Data Source=C:/Documents and Settings/gw/My Documents/Book3.xls;" & _
               "Extended Properties=""Excel 8.0;"""
    cn.Open
   
    Set xlApp = New Excel.Application
   
   
    MsgBox "Creat ready." & [a1].End(xlDown).Row, vbInformation
   
    For i = 2 To [a1].End(xlDown).Row
       
        Email = Cells(i, 11)
        MsgBox "Creat ready." & Email, vbInformation
        Set xlBook = xlApp.Workbooks.Add
        Set xlSheet = xlBook.ActiveSheet
   
        MsgBox "Creat is beginning." & i, vbInformation
   
        xlSheet.Range("a1:k1").Value = Range("a1:k1").Value
        xlSheet.Range("a2:k2").Value = Range("a" & i & ":k" & i).Value
       
  
       
        outFile = "C:/Documents and Settings/" & Cells(i, 4) & ".xls"
       
        MsgBox "Creat file name." & outFile, vbInformation
       
        xlBook.SaveAs "C:/Documents and Settings/" & Cells(i, 4) & ".xls"
       
        MsgBox "Creat file " & outFile & "is closing", vbInformation
       
        xlBook.Close False
       
        MsgBox "Creat file " & outFile & "close", vbInformation
       
        With oItemMail
            .Subject = "工资单"
            .Body = "请确认你的工资单,若有问题在本月10号之前到财务处确认"
            .Attachments.Add (outFile)
            .Importance = olImportanceHigh
            .Recipients.Add (Email)
            .Sensitivity = olPersonal
            .Send
        End With
       
        outFile = ""
        Email = ""
        Set oItemMail = oOutlookApp.CreateItem(olMailItem)
       
        MsgBox "Mail" & (i - 1) & " is Created"
   
    Next    
    
    cn.Close
    Set cn = Nothing
   
    MsgBox "all finish", vbInformation
End Sub

各位只要在工资表上加一个BUTTON,再把这段代码略加修改就可使用了

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值