Private Declare PtrSafe Function OpenClipboard Lib "USER32" (Optional ByVal hwnd As Long = 0) As Long
Private Declare PtrSafe Function CloseClipboard Lib "USER32" () As Long
Private Declare PtrSafe Function EmptyClipboard Lib "USER32" () As Long
Private Declare PtrSafe Function GetInputState Lib "USER32" () As Long
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)
Dim isContinue_ As Boolean
Dim targetSheet_ As Worksheet
Dim pasteRow_ As Integer
Const pasteCol_ As Integer = 2
Sub Kicker()
MsgBox "AutoCaptureを開始"
Call Preparation
Call AutoCapture
End Sub
Private Sub AutoCapture()
Dim cbFormat As Variant
Dim sh As Worksheet
cbFormat = Application.ClipboardFormats
If cbFormat(1) <> -1 Then
targetSheet_.Paste Destination:=targetSheet_.Cells(pasteRow_, pasteCol_)
UpdateRow (cbFormat)
OpenClipboard
EmptyClipboard
CloseClipboard
End If
DoEvents
If isContinue_ = True Then
Application.OnTime DateAdd("s", 1, Now), "AutoCapture"
End If
End Sub
Private Sub Preparation()
Dim ws As Worksheet, isExist As Boolean, name As String
pasteRow_ = 1
isContinue_ = True
OpenClipboard
EmptyClipboard
CloseClipboard
With ThisWorkbook
name = .Sheets("menu").Cells(8, 7).Value
For Each ws In .Worksheets
If ws.name = name Then isExist = True
Next ws
If isExist = True Then
Set targetSheet_ = .Sheets(name)
With targetSheet_
If IsEmpty(.Cells(1, 1)) Then
pasteRow_ = 1
Else
pasteRow_ = .Cells(1, 1).Value
End If
End With
Else
Worksheets.Add After:=Worksheets(Worksheets.Count)
ActiveWindow.View = xlPageBreakPreview
.ActiveSheet.name = name
Set targetSheet_ = .ActiveSheet
.Sheets("menu").Select
End If
End With
End Sub
Sub Kill()
isContinue_ = False
With targetSheet_.Cells(1, 1)
.Locked = False
.Font.ColorIndex = 2
.Value = pasteRow_
.Locked = True
End With
MsgBox "AutoCaptureを終了"
End Sub
Private Function UpdateRow(cbFormat As Variant)
Dim isImg As Boolean
isImg = False
With targetSheet_
For Each fmt In cbFormat
If fmt = xlClipboardFormatBitmap Then
lastImg = .Shapes.Count
imgHeighet = .Shapes(lastImg).Height
cellHeight = .Cells(1, 1).RowHeight
pasteRow_ = pasteRow_ _
+ Round(imgHeighet / cellHeight) _
+ 3
isImg = True
End If
Next
If isImg = False Then
With .Cells(pasteRow_, pasteCol_).CurrentRegion
pasteRow_ = .Row + .Rows.Count
End With
End If
End With
End Function
Sub SaveSheet()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim filePath As String
filePath = ThisWorkbook.Sheets("menu").Cells(9, 7).Value
With Workbooks.Add
For Each sh In ThisWorkbook.Worksheets
If sh.name <> "menu" Then
sh.Copy After:=.Sheets(.Worksheets.Count)
End If
Next
.Sheets("Sheet1").Delete
.SaveAs Filename:=filePath, _
FileFormat:=Excel.XlFileFormat.xlOpenXMLWorkbook
.Close
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub