美文网首页
VBA 宏 - 日常积累

VBA 宏 - 日常积累

作者: 菠萝_4792 | 来源:发表于2020-12-17 22:32 被阅读0次

    EXL 202105 自动检查输入 - DLP

    Sub AutoFill()
    
    '自动填充
    Dim q As Long
        q = Application.WorksheetFunction.CountA(Sheet1.Range("E:E"))
        
        Range("L3").FormulaR1C1 = _
            "=IF(RC[-10]="""","" "",RC[-10]&""|""&RC[-9]&""|""&RC[-8]&""|""&RC[-7]&""|""&RC[-6]&""|""&RC[-5]&""|""&RC[-4])"
        
        Range("L3").Select
        Selection.AutoFill Destination:=Range("L3:L" & q)
    
    '表头补充
        Range("J1").Select
        ActiveCell.FormulaR1C1 = "=COUNTA(C[-5])-2"
        Range("K2").Select
        ActiveCell.FormulaR1C1 = "=""TRL""&REPT(""0"",9-LEN(R[-1]C[-1]))&R[-1]C[-1]"
        Range("L2").Select
        ActiveCell.FormulaR1C1 = _
            "=""HDR""&TEXT(TODAY(),""YYYYMMDD"")&REPT(""0"",9-LEN(R[-1]C[-2]))&R[-1]C[-2]"
        Range("L3").Select
    
    End Sub
    
    
    Sub A_检查()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    
    'Remember time when macro starts
      StartTime = Timer
    
    '********************************************************************************************************************
    
    Application.ScreenUpdating = False '//关闭屏幕刷新
    Application.Calculation = xlCalculationManual '手动重算
    
    '=====
    Dim q As Long, w As String, e As String
        q = 3
        Do While Range("E" & q).Value <> blank
            w = Range("B" & q).Value & Range("F" & q).Value
            If w = "C01Same Contact" Or _
                    w = "C02Fail PSB check " Or _
                    w = "C02ID Expire" Or _
                    w = "C03AML High Risk Not Contactable" Or _
                    w = "C03AML Batch Screening Not Contactable" Or _
                    w = "C03Screening red flag true match" Or _
                    w = "C03Multiple CIF" Or _
                    w = "C04PAT testing - Fail PSB check" Or _
                    w = "C04Customer behavior monitoring" Or _
                    w = "C04Incomplete Address" Or _
                    w = "C04Fail ID expired date logic check" Or _
                    w = "C05PAT testing - Fail PSB check" Or _
                    w = "C05Hubei Province" Or _
                    w = "C05Jiebei Ever30+" Or _
                    w = "C05rewrite account" Or _
                    w = "C04Fail KYC check" Or _
                    w = "C05skip account" Then
                GoTo Next1
            Else
                MsgBox "第" & q & "行数据有问题,block reason code 和 reason remark不对"
                Range("A" & q).Select
                GoTo Issueexit
            End If
                  
    Next1:
            e = Len(Range("E" & q).Value)
            If e = "9" Then
                GoTo Next2
            Else
                MsgBox "第" & q & "行数据有问题,客户号长度不对"
                Range("A" & q).Select
                GoTo Issueexit
            End If
    
    Next2:
            If Range("F" & q).Value = "AML Batch Screening Not Contactable" And Range("I" & q).Value = "" Then
                MsgBox "第" & q & "行数据有问题,I列缺少remark"
                Range("A" & q).Select
                GoTo Issueexit
            Else
                GoTo Next3
            End If
    Next3:
            If Range("A" & q).Value = "" Then
                MsgBox "第" & q & "行数据有问题,A列缺少日期"
                Range("A" & q).Select
                GoTo Issueexit
            Else
                GoTo Next4
            End If
            
    
    Next4: q = q + 1
        
        Loop
    
    
    
    '=====
    Issueexit: Application.ScreenUpdating = True
                Application.Calculation = xlCalculationAutomatic '自动重算
    
    '********************************************************************************************************************
    
    'Determine how many seconds code took to run
      SecondsElapsed = Round(Timer - StartTime, 2)
    
    'Notify user in seconds
      MsgBox "验证时间共计 " & SecondsElapsed & " seconds", vbInformation
        
    Range("A" & q).Select
    
    End Sub
    
    
    
    
    

    EXL 202105 Autobackup - DLP

    Private Sub Workbook_AfterSave(ByVal Success As Boolean)
        
        Application.ScreenUpdating = False
        
        Dim SavePath As String, myName As String, ext As String, user As String, T As String, File As String
        
        SavePath = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Report center\CNDLP Blacklist backup\"
        myName = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".") - 1)) '文件名
        ext = Right(ThisWorkbook.Name, Len(ThisWorkbook.Name) - InStrRev(ThisWorkbook.Name, ".")) '文件后缀
        user = Environ("username") '文件编辑用户名
        T = Format(Now, "yyyymmdd-hhmmss")
        File = SavePath & myName & " " & T & "-" & user & "." & ext
            
        ActiveSheet.Copy
            ActiveWorkbook.SaveAs Filename:= _
                SavePath & myName & " " & T & "-" & user & ".xlsx" _
                , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            
            Columns("C:D").ClearContents
            ActiveWorkbook.Save
            ActiveWindow.Close
        Application.ScreenUpdating = True
        
    End Sub
    
    Private Sub Workbook_Open()
    
    End Sub
    

    EXL 202105 Personal

    Sub InputPath(w As String)
    
    w = InputBox("路径")
    
        If w = "DL" Or w = "dl" Or w = "" Then
            w = "\\shavnasgcg0001\bg52134$\Downloads"
        ElseIf w = "IP" Or w = "ip" Then
            w = "\\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Mail communication"
        ElseIf w = "IL" Or w = "il" Then
            w = "\\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Other Log"
        ElseIf w = "PP" Or w = "pp" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\1-Policy & Other process"
        ElseIf w = "PO" Or w = "po" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Others"
        ElseIf w = "TT" Or w = "tt" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Temp"
    
        'CDD & Screening
        ElseIf w = "PSCREENING" Or w = "pscreening" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\BAU name screening"
        ElseIf w = "PCDD" Or w = "pcdd" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\BAU CDD or KYC refresh"
    
        'Project
        ElseIf w = "UAT" Or w = "uat" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\CBSU BAU Projects\2021 Q2 AML CDD CR testing"
        ElseIf w = "FRD" Or w = "frd" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\CBSU BAU Projects\2021 Q2 AML CDD CR testing\1-BRD FRD"
        ElseIf w = "screening" Or w = "SCREENING" Then
            w = "I:\1-Irene\Citikyc & Project\6-OPPM\2021 Q2 CSAW C Screening"
    
        'Temp
        ElseIf w = "CSI" Or w = "csi" Then
            w = "X:\CBSU\MCA-AML\2021\Q2\CBSU Testing\202104 CitiScreening Product Issue - No hit"
    
    
        'MCA
        ElseIf w = "mca" Or w = "MCA" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\MCA-AML\2021\Q1\CBSU Testing result"
    
        End If
    
    End Sub
    
    Sub A_另存当前文件()
    
    Application.ScreenUpdating = False
    
    Dim w As String, CK2 As String, mypath As String, myfilename As String
    
    Call InputPath(w)
    
            mypath = w & "\"
            'On Error Resume Next
            'VBA.MkDir (mypath)
    
        CK2 = MsgBox("用当前文件名?", vbYesNo)
            If CK2 = 6 Then
                myfilename = ActiveWorkbook.Name
    '            MsgBox mypath & myfilename
                ActiveWorkbook.SaveAs Filename:= _
                mypath & myfilename, _
                FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            Else
                myfilename = InputBox("输入文件名") & ".xlsx"
    '            MsgBox mypath & myfilename
                ActiveWorkbook.SaveAs Filename:= _
                mypath & myfilename, _
                FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            End If
        
        Dim obj As Object
        Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        obj.SetText mypath & myfilename
        obj.PutInClipboard
        Set obj = Nothing
    
    Application.ScreenUpdating = True
    
            
    End Sub
    
    Sub A_当前sheet保存()
    Application.ScreenUpdating = False
    ActiveWorkbook.Save
    
    'Dim sht As Worksheet
    'sht = ActiveSheet
        ActiveSheet.Copy
         ActiveWorkbook.SaveAs Filename:= _
            "\\shavnasgcg0001\bg52134$\Downloads\" & ActiveSheet.Name & ".xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
    
    Dim obj As Object
        Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        obj.SetText "\\shavnasgcg0001\bg52134$\Downloads\" & ActiveSheet.Name & ".xlsx"
        obj.PutInClipboard
        Set obj = Nothing
      
    
    
    Application.ScreenUpdating = True
    End Sub
    
    Sub A_打开当前文件夹()
    
    Shell "explorer.exe " & ActiveWorkbook.Path, vbMaximizedFocus
    
    End Sub
    
    Sub A_创建文件夹()
    Dim myfilename As String, Filename As String
        
    mypath = InputBox("文件夹地址") & "/"
    Filename = InputBox("新文件夹名字")
    
    
        On Error Resume Next
        VBA.MkDir (mypath & Filename)
            
    End Sub
    
    Sub B_保护只读()
    
    ActiveWorkbook.SaveAs WriteResPassword:="Citi1234", ReadOnlyRecommended:=False
    ActiveWorkbook.Save
    
    End Sub
    
    Sub B_保护打开()
    
    ActiveWorkbook.SaveAs Password:="Citi2020", ReadOnlyRecommended:=False
    ActiveWorkbook.Save
    
    End Sub
    
    Sub B_目录()
    
        Sheets(1).Select
            Dim wt As Worksheet
            Sheets.Add.Name = "目录"
            Set wt = Worksheets("目录")
        
        Dim sht As Worksheet, irow As Integer
        irow = 2
        For Each sht In Worksheets
            wt.Cells(irow, "A").Value = irow - 1
            wt.Hyperlinks.Add Anchor:=wt.Cells(irow, "B"), Address:="", SubAddress:="'" & sht.Name & "'!A1", TextToDisplay:=sht.Name
            irow = irow + 1
    
        Next
    
    End Sub
    
    Sub C_功能_添加超链接()
    
    Dim q As Long, w As String, mypath As String, myfilename As String, Thisyear As String
        
    q = 2
    
    Do While Range("A" & q).Value <> blank
        w = "I:\1-Irene\BAU\1-CDD reference checker\" & Range("B" & q).Value
        
        On Error Resume Next
        VBA.MkDir (w)
        
        ActiveSheet.Hyperlinks.Add Anchor:=Range("A" & q), Address:=w
        
        q = q + 1
    Loop
    
    'MsgBox w
    
        
    End Sub
    
    Sub C_功能_8位数字变日期格式()
    
    Dim q As Long, w As String, year As String, month As String, day As String
        
    q = 2
    
    Do While Range("A" & q).Value <> blank
        
        w = Range("A" & q).Value
        year = Left(w, 4)
        month = Mid(w, 5, 2)
        day = Right(w, 2)
        
        year = year & "/" & month & "/" & day
        Range("B" & q).Value = year
        
        q = q + 1
    Loop
        
    End Sub
    
    Sub D_MCA_SMP()
    
        Columns("A:A").Select
        Selection.EntireColumn.Hidden = True
        Columns("D:H").Select
        Selection.EntireColumn.Hidden = True
        Columns("K:K").Select
        Selection.EntireColumn.Hidden = True
        Columns("M:O").Select
        Selection.EntireColumn.Hidden = True
        Columns("Q:U").Select
        Selection.EntireColumn.Hidden = True
        Columns("W:X").Select
        Selection.EntireColumn.Hidden = True
        Columns("Z:AN").Select
        Selection.EntireColumn.Hidden = True
        Columns("AP:AQ").Select
        Selection.EntireColumn.Hidden = True
        Columns("AS:BE").Select
        Selection.EntireColumn.Hidden = True
        Columns("BG:BH").Select
        Selection.EntireColumn.Hidden = True
        Columns("BJ:CE").Select
        Selection.EntireColumn.Hidden = True
        Range("B1").Select
        Columns("B:B").EntireColumn.AutoFit
        Columns("B:B").ColumnWidth = 37.56
        Range("I1:J1").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        Range("P1").Select
        With Selection.Font
            .Color = -16776961
            .TintAndShade = 0
        End With
    End Sub
    
    
    Sub D_画文本框()
    
        ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 117, 35.4, 173.4, _
            72.6).Select
        Selection.ShapeRange.ShapeStyle = msoShapeStylePreset3
        Selection.ShapeRange.Fill.Visible = msoFalse
        With Selection.ShapeRange.Line
            .Visible = msoTrue
            .Weight = 4.5
        End With
        
        'Selection.Delete
    
    End Sub
    
    
    
    

    EXL 202105 Work File

    Sub A_打开当前文件夹()
    
    Shell "explorer.exe " & ActiveWorkbook.Path, vbMaximizedFocus
    
    End Sub
    
    Sub A_另存当前文件()
    
    Application.ScreenUpdating = False
    
    Dim CK As String, CK2 As String, mypath As String, myfilename As String
    
        CK = MsgBox("保存到Download?", vbYesNo)
            If CK = 6 Then
                mypath = "\\shavnasgcg0001\bg52134$\Downloads\"
            Else
                mypath = InputBox("文件路径") & "\"
        '        On Error Resume Next
        '        VBA.MkDir (mypath)
            End If
        CK2 = MsgBox("用当前文件名?", vbYesNo)
            If CK2 = 6 Then
                myfilename = ActiveWorkbook.Name
    '            MsgBox mypath & myfilename
                ActiveWorkbook.SaveAs Filename:= _
                mypath & myfilename, _
                FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            Else
                myfilename = InputBox("输入文件名") & ".xlsx"
    '            MsgBox mypath & myfilename
                ActiveWorkbook.SaveAs Filename:= _
                mypath & myfilename, _
                FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            End If
    
    Application.ScreenUpdating = True
    
            
    End Sub
    
    Sub A_当前sheet保存()
    Application.ScreenUpdating = False
    ActiveWorkbook.Save
    
    'Dim sht As Worksheet
    'sht = ActiveSheet
        ActiveSheet.Copy
         ActiveWorkbook.SaveAs Filename:= _
            "\\shavnasgcg0001\bg52134$\Downloads\" & ActiveSheet.Name & ".xlsx" _
            , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
    
    
    Application.ScreenUpdating = True
    End Sub
    
    
    Sub A_创建文件夹()
    Dim myfilename As String, Filename As String
        
    mypath = InputBox("文件夹地址") & "/"
    Filename = InputBox("新文件夹名字")
    
    
        On Error Resume Next
        VBA.MkDir (mypath & Filename)
    
        
            
    End Sub
    
    
    Sub B_目录()
    
        Sheets(1).Select
            Dim wt As Worksheet
            Sheets.Add.Name = "目录"
            Set wt = Worksheets("目录")
        
        Dim sht As Worksheet, irow As Integer
        irow = 2
        For Each sht In Worksheets
            wt.Cells(irow, "A").Value = irow - 1
            wt.Hyperlinks.Add Anchor:=wt.Cells(irow, "B"), Address:="", SubAddress:="'" & sht.Name & "'!A1", TextToDisplay:=sht.Name
            irow = irow + 1
    
        Next
    
    End Sub
    
    Sub A_添加超链接()
    
    Dim q As Long, w As String, mypath As String, myfilename As String, Thisyear As String
        
    q = 2
    
    Do While Range("A" & q).Value <> blank
        'w = Range("B" & q).Value
        w = "I:\1-Irene\BAU\1-CDD reference checker\" & Range("B" & q).Value
        
        On Error Resume Next
        VBA.MkDir (w)
        'MsgBox w
        
        ActiveSheet.Hyperlinks.Add Anchor:=Range("A" & q), Address:=w
        
        q = q + 1
    Loop
    
    'MsgBox w
    
        
    End Sub
    
    
    

    Outlook 202105 Personal

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Option Explicit
    
    
    Private Sub InputPatch(w As String)
    
    w = InputBox("路径")
    
        If w = "DL" Or w = "dl" Or w = "" Then
            w = "\\shavnasgcg0001\bg52134$\Downloads"
        ElseIf w = "IP" Or w = "ip" Then
            w = "\\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Mail communication"
        ElseIf w = "IL" Or w = "il" Then
            w = "\\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Other Log"
        ElseIf w = "PP" Or w = "pp" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\1-Policy & Other process"
        ElseIf w = "PO" Or w = "po" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Others"
        ElseIf w = "TT" Or w = "tt" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Temp"
    
        'CDD & Screening
        ElseIf w = "PSCREENING" Or w = "pscreening" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\BAU name screening"
        ElseIf w = "PCDD" Or w = "pcdd" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\BAU CDD or KYC refresh"
    
        'Project
        ElseIf w = "UAT" Or w = "uat" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\CBSU BAU Projects\2021 Q2 AML CDD CR testing"
        ElseIf w = "FRD" Or w = "frd" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\CBSU BAU Projects\2021 Q2 AML CDD CR testing\1-BRD FRD"
        ElseIf w = "screening" Or w = "SCREENING" Then
            w = "I:\1-Irene\Citikyc & Project\6-OPPM\2021 Q2 CSAW C Screening"
    
        'Temp
        ElseIf w = "CSI" Or w = "csi" Then
            w = "X:\CBSU\MCA-AML\2021\Q2\CBSU Testing\202104 CitiScreening Product Issue - No hit"
    
    
        'MCA
        ElseIf w = "mca" Or w = "MCA" Then
            w = "\\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\MCA-AML\2021\Q1\CBSU Testing result"
    
        End If
            'Download - DL - \\shavnasgcg0001\bg52134$\Downloads\
            'My Project-IP - \\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Mail communication\
            'My Log-IL - \\shavnasgcg0001\bg52134$\1-Irene\Citikyc & Project\0-Other Log\
            'Pub Policy-PP - \\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\1-Policy & Other process\
            'Pub Policy Others-PO - \\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Others
            'Pub Temp-TT - \\canvnasgcg0001\GRP_GCBCN_CBORC\CBSU\Policy & Sharing\Temp
    
    
    End Sub
    
    Public Sub 邮箱保存到下载()
    
    Dim oMail As Object
    Dim objItem As Object
    Dim sPath As String
    Dim sFrom As String
    Dim dtDate As Date
    Dim sName As String
    Dim obj As Object
    Dim enviro As String
    
    For Each oMail In ActiveExplorer.Selection
        sName = oMail.Subject
        dtDate = oMail.ReceivedTime
        sFrom = Left(oMail.Sender, 15)
        ReplaceCharsForFileName sName, ""
        
        sName = Format(dtDate, "yy_mm_dd_", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "hhnn", _
            vbUseSystemDayOfWeek, vbUseSystem) & "_" & sFrom & "-" & sName & ".msg"
        sPath = "\\shavnasgcg0001\bg52134$\Downloads\"
            'sPath = "I:\1-Irene\Mail backup\1- BAU-CDD\201912\" '邮件保存路径
        Debug.Print sPath & sName
        
        oMail.SaveAs sPath & sName, olMSG
        
        'Sleep 4000
    Next
    
            Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
            obj.SetText sPath & sName
            obj.PutInClipboard
            Set obj = Nothing
    
    End Sub
    
    Public Sub 邮箱保存指定路径()
    
    Dim oMail As Object
    Dim objItem As Object
    Dim sPath As String
    Dim sFrom As String
    Dim dtDate As Date
    Dim sName As String
    Dim obj As Object
    Dim enviro As String
    Dim w As String
    
    InputPatch w
    
    For Each oMail In ActiveExplorer.Selection
        sName = oMail.Subject
        dtDate = oMail.ReceivedTime
        sFrom = Left(oMail.Sender, 15)
        ReplaceCharsForFileName sName, ""
        
        sName = Format(dtDate, "yy_mm_dd_", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "hhnn", _
            vbUseSystemDayOfWeek, vbUseSystem) & "_" & sFrom & "-" & sName & ".msg"
        sPath = w & "\"
            'sPath = "I:\1-Irene\Mail backup\1- BAU-CDD\201912\" '邮件保存路径
        Debug.Print sPath & sName
        oMail.SaveAs sPath & sName, olMSG
        
        'Sleep 4000
    Next
        
        Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        obj.SetText sPath & sName
        obj.PutInClipboard
        Set obj = Nothing
      
    End Sub
    Public Sub 保存当前打开邮件()
    
    Dim oMail As Object
    Dim objItem As Object
    Dim sPath As String
    Dim sFrom As String
    Dim dtDate As Date
    Dim sName As String
    Dim obj As Object
    Dim enviro As String
    Dim w As String
    
    InputPatch w
    
    Set oMail = ActiveInspector.CurrentItem
        sName = oMail.Subject
        dtDate = oMail.ReceivedTime
        sFrom = Left(oMail.Sender, 15)
        ReplaceCharsForFileName sName, ""
        
        sName = Format(dtDate, "yy_mm_dd_", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "hhnn", _
            vbUseSystemDayOfWeek, vbUseSystem) & "_" & sFrom & "-" & sName & ".msg"
        sPath = w & "\"
            'sPath = "I:\1-Irene\Mail backup\1- BAU-CDD\201912\" '邮件保存路径
        Debug.Print sPath & sName
        oMail.SaveAs sPath & sName, olMSG
    
        
        Set obj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        obj.SetText sPath & sName
        obj.PutInClipboard
        Set obj = Nothing
      
    End Sub
    Sub 记录时间()
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    'Remember time when macro starts
      StartTime = Timer
    
    '*****************************
        Dim oMail As Object
        Dim dtDate As Date
        Dim sName As String
        Dim sFrom As String
    
        For Each oMail In ActiveExplorer.Selection
    
    
            dtDate = oMail.ReceivedTime
            sName = oMail.Subject
            sFrom = Left(oMail.Sender, 15)
            ReplaceCharsForFileName sName, ""
    
           sName = Format(dtDate, "yy_mm_dd_", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "hhnn", _
            vbUseSystemDayOfWeek, vbUseSystem) & "_" & sFrom & "-" & sName
            oMail.Subject = sName
            oMail.Save
        Next
        
    '*****************************
    
    'Determine how many seconds code took to run
      SecondsElapsed = Round(Timer - StartTime, 2)
    
    'Notify user in seconds
      MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
    End Sub
    
    Sub 修改名字()
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    'Remember time when macro starts
      StartTime = Timer
    
    '*****************************
        Dim oMail As Object
        Dim dtDate As Date
        Dim sName As String
        Dim sFrom As String
    
        For Each oMail In ActiveExplorer.Selection
    
    
            dtDate = oMail.ReceivedTime
            sName = oMail.Subject
            sFrom = Left(oMail.Sender, 15)
            sName = Format(dtDate, "yy_mm_dd_", vbUseSystemDayOfWeek, vbUseSystem) & Format(dtDate, "hhnn", _
            vbUseSystemDayOfWeek, vbUseSystem) & "_" & sFrom & "-" & sName
            
            ReplaceCharsForFileName sName, ""
            oMail.Subject = sName
            oMail.Save
        Next
        
    '*****************************
    
    'Determine how many seconds code took to run
      SecondsElapsed = Round(Timer - StartTime, 2)
    
    'Notify user in seconds
      MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
    End Sub
    
    Private Sub ReplaceCharsForFileName(sName As String, sChr As String)
    
      sName = Replace(sName, "'", sChr)
      sName = Replace(sName, "*", sChr)
      sName = Replace(sName, "/", sChr)
      sName = Replace(sName, "\", sChr)
      sName = Replace(sName, ":", sChr)
      sName = Replace(sName, "?", sChr)
      sName = Replace(sName, Chr(34), sChr)
      sName = Replace(sName, "<", sChr)
      sName = Replace(sName, ">", sChr)
      sName = Replace(sName, "|", sChr)
      sName = Replace(sName, "!", sChr)
      sName = Replace(sName, ":", sChr)
      sName = Replace(sName, ":", sChr)
      sName = Replace(sName, "】", sChr)
      sName = Replace(sName, "【", sChr)
      sName = Replace(sName, "  ", sChr)
      sName = Replace(sName, ",", sChr)
      sName = Replace(sName, "(", sChr)
      
      If Len(sName) > 120 Then
        sName = Left(sName, 120)
      End If
      
    End Sub
    
    
    Public Sub 延迟发送SendDeferredMessage()
    Dim objMsg As MailItem
    Dim SendAt
    
    Set objMsg = ActiveInspector.CurrentItem
    
    'send at 8:24 AM. .25 = 6 AM, .50 = noon // (.25 = 6 AM, .50 = noon, .75 = 6 PM.)
        'MyDate contains the date for February 12, 1969.
        'MyDate = DateSerial(1969, 2, 12)    ' Return a date.
        'SendAt = DateSerial(Year(Now), Month(Now), Day(Now + 3)) + #9:00:00 AM#
    
    SendAt = DateSerial(2021, 6, 1) + #9:00:00 AM#
    
    
    
      objMsg.DeferredDeliveryTime = SendAt
    
     'displays the message form
      objMsg.Display
      
      Set objMsg = Nothing
    
    End Sub
    
    
    

    相关文章

      网友评论

          本文标题:VBA 宏 - 日常积累

          本文链接:https://www.haomeiwen.com/subject/pqutnktx.html