VBA CODE

VBA 이미지 다운로드 저장 코드

VBA 2023. 3. 31. 00:31

Sub DownloadImage()
    Dim myURL As String
    myURL = "https://www.example.com/myimage.jpg"
    Dim myData() As Byte
    Dim myFile As String
    myFile = "C:\이미지테스트\MyImage.jpg"
    
    Dim winHttpReq As Object
    Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
    
    winHttpReq.Open "GET", myURL, False
    winHttpReq.Send
    
    myData = winHttpReq.ResponseBody
    Set winHttpReq = Nothing
    
    Dim myStream As Object
    Set myStream = CreateObject("ADODB.Stream")
    myStream.Type = 1
    myStream.Open
    myStream.Write myData
    myStream.SaveToFile myFile, 2
    myStream.Close
    Set myStream = Nothing
End Sub

 

 

 

---------------------------------------------------------------------------------------------------------------------------------------

 

 

Sub Report()
    
    Dim lngEndRow           As Long
    Dim i                        As Long
    Dim strSavePath           As String
    Dim strURL                 As String
    Dim Img                     As Object
    
    lngEndRow = Range("D1000").End(xlUp).Row
    ActiveSheet.Pictures.Delete
    
    For i = 6 To lngEndRow
        strURL = Range("E" & i)
        
        Set Img = ActiveSheet.Pictures.Insert(strURL)
        Img.Name = Range("D" & i)
        
        strSavePath = Range("E3") & "\" & Img.Name & ".PNG"
        
        Img.CopyPicture Appearance:=xlScreen, Format:=xlPicture
        With Sheet6.ChartObjects.Add(0, 0, Img.Width, Img.Height)
            With .Chart
                 .Parent.Select
                 .Paste
                 .Export strSavePath
                 .Parent.Delete
            End With
        End With
        ActiveSheet.Pictures.Delete
    Next i
    
    MsgBox "이미지 저장 완료!"
End Sub

 

 

--------------------------------

 

Sub PhotoPaste()
    Dim F_dlg As FileDialog
    Dim P_Shape As Shape
    Dim MyChart As Chart
    Dim NewWS As Worksheet
    Dim i As Integer
    Dim F_Path, F_Name As String
    
    '1. FileDialog로 폴더명 지정하기
    Set F_dlg = Application.FileDialog(msoFileDialogFolderPicker)
    F_dlg.Title = "저장할 폴더를 선택하세요"
    F_dlg.Show
    F_Path = F_dlg.SelectedItems(1)
    
    '각각의 그림을 순환하면서
    For Each P_Shape In ActiveSheet.Shapes
    
        ' 2. 이미지의 파일명이 될 두번째 열의 값을 가져오기
        For i = 2 To 10000
            If Range("B" & i).Top > P_Shape.Top Then
                F_Name = Range("B" & i - 1).Value
                Exit For
            End If
        Next i
    
        '3. 이미지를 저장하기 ( 워크시트 생성 → Chart 생성 → Chart에 그림 붙이고, 저장 → 워크시트 삭제)
        Set NewWS = ActiveWorkbook.Sheets.Add
        
        With NewWS.Shapes.AddChart2
            .Height = P_Shape.Height
            .Width = P_Shape.Width
        End With
    
        P_Shape.Copy
        Set MyChart = NewWS.Shapes.Item(1).Chart
        MyChart.ChartArea.Select
        MyChart.Paste
        MyChart.Export F_Path & "\" & F_Name & ".PNG", "PNG"
        Application.DisplayAlerts = False
        NewWS.Delete
        Application.DisplayAlerts = True
    
    Next P_Shape
End Sub