31
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【ExcelVBA】HTTP/HTTPS通信でWebページを取得する

Last updated at Posted at 2019-12-03

はじめに

業務でVBAのプログラムを作っていた時に、内閣府の祝日情報を扱いたいと思ったのですが、VBAではお手軽にHTTP/HTTPS通信出来る組み込みの関数が用意されていませんでした。
(※内閣府の祝日情報はCSV形式で提供されています。)

そこで様々なサイトを参考にしながら、HTTP/HTTPS通信でWebページを取得できるモジュール(クラス)を作成してみました。

作成したクラス

HttpClient.bas
Option Explicit
'--------------------------------------------------------------------------------
' HTTP通信用クラス。
'--------------------------------------------------------------------------------

' HTTP通信用オブジェクト
Private httpObj As Object

'--------------------------------------------------------------------------------
' コンストラクタ
'--------------------------------------------------------------------------------
Public Sub Class_Initialize()
    'Set httpObj = CreateObject("MSXML2.XMLHTTP")           ' TLS1.2に非対応
    Set httpObj = CreateObject("MSXML2.ServerXMLHTTP")
End Sub

'--------------------------------------------------------------------------------
' デストラクタ
'--------------------------------------------------------------------------------
Public Sub Class_Terminate()
    Set httpObj = Nothing
End Sub

'--------------------------------------------------------------------------------
' 引数のURLをGETメソッドで取得する。
'
' url:URL文字列。
' return:取得したページ。
'--------------------------------------------------------------------------------
Public Function GetPage(url As String) As String
    httpObj.Open "GET", url
    httpObj.send

    ' readyState=4で読み込みが完了
    Do While httpObj.readyState < 4
        DoEvents
    Loop

    Dim statusCode As Integer
    statusCode = httpObj.Status
    
    ' HTTPのステータスコードが200(OK)以外であれば、ステータスコードなどを返す。
    If (statusCode = 200) Then
'        GetPage = httpObj.responseText ' レスポンスの文字コードがShift_JIS(MS932)の時はこちらを使う。
        GetPage = StrConv(httpObj.responseBody, vbUnicode)
    Else
        GetPage = "HTTP StatusCode:" & statusCode & ", HTTP StatusText:" & httpObj.statusText
    End If

End Function

テストコード

TestHttpClient.bas
Option Explicit

'--------------------------------------------------------------------------------
' Getメソッドのテストを実行する。
'--------------------------------------------------------------------------------
Public Sub Test_GetPage()
    Dim httpObj As HttpClient
    Set httpObj = New HttpClient
    
    Dim response As String
    response = httpObj.GetPage("https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv")
    Debug.Print response
End Sub
  • 2016年から2026年(現在年の翌年までの10年分)の祝日の情報が返されるため、実行結果を一部省略しています。
実行結果
2016/3/21,休日
2016/4/29,昭和の日
2016/5/3,憲法記念日
...
2025/1/1,元日
2025/1/13,成人の日
2025/2/11,建国記念の日
2025/2/23,天皇誕生日
2025/2/24,休日
2025/3/20,春分の日
2025/4/29,昭和の日
2025/5/3,憲法記念日
2025/5/4,みどりの日
2025/5/5,こどもの日
2025/5/6,休日
2025/7/21,海の日
2025/8/11,山の日
2025/9/15,敬老の日
2025/9/23,秋分の日
2025/10/13,スポーツの日
2025/11/3,文化の日
2025/11/23,勤労感謝の日
2025/11/24,休日
...
2026/10/12,スポーツの日
2026/11/3,文化の日
2026/11/23,勤労感謝の日

まとめ

  • 当初はHTTPS通信に対応させる方法が分からずに四苦八苦しましたが、結果的に簡単な方法でクリアできて良かったです。
  • 中には「レジストリを書き換える」というような方法を紹介しているページもありましたが、そこで諦めずに色々調べてみて良かったです...

その他

31
33
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?