zoukankan      html  css  js  c++  java
  • Some useful vba macros

    1. Save attachements from multiple emails to a directory (Outlook):
    Public Sub SaveAttachments()
        Dim SaveToPath As String
        SaveToPath = "C:\temp\"
        Set myfolder = Application.ActiveExplorer.CurrentFolder
        For Each myitem In myfolder.Items
            For Each myattachment In myitem.Attachments
                myattachment.SaveAsFile SaveToPath & myattachment.FileName
            Next
        Next
        MsgBox "All attachements in "& myfolder.FolderPath & " have been saved to " & SaveToPath
    End Sub

    2. Convert all xls files in a directory to csv files (Excel) (from http://jointtech.com/today-a-client-asked/xls-csv-convert):

    Option Explicit
    Sub testme01()

        Application.ScreenUpdating = False

        Dim myFiles() As String
        Dim fCtr As Long
        Dim myFile As String
        Dim myPath As String
        Dim tempWkbk As Workbook
        Dim logWks As Worksheet
        Dim tempName As String
        Dim wks As Worksheet
        Dim oRow As Long


        'change to point at the folder to check
        myPath = "D:\My Documents\john"
        If Right(myPath, 1) <> "\" Then
            myPath = myPath & "\"
        End If


        myFile = Dir(myPath & "*.xls")
        If myFile = "" Then
            MsgBox "no files found"
            Exit Sub
        End If


        Set logWks = Workbooks.Add(1).Worksheets(1)
        logWks.Range("a1").Resize(1, 3).Value _
            = Array("WkbkName", "WkSheetName", "CSV Name")


        'get the list of files
        fCtr = 0
        Do While myFile <> ""
            fCtr = fCtr + 1
            ReDim Preserve myFiles(1 To fCtr)
            myFiles(fCtr) = myFile
            myFile = Dir()
        Loop


        If fCtr > 0 Then
            oRow = 1
            For fCtr = LBound(myFiles) To UBound(myFiles)
                Set tempWkbk = Nothing
                On Error Resume Next
                Set tempWkbk = Workbooks.Open(Filename:=myPath & myFiles(fCtr))
                On Error GoTo 0
                If tempWkbk Is Nothing Then
                    logWks.Cells(oRow, "A").Value = "Error Opening: " _
                                                          & myFiles(fCtr)
                    oRow = oRow + 1
                Else
                    For Each wks In tempWkbk.Worksheets
                        With wks
                            If Application.CountA(.UsedRange) = 0 Then
                                'do nothing
                            Else
                                .Copy 'to a new workbook
                                tempName = myPath & Left(myFiles(fCtr), Len(myFiles(fCtr)) - 4) & "." & Trim(.Name) & ".csv"
                                Do
                                    If Dir(tempName) = "" Then
                                        Exit Do
                                    Else
                                        tempName = myPath & Trim(.Name) & "_" _
                                              & Format(Time, "hhmmss") & ".csv"
                                    End If
                                Loop
                                oRow = oRow + 1
                                With ActiveWorkbook
                                    .SaveAs Filename:=tempName, FileFormat:=xlCSV
                                    .Close savechanges:=False
                                End With
                                logWks.Cells(oRow, "A").Value = myFiles(fCtr)
                                logWks.Cells(oRow, "b").Value = .Name
                                logWks.Cells(oRow, "C").Value = tempName
                            End If
                        End With
                    Next wks
                    tempWkbk.Close savechanges:=False
                End If
            Next fCtr
        End If


        With logWks.UsedRange
            .AutoFilter
            .Columns.AutoFit
        End With


        Application.ScreenUpdating = True


    End Sub

  • 相关阅读:
    ReentrantLock(重入锁)的源码解析
    vue项目使用vue-photo-preview插件实现点击图片放大预览和移动
    BOM简单总结
    js中属性类型:数据属性与访问器属性
    Javascript面向对象编程(三):非构造函数的继承(对象的深拷贝与浅拷贝)
    Javascript面向对象编程(二):构造函数的继承 作者:yuan一峰
    Javascript 面向对象编程(一):封装 作者:yuan一峰
    js面向对象、创建对象的工厂模式、构造函数模式、原型链模式
    Vue中父子组件执行的先后顺序
    Vue子组件调用父组件的方法
  • 原文地址:https://www.cnblogs.com/amonw/p/1062047.html
Copyright © 2011-2022 走看看