ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

VBA_OutLook_代码

2022-06-27 14:33:58  阅读:173  来源: 互联网

标签:Dim VBA Set End string 代码 OutLook String string1



Sub Get2FACode()
    Dim objApp As Outlook.Application
    Dim objItem As Object ' MailItem
    Dim myOlItems As Object
    
    Set objApp = Outlook.Application
    
    '获取选中的(或打开的)邮件
'    Set objItem = objApp.ActiveExplorer.Selection.item(1)
'    Call SaveAutoAttach(objItem)
    
    '获取收件箱内最新的邮件
'    Set objNS = objApp.GetNamespace("MAPI")
'    Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
'    Call SaveAutoAttach(myOlItems(myOlItems.Count))
    
    
    '循环遍历收件箱内前3封邮件
    Dim i
    Dim j
    Dim ret
    Set objNS = objApp.GetNamespace("MAPI")
    Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
    For i = 1 To 10
        Application.Wait (Now + TimeValue("0:00:05"))
        For j = myOlItems.Count To (myOlItems.Count - 3) Step -1
            ret = ""
            ret = SaveAutoAttach(myOlItems(j))
            If ret <> "" Then
                Exit For
            End If
        Next j
        If ret <> "" Then
            Exit For
        End If
    Next i
    Debug.Print (ret)
End Sub



Public Function SaveAutoAttach(item As Outlook.MailItem) As String
    Dim regex           As Object
    Dim MatchSet        As Object
    Dim Match2FACode    As String
    
    Match2FACode = ""
    Set regex = CreateObject("vbscript.regexp")
    
    'Find the 2FA passcode   ---  For SAP
'    regEx.Pattern = "2FATokenforlogin\:[a-zA-Z0-9]{6,20}"
'    regEx.Global = True
'
'    source_string = VBA.Replace(item.Body, " ", "")
'    Set MatchSet = regEx.Execute(source_string)
'
'    If MatchSet.Count > 0 Then
'        Match2FACode = Split(MatchSet(0).Value, ":")(1)
'    End If
    
    'Find the 2FA passcode   ---  For IBM internal website
    regex.Pattern = "Yourpasscodeis\:[0-9]{4}-[0-9]{6}"
    regex.Global = True
    
    source_string = VBA.Replace(item.Body, " ", "")
    Set MatchSet = regex.Execute(source_string)
    
    If MatchSet.Count > 0 Then
        Match2FACode = Split(Split(MatchSet(0).Value, ":")(1), "-")(1)
    End If
    
    
    'Debug.Print (Match2FACode)
    SaveAutoAttach = Match2FACode
    
    'Save the 2FA passcode into environment "GV_AC_CODE"
    'Call WriteUserEnv("2FA_CODE", CStr(Match2FACode))

End Function

'Write certain value into environment variable function
Sub WriteUserEnv(in_name As String, in_value As String)
    Dim objUserEnvVars As Object
    Set objUserEnvVars = CreateObject("WScript.Shell").Environment("User")
    objUserEnvVars.item(in_name) = in_value
End Sub



'正则表达式的其他例子:
Sub t3()
    Dim bo As Boolean
    bo = isDightOrLetter("我12sdf", "Asc")
    Debug.Print (bo)
    bo = isDightOrLetter("我12sdf", "Regx")
    Debug.Print (bo)

End Sub

Function isDightOrLetter(in_str As String, in_type As String) As Boolean
    '均在半角下有效
    Dim string1     As String
    string1 = in_str
    
    If in_type = "Asc" Then
        Dim string_all  As String
        Dim string1_arr
        Dim i
        Dim j
 
        string1 = VBA.Replace(string1, " ", "")
        For i = 1 To Len(string1)
            string_all = VBA.Trim(string_all & Mid(string1, i, 1) & "|")
        Next
        
        If Right(string_all, 1) = "|" Then
            string_all = VBA.Left(string_all, Len(string_all) - 1)
        End If
        
        string1_arr = VBA.Split(string_all, "|")
        'Debug.Print (UBound(string1_arr))
        
        For Each j In string1_arr
            'Debug.Print (Asc(j))
            If (Asc(j) >= 48 And Asc(j) <= 57) Or (Asc(j) >= 65 And Asc(j) <= 90) Or (Asc(j) >= 97 And Asc(j) <= 122) Then
                isDightOrLetter = True
            Else
                isDightOrLetter = False
                Exit For
            End If
        Next j
        
    ElseIf in_type = "Regx" Then
        Dim regex As New RegExp
        Dim MatchSet
        Set regex = CreateObject("vbscript.regexp")
        regex.Pattern = "^[0-9A-Za-z]+[0-9A-Za-z]$"
        regex.Global = True
    
        Set MatchSet = regex.Execute(string1)
        
        If MatchSet.Count > 0 Then
            isDightOrLetter = True
        Else
            isDightOrLetter = False
        End If
    End If
    
End Function




















标签:Dim,VBA,Set,End,string,代码,OutLook,String,string1
来源: https://www.cnblogs.com/Collin-pxy/p/16416000.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有