1
5

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 3 years have passed since last update.

VBAでRPA

Posted at

目的

RPAに使えそうな機能を使いやすくした実装を作った際の備忘録。
以下3つが比較的有用だったので、記しています

  1. SendKeysのWrapper(擬似キー操作用)
  2. SendKeysでPrintScreen
  3. Processの存在チェック

1. SendKeysのWrapper(擬似キー操作用)

VBAからSendKeyを使った場合に、NumLockが外れるという現象があるようなので
それをリカバリする機能を盛り込む必要があったのでそれに対応したWrapperを実装
GetKeyboardState を使って、NUMLOCKキーチェック後、必要であれば{NUMLOCK}を送信する簡易的なものです

Private Declare Function GetKeyboardState Lib "user32" _
   (pbKeyState As Byte) As Long
Const VK_NUMLOCK = &H90

Function IsNumLockOn() As Boolean
	Dim keys(0 To 255) As Byte
	GetKeyboardState keys(0)
	IsNumLockOn = keys(VK_NUMLOCK)
End Function

Sub SendKeysWrapper(keys)
    Dim wasNumLock
    wasNumLock = IsNumLockOn()
    
    Application.SendKeys keys, True
    If wasNumLock = True Then
       Application.SendKeys "{NUMLOCK}", True
    End If
End Sub

2. SendKeysでPrintScreen

これが意外に見つからなくてstackoverflowで見つけたので

Public Sub PrintScreenWrapper()
    Application.SendKeys "%{1068}" , True
End Sub

3. Processの存在チェック

WIN32APIじゃなくても出来るプロセスチェック
サンプルなので、実際ではword objectはキャッシュした方が無論使い勝手は良いです。

Function IsProcessAvailable(name)
    Set word = CreateObject("Word.Application")
    IsProcessAvailable = word.Tasks.Exists(name)
    word.Quit
	Set word = Nothing
End Function
1
5
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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?