LoginSignup
1
3

More than 5 years have passed since last update.

Implement FTP on VBA with WinInet

Posted at

VBA に FTP 機能を実装したい。

外部ライブラリをインストールできる環境なら下記を参考に BASP21 を導入してシンプルな実装が可能。
http://officetanaka.net/excel/vba/tips/tips47.htm

それができない環境では Windows 標準の WinInet を利用して実装しようというお話。
実装したい機能の Windows API に対する逆引きを示す。

Windows Internet

インターネットサービスのハンドルを取得

WinInet を初期化して使用開始する。これ自体は FTP に特化したものでなく、 HTTP も利用可能。

InternetOpen
https://msdn.microsoft.com/en-us/library/windows/desktop/aa385096(v=vs.85).aspx

FTP サーバへ接続

同じく、HTTP も利用可能。

InternetConnect
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384363(v=vs.85).aspx

FTP サーバからファイルをダウンロード

FtpGetFile
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384157(v=vs.85).aspx

FTP サーバへファイルをアップロード

FtpPutFile
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384170(v=vs.85).aspx

インターネットサービスを閉じる

WinInet の使用を終了する。

InternetCloseHandle
https://msdn.microsoft.com/en-us/library/windows/desktop/aa384350(v=vs.85).aspx

その他インターネットに関する API の一覧

Constant

定数を VBA で再定義するというのが冗長でやりたくないが、仕方ないのだろうか。

WinInet Constant

File Attribute Constant

Sample Code

これら踏まえた上で、サンプルコードとしては下記がわかりやすい。
http://cya.sakura.ne.jp/vb/WinInet.htm

サンプルコード中の FTP 関連の定数がドキュメントでヒットしない。
INTERNET で再定義されていると思われる。

1
3
0

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
1
3