Option Explicit
Public Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, _
ByVal dwExtraInfo As Long)
Const ALT_KEY = &HA4 '//[Alt]キー
Const PRINT_SCRN_KEY = &H2C '//[PrintScrn]キー
Const KEY_DOWN = &H1 '// キーを押す
Const KEY_UP = &H2 '// キーを放す
Private File_Path As String
Private FSO As Scripting.FileSystemObject
Public Sub AAA()
Dim saveFolderPath As String
Dim imgFileName As String
Dim imgType As String
Set FSO = New Scripting.FileSystemObject
saveFolderPath = "C:\Users\User\Desktop\画像保存"
imgFileName = "test.png"
imgType = "png"
File_Path = FSO.BuildPath(saveFolderPath, imgFileName)
Call ScreenShot(imgType, True)
With ThisWorkbook.Sheets("貼り付け")
'作成したスクショ画像の貼り付け
.Activate
.Range("A1").Select
.Pictures.Insert(File_Path).Select
End With
MsgBox "[貼り付け]シートにスクショ画像を貼り付けました。", vbInformation, "画像"
End Sub
'// クリップボードの画像を保存する
Sub ScreenShot(ByVal img_type As String, _
Optional ByVal alt As Boolean)
Dim psCmdStr As String '// PowerShellコマンド
''特定のファイルをスクショする
'Workbooks("bbb.xlsx").Activate
If alt Then keybd_event ALT_KEY, 0, KEY_DOWN, 0
keybd_event PRINT_SCRN_KEY, 0, KEY_DOWN, 0
keybd_event PRINT_SCRN_KEY, 0, KEY_DOWN Or KEY_UP, 0
If alt Then keybd_event ALT_KEY, 0, KEY_DOWN Or KEY_UP, 0
'// PowerShellのクリップボード画像保存コマンド
psCmdStr = "powershell Add-Type -AssemblyName System.Windows.Forms;$ImagePath = '" & File_Path & _
"'; [Windows.Forms.Clipboard]::GetImage().Save($ImagePath, [System.Drawing.Imaging.ImageFormat]::" & _
img_type & ")"
'// PowerShellコマンド実行
CreateObject("WScript.Shell").Run Command:=psCmdStr, WindowStyle:=0, WaitOnReturn:=True
End Sub
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme