`

vbs记录

    博客分类:
  • vbs
阅读更多
有时候要使用vbs做一些小的工具,在此记录一些用到的技巧。
1、计算日期
Dim Zero,LastMonday,LastSunday,LastMonth1,LastMonth2,msg
Zero="1900-01-01 00:00:00"
LastMonth1=DateAdd("m", DateDiff("m", Zero,Now)-1, Zero)
LastMonth2=DateAdd("d",-1,DateAdd("m", DateDiff("m",Zero,Now), Zero))
LastMonday=DateAdd("ww",DateDiff("ww",Zero,Now),-5)
LastSunday=DateAdd("ww",DateDiff("ww",Zero,Now),1)
msg="上月第一天:"&LastMonth1&",最后一天:"&LastMonth2&vbCrLf
msg=msg&"上周一:"&LastMonday&",上周日:"&LastSunday
MsgBox msg

2、格式化日期函数
dim str,date1,y,m,d,dd
str="2011/5/22"
date1=cdate(str)
msgbox FormateDateTime(date1,"3") 
rem   //*********************************************************** 
rem   函数名:FormateDateTime(sendTime,Para) 
rem   函数参数:sendTime:日期格式,Para:格式 
rem   函数作用:格式时间或日期 
rem   函数返回:返回格式化后的字符串 
rem//************************************************************ 
Function   FormateDateTime(sendTime,Para) 
select   case   Para 
rem   YYYYMMDDHHmmss 
case   "1" 
sendTime   =   year(sendTime)   &   right( "00"   &   month(sendTime),2)   &   right( "00"   &   day(sendTime),2)   &   right( "00"   &   hour(sendTime),2)   

&   right( "00"   &   minute(sendTime),2)   &   right( "00"   &   second(sendTime),2) 
rem   YYYYMMDD 
case   "2" 
sendTime   =   year(sendTime)   &   right( "00"   &   month(sendTime),2)   &   right( "00"   &   day(sendTime),2)   
rem   YYYY-MM-DD 
case   "3" 
sendTime   =   year(sendTime)   & "-"&   right( "00"   &   month(sendTime),2)   & "-"&   right( "00"   &   day(sendTime),2) 
rem   YYYY年MM月DD日 
case   "4" 
sendTime   =   year(sendTime)   & "年"&   right( "00"   &   month(sendTime),2)   & "月"&   right( "00"   &   day(sendTime),2)& "日" 
rem   YYYY-MM-DD   HH:mm:ss 
case   "5" 
sendTime   =   year(sendTime)   & "-"&   right( "00"   &   month(sendTime),2)   & "-"&   right( "00"   &   day(sendTime),2)   & "   "&   right( "00"   &   

hour(sendTime),2)   & ":"&   right( "00"   &   minute(sendTime),2)   & ":"&   right( "00"   &   second(sendTime),2) 
end   select   
FormateDateTime   =   SendTime 
end   Function 

3、发送邮件
Sub Send_mail
Dim Email,You_ID,MS_Space
'code by NetPatch 
'VBS发送邮件参数说明 
'You_Account:你的邮件帐号 
'You_Password:你的邮件密码 
'Send_Email: 
'主要邮件地址 
'Send_Email2: 备用邮件地址 
'Send_Topic: 
'邮件主题 
'Send_Body: 
'邮件内容 
'Send_Attachment:邮件附件 
You_ID=Split("你的邮箱地址", "@", -1, vbTextCompare) 
'帐号和服务器分离 
MS_Space = "http://schemas.microsoft.com/cdo/configuration/" 
'这个是必须要的,不过可以放心的事,不会通过微软发送邮件 
Set Email = CreateObject("CDO.Message") 
Email.From = "你的邮箱地址" 
'这个一定要和发送邮件的帐号一样 
Email.To = "收件人地址" 
'主要邮件地址 
Email.CC = "" 
'备用邮件地址 
Email.Subject = "话费统计" 
'邮件主题 
Email.Textbody = "你好,以下是统计日志:" & vbCrLf &Result
'邮件内容 
Email.AddAttachment CurrentPath & "log.TxT"
'邮件附件 
With Email.Configuration.Fields 
.Item(MS_Space&"sendusing") = 2 
'发信端口 
.Item(MS_Space&"smtpserver") = "mail."&You_ID(1) 
'SMTP服务器地址 
.Item(MS_Space&"smtpserverport") = 25 
'SMTP服务器端口 
.Item(MS_Space&"smtpauthenticate") = 1 
'cdobasec 
.Item(MS_Space&"sendusername") = You_ID(0) 
'你的邮件帐号 
.Item(MS_Space&"sendpassword") = "*******" 
'你的邮件密码 
.Update 
End With 
Email.Send 
'发送邮件 
Set Email=Nothing 
Set You_ID = Nothing
Set MS_Space = Nothing
'关闭组件 
End Sub 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics