EXCEL上で図形を配置し、範囲指定をして、その部分を画像ファイル化するマクロを何回か動作させると、
実行時エラー'1004'worksheetクラスのpasteメソッドが失敗しました。
や
実行時エラー'1004'RangeクラスのCopyPictureメソッドが失敗しました。
というエラーが表示され、図形がない空白の画像ファイルが作成されます。
その現象が起こるのは、特定のPCに限られています。
インストール時等で、なにか不足しているものがあったりするのでしょうか。
マクロは以下の内容です。
---------------------------------------------------------------------------------------------------------------
Sub 画像として保存()Dim m_SavePath As String
m_SavePath = Application.GetSaveAsFilename(, "PNGファイル (*.png), *.png")
If m_SavePath <> "False" Then
Call SaveSelectionAsImage(m_SavePath)
End If
End Sub
Public Sub SaveSelectionAsImage(ByVal argSavePath As String)
Dim m_Width As Double, m_Height As Double
' 選択範囲を画像としてコピー
'Selection.CopyPicture Appearance:=xlScreen, Format:=xlBitmap
' 一旦シートに貼り付け
'ActiveSheet.Paste
If Len(argSavePath) > 0 Then
'Application.ScreenUpdating = False
Selection.CopyPicture xlScreen, xlBitmap
ActiveSheet.Paste
With Selection
m_Width = .Width + 8: m_Height = .Height + 8
.CopyPicture xlScreen, xlBitmap
.Delete
End With
On Error Resume Next
With ActiveSheet.ChartObjects.Add(0, 0, m_Width, m_Height).Chart
.Paste
.ChartArea.Border.LineStyle = 0
.Export argSavePath, "PNG"
.Parent.Delete
End With
On Error GoTo 0
Application.ScreenUpdating = True
End If
End Sub
---------------------------------------------------------------------------------------------------------------
ご教授、よろしくお願いいたします。