使用Set - VBA Excel宏将裁剪的图像粘贴到活动单元格

时间:2017-05-22 15:00:31

标签: excel vba excel-vba

我有一个模块,当单击使用ActiveX控件设置的按钮时,该模块会截取左(主)监视器的屏幕截图。屏幕截图被裁剪并放入Excel电子表格中。模块代码(不带按钮):

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
  bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Const VK_SNAPSHOT = &H2C
Option Explicit
Sub foo()

Application.SendKeys "({1068})", True
DoEvents
ActiveSheet.Paste


Dim shp As Shape
With ActiveSheet
    Set shp = .Shapes(.Shapes.Count)

End With


shp.Height = 2500
shp.Width = 2500


Dim h As Single, w As Single, t As Single
w = -(350 - shp.Width) 'alter value to change horizontal axis of screenshot
h = -(475 - shp.Height)      'alter value to change vertical axis of screenshot


shp.LockAspectRatio = False
shp.PictureFormat.CropRight = w
shp.PictureFormat.CropBottom = h
shp.PictureFormat.CropTop = h

Set shp = ActiveCell

End Sub

我正在尝试将活动单元格(我单击的位置)设置为单击按钮后屏幕截图所在的位置。当前代码生成图像但不在正确的位置,因为屏幕截图被裁剪并且excel将“阴影”即未剪切的图像边界放置在活动单元格上。

尝试

Set shp = ActiveCell
裁剪图像后

会产生不匹配。为什么?我是否需要创建一个新变量来包含裁剪的形状?

0 个答案:

没有答案