Skip to main content

Insert all image in folder into Excel using VBA

' Funtion to get name of root folder
Function getNameFolder()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    If fd.Show = -1 Then
        getNameFolder = fd.SelectedItems(1)
    Else
        getNameFolder = ""
    End If
End Function

'main handling...
Sub Main()
    'Declare variables for folders
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objSubFolder As Object

    'Declare variables for File
    Dim objFolderFile As Object
    Dim objFile As Object
    Dim ws As Worksheet
 
    Dim pic As Picture
    'Declare variables for format image when insert
    Dim i As Long
    Dim width As Long
    Dim height As Long
    'Declare variables for path initinal
    Dim path As String
    path = getNameFolder
    Application.ScreenUpdating = False
    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    'Get the folder object
    Set objFolder = objFSO.GetFolder(path)
    'loops through each file in the directory and prints their names and path
    For Each objSubFolder In objFolder.subfolders
        'Declare variables for default format initial
        i = 20
   
        'add new sheet
        With ThisWorkbook
            Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        End With
   
        'Get the folder object
        'Set name for sheet
        ws.Name = "IMP_" & objSubFolder.Name
     
        Range("A1") = "Test ID:"
        Range("B1") = "IMP_" & objSubFolder.Name
     
        Set objFolderFile = objFSO.GetFolder(objSubFolder.path)
            For Each objFile In objFolderFile.Files
         
                Set pic = ActiveSheet.Pictures.Insert(objFile.path)
                pic.Select
                'Selection.ShapeRange.IncrementTop i
                ' Scale images after insert by 100%
                Selection.ShapeRange.ScaleWidth 1, msoTrue
                Selection.ShapeRange.ScaleHeight 1, msoTrue
             
                width = Selection.ShapeRange.width
                height = Selection.ShapeRange.height
                pic.Delete
                Application.ActiveSheet.Shapes.AddPicture objFile.path, False, True, 50, i, width, height
                i = i + height + 20
             
                Application.ActiveSheet.Shapes.AddPicture "D:\a.jpg", False, True, (width / 2), i, 75, 75
             
                i = i + 75 + 20
            Next objFile
     
        ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Select
        Selection.Delete
     
     
        Range("A1").Select
    Next objSubFolder
 
    Application.ScreenUpdating = True
End Sub


Popular posts from this blog

UTF-8 Miscellaneous Symbols

Dưới đây là danh sach mã Icon được sử dụng trong HTML, bạn có thể tham khảo bài viết  Thay thế ký tự với Icon sử dụng mảng trong PHP  để biết cách sử dụng cho bộ Icon này nhé. Chúc các bạn thành công. Arial Arial Unicode MS Segoe UI Symbol Dec Hex Entity Description ☀ ☀ ☀ 9728 2600 BLACK SUN WITH RAYS ☁ ☁ ☁ 9729 2601 CLOUD ☂ ☂ ☂ 9730 2602 UMBRELLA ☃ ☃ ☃ 9731 2603 SNOWMAN ☄ ☄ ☄ 9732 2604 COMET ★ ★ ★ 9733 2605 BLACK STAR ☆ ☆ ☆ 9734 2606 WHITE STAR ☇ ☇ ☇ 9735 2607 LIGHTNING ☈ ☈ ☈ 9736 2608 THUNDERSTORM ☉ ☉ ☉ 9737 2609 SUN ☊ ☊ ☊ 9738 260A ASCENDING NODE ☋ ☋ ☋ 9739 260B DESCENDING NODE ☌ ☌ ☌ 9740 260C CONJUNCTION ☍ ☍ ☍ 9741 260D OPPOSITION ☎ ☎ ☎ 9742 260E BLACK TELEPHONE ☏ ☏ ☏ 9743 260F WHITE TELEPHONE ☐ ☐ ☐ 9744 2610 BALLOT BOX ☑ ☑ ☑ 9745 2611 BALLOT BOX WITH CHECK ☒ ☒ ☒ 9746 2612 BALLOT BOX WITH X ☓ ☓ ☓ 9747 2613 SALTIRE ☔ ☔ ☔ 9748 2614 UMBRELLA WITH RAIN DROPS ☕ ☕ ☕ 9749 2615 HOT BEVERAGE ☖ ☖ ☖ 9750 2616 WHITE SHOGI PIECE ☗ ☗ ☗ 9751 2617 BLACK SHOGI P...

Tìm hiểu về margin và padding

Để có một blog đẹp chúng ta phải chỉnh sửa mã nguồn cho các thành phần trở nên cân đối và hài hòa. Bài viết này sẽ hướng dẫn các bạn làm quen với margin và padding,  canh lề và canh đệm. Những ai đã từng học qua CSS thì không lạ gì với hai tag này.

goPet 130 HD - Game Đấu Thú Được Yêu Thích

goPet 130 HD - Game Đấu Thú Được Yêu Thích Nuôi Thú online chiến đấu, đông vui, khắc nghiệt, thể hiện phong cách xì teen của mình.... Tải miễn phí