よく使うWindowsAPI

本ページはプロモーションが含まれています

URLDownloadToFile:URL指定でファイルをダウンロードする

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
引数 説明
pCaller 気にせず常に『0』でいい
szURL ダウンロードしたいファイルのURL
szFileName ダウンロードしたファイルを保存するパス
dwReserved 気にせず常に『0』でいい
lpfnCB 気にせず常に『0』でいい

サンプルソース

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

Sub Sample()
Dim strURL As String, strFile As String
strURL = "https://i0.wp.com/javeo.jp/wp-content/uploads/2022/02/20210824_060805-scaled-e1645711904613-218x300.jpg"
strFile = "C:\ネコ.jpg"
URLDownloadToFile 0, strURL, strFile, 0, 0
End Sub

テストページにある我が家のにゃんこの若かりし頃の画像が手に入ります

GetPrivateProfileString:iniファイルを読み込む

Public Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
                         (ByVal lpApplicationName As String, _
                          ByVal lpKeyName As Any, _
                          ByVal lpDefault As String, _
                          ByVal lpReturnedString As String, _
                          ByVal nSize As Long, _
                          ByVal lpFileName As String) As Long
引数 説明
lpApplicationName セクション名
lpKeyName キー名
lpDefault キーが存在しない場合の初期値
lpReturnedString 取得してきた値が格納される
nSize lpReturnedStringのバイト数
lpFileName 対象iniファイルパス

サンプルファイルとサンプルソース

[ChromeDriver]
ChromeVersion=98.0.4758.102
ChromePath=C:\Program Files (x86)\Google\Chrome\Application
Sub Sample()
Dim RC As Long
Dim RS As String
RC = GetPrivateProfileString("ChromeDriver", "ChromeVersion", "1.0.0.0", RS, 255, "config.ini")
Debug.Print RS '---98.0.4758.102
End Sub

WritePrivateProfileString:iniファイルに書き込む

Public Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
                         (ByVal lpApplicationName As String, _
                          ByVal lpKeyName As Any, _
                          ByVal lpString As Any, _
                          ByVal lpFileName As String) As Long
引数 説明
lpApplicationName セクション名
lpKeyName キー名
lpString 変更する値
lpFileName 対象iniファイルパス

サンプルソースと処理後のサンプルファイル

Sub Sample()
RC = WritePrivateProfileString("ChromeDriver", "ChromeVersion", "99.9.9999.999", "C:\Program Files\SeleniumBasic\config.ini")
End Sub
[ChromeDriver]
ChromeVersion=99.9.9999.999
ChromePath=C:\Program Files (x86)\Google\Chrome\Application

Sleep:待機する

Public Declare PtrSafe Function Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
引数 説明
dwMilliseconds 待機時間(ミリ秒)

サンプルソース

Public Declare PtrSafe Function Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Sample()
Call Sleep(100)
Call Application.Wait(Now + TimeSerial(0, 0, 1))
End Sub

どちらも1秒待機する
※つまりSleepを使わなくても同じようなことはできる

タイトルとURLをコピーしました