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

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.

Lọc các ký tự xấu (chửi tục) và thay thế chúng sử dụng mảng trong PHP

Trong một số diễn đàn, có một số bạn sử dụng một số từ thô tục để nói với nhau hoặc chửi nhau. Ở bài viết này, mình sẽ hướng dẫn các bạn tạo một hàm cơ bản để lọc các từ ngữ thô tục và thay thế chúng bằng các từ ngữ dễ thương hơn. Bằng cách tương tự như  Thay thế ký tự với Icon sử dụng mảng trong PHP  ta sẽ tiếp tục sử dụng cách thức trên đối với việc lọc các từ ngữ thô tục và thay thế chúng. VD: khi một bạn chat "Mày bị khùng, đm mày, đồ thằng chó". <?php function badWordFilter($data){ $originals = array( "Khùng" , "đm" , "chó" ); $replacements = array( "dễ thương" , "yêu" , "đẹp trai" ); $data = str_ireplace($originals,$replacements,$data); return $data; } $myData = " Mày bị khùng, đm mày, đồ thằng chó " ; $cleaned = badWordFilter($myData); echo $cleaned; //Mày bị dễ thương, yêu mày, đồ thằng đẹp trai ?> Thật là dễ dàng phải khôngkhông, tại sao không thử nhỉ.