엑셀 매크로 관련

기타 2024. 5. 23. 15:20

엑셀에서 매크로 사용전에 체크사항: 

 

  • 보안 설정: 매크로 실행을 위해서는 엑셀의 매크로 보안 설정을 적절히 조정해야 할 수도 있습니다. 파일 > 옵션 > 보안 센터 > 보안 센터 설정 > 매크로 설정에서 매크로를 활성화할 수 있습니다.
  • 참조 설정: HTTP 요청을 처리하기 위해 Microsoft XML, v6.0 참조를 추가해야 할 수도 있습니다. VBA 편집기에서 도구 > 참조를 선택한 후, 해당 항목을 체크합니다.

개발도구 리본 보이기:
 파일->옵션->리본설정 

 

 

엑셀매크로에서 이미지 전송 예제: 이미지뷰어 필수

Sub SendImageToKakaoTalk()
    Dim imgPath As String
    Dim wsShell As Object

    ' 이미지 파일 경로 설정
    imgPath = "C:\Path\To\Your\Image.jpg"

    ' Shell 객체 생성
    Set wsShell = CreateObject("WScript.Shell")

    ' 이미지 파일 열기
    wsShell.Run Chr(34) & imgPath & Chr(34)
    Application.Wait (Now + TimeValue("0:00:02")) ' 이미지 파일이 열리기를 기다립니다.

    ' 이미지 파일을 복사합니다.
    wsShell.SendKeys "^a", True ' 전체 선택 (Ctrl + A)
    Application.Wait (Now + TimeValue("0:00:01"))
    wsShell.SendKeys "^c", True ' 복사 (Ctrl + C)
    Application.Wait (Now + TimeValue("0:00:01"))

    ' 카카오톡 창 활성화
    AppActivate "카카오톡"
    Application.Wait (Now + TimeValue("0:00:01"))

    ' 카카오톡 대화창으로 이동 (여기서는 예시로 첫 번째 채팅방)
    wsShell.SendKeys "^l", True ' 채팅방 리스트로 이동 (Ctrl + L)
    Application.Wait (Now + TimeValue("0:00:01"))
    wsShell.SendKeys "{TAB}", True ' 첫 번째 채팅방으로 이동
    Application.Wait (Now + TimeValue("0:00:01"))
    wsShell.SendKeys "~", True ' 채팅방 열기 (Enter)
    Application.Wait (Now + TimeValue("0:00:01"))

    ' 이미지 붙여넣기 및 전송
    wsShell.SendKeys "^v", True ' 붙여넣기 (Ctrl + V)
    Application.Wait (Now + TimeValue("0:00:01"))
    wsShell.SendKeys "~", True ' 전송 (Enter)

    ' 다시 엑셀로 포커스 이동
    AppActivate Application.Caption
End Sub

 

 

엑셀매크로에서 Rest API

Sub GetRequestExample()
    Dim xmlhttp As Object
    Dim url As String
    Dim response As String

    ' MSXML2.XMLHTTP 객체 생성
    Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
    
    ' API URL 설정
    url = "https://api.example.com/data"

    ' GET 요청 초기화
    xmlhttp.Open "GET", url, False
    ' 요청 헤더 설정 (필요한 경우)
    xmlhttp.setRequestHeader "Content-Type", "application/json"
    
    ' 요청 보내기
    xmlhttp.send
    
    ' 응답 받기
    response = xmlhttp.responseText
    
    ' 응답 출력
    MsgBox response
    
    ' 객체 해제
    Set xmlhttp = Nothing
End Sub



Sub PostRequestExample()
    Dim xmlhttp As Object
    Dim url As String
    Dim postData As String
    Dim response As String

    ' MSXML2.XMLHTTP 객체 생성
    Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
    
    ' API URL 설정
    url = "https://api.example.com/data"
    
    ' POST 데이터 설정 (JSON 형식 예시)
    postData = "{""name"":""John"",""age"":30}"

    ' POST 요청 초기화
    xmlhttp.Open "POST", url, False
    ' 요청 헤더 설정
    xmlhttp.setRequestHeader "Content-Type", "application/json"
    
    ' 요청 보내기
    xmlhttp.send postData
    
    ' 응답 받기
    response = xmlhttp.responseText
    
    ' 응답 출력
    MsgBox response
    
    ' 객체 해제
    Set xmlhttp = Nothing
End Sub

 

 

다운로드 이미지 예제

Sub DownloadFile()
    Dim http As Object
    Dim url As String
    Dim localFilePath As String
    Dim binaryStream As Object

    ' 다운로드할 파일의 URL
    url = "http://localhost:8080/download/file"

    ' 저장할 파일 경로
    localFilePath = "C:\path\to\save\downloadedfile.ext"

    ' Create XMLHTTP object
    Set http = CreateObject("MSXML2.XMLHTTP")
    http.Open "GET", url, False
    http.send

    If http.Status = 200 Then
        If Not IsNull(http.responseBody) And LenB(http.responseBody) > 0 Then
            ' Create binary stream object
            Set binaryStream = CreateObject("ADODB.Stream")
            binaryStream.Type = 1 ' Binary
            binaryStream.Open
            binaryStream.Write http.responseBody
            binaryStream.SaveToFile localFilePath, 2 ' Save binary data to file
            binaryStream.Close
            MsgBox "File downloaded successfully!"
        Else
            MsgBox "The server returned null or empty response."
        End If
    Else
        MsgBox "Failed to download file. Status: " & http.Status
    End If

    ' Clean up
    Set http = Nothing
    Set binaryStream = Nothing
End Sub
Posted by yongary
,