0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AccessのVBAでコマンドプロンプトのコマンド実行(コマンド実行結果の取得についても)

Posted at

やり方

まず『コマンドプロンプトを使う為のクラス』が使えるように参照設定。
Windows Script Host Object Model
image.png

サンプルコード
'コマンドプロンプトを使う為のクラス。
Dim wsh_shell_ As New IWshRuntimeLibrary.WshShell

'実行結果保持用。
Dim shell_result_ As IWshRuntimeLibrary.WshExec

'フォルダーを作成するコマンド。
Dim command_ As String
command_ = "mkdir C:\Users\d\Desktop\bat\test"

'コマンド実行して結果を保持。
Set shell_result_ = wsh_shell_.Exec("%ComSpec% /c " & command_)

'結果確認。
Debug.Print shell_result_.ExitCode

ExitCode 以外にも下記のようなものが取得できる。
image.png

Run()

WshShellには Run() という関数もある。

関数名 コマンドプロンプト画面 コマンド実行結果取得
Run() 表示されない できない
Exec() 表示される できる

蛇足

『もしコマンドが失敗したらユーザーに通知 or メール通知 or ログ出力』
という要件が後から追加されることがある。

個人的には最初からExec()で作っておいた方がいいかなあって思う。

参考サイトさん

バージョン

Windows 10 Pro 21H2 OSビルド 19045.2251
Microsoft Access for Microsoft 365 MSO (バージョン 2209 ビルド 16.0.15726.20068) 32 ビット

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?