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

  • 相关阅读:
    【Java】Java网络编程
    (4.47)sql server 中的 values 构造临时表
    阿里时序数据库 telegraf+influxdb+grafana for sqlserver input plugin
    全景互动制作工具
    湖南师范大学的案例
    git观点
    js-cookie对cookie的操作
    hsf的意义在于什么
    Prettier看这一篇就行了
    关于微前端的观点
  • 原文地址:https://www.cnblogs.com/amonw/p/1062047.html
Copyright © 2011-2022 走看看