如何捕获PowerView图像?

时间:2015-10-02 14:17:20

标签: excel vba screenshot powerview

我正在生成Excel PowerView仪表板。我认为对于那些只想快速查看仪表板截图的用户来说会很有用,其中有一个链接可供更多分析人员下载excel文件以重新编译或深入了解他们心中的满足感。

我发现了一个允许我捕获屏幕截图并将其保存到本地文件的宏,但屏幕截图是我的整个桌面或活动应用程序。这两个看起来很糟糕,我只需要捕获PowerView(见下图):enter image description here

有没有办法从VBA中仅捕获屏幕的某些部分?

1 个答案:

答案 0 :(得分:0)

蟋蟀 ....

好的,我最终弄清楚了:

步骤1)在您的计算机上安装Imagemagick。请确保包含COM组件,以便您可以在VBA中使用它。

步骤2)在VBE中注册参考(工具 - >参考 - >找到ImageMagick并启用它)

步骤3)关闭excel并重新打开它。

步骤4)在VBA中创建以下宏,并将其分配给快速启动栏上的按钮。

Sub AltPrintScreen()
Dim a, b

    ' First, take a screenshot and save it
    a = Replace(ActiveWorkbook.Name, ".xlsx", "")
    Set b = ActiveWorkbook
    keybd_event VK_MENU, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, 0, 0
    keybd_event VK_SNAPSHOT, 0, KEYEVENTF_KEYUP, 0
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0

    Dim chtObj As ChartObject

    With b.Worksheets(1)

    .Activate

    ' My resolution is 1366 x 768, adjust this part as per ur rez
    Set chtObj = .ChartObjects.Add(100, 30, 1000, 568)
    chtObj.Name = "TemporaryPictureChart"

    ActiveSheet.ChartObjects("TemporaryPictureChart").Activate
    ActiveChart.Paste

    ActiveChart.Export Filename:=ThisWorkbook.Path & "\" & a & ".jpg", FilterName:="jpg"
End With
chtObj.Delete

'Next, use ImageMagick to crop the image to get only the part you want.
Dim img
Set img = CreateObject("ImageMagickObject.MagickImage.1")
img.Convert ThisWorkbook.Path & "\" & a & ".jpg", "-crop", "845x645+270+62^", "+repage", ThisWorkbook.Path & "\" & a & ".jpg"
Set img = Nothing
Sheets("Power View1").Activate

End Sub

步骤5)然后,从要进行屏幕截图的工作表中,单击运行宏的小按钮。应该这样做(虽然你可能不得不调整参数)。

SIDE NOTE :直接从屏幕截图转到ImageMagick会更容易,例如:

Dim a, img
a = Replace(ActiveWorkbook.Name, ".xlsx", "")
Selection.CopyPicture xlScreen, xlBitmap
Set img = CreateObject("ImageMagickObject.MagickImage.1")
img.Convert "clipboard:myimage", "-crop", "845x645+270+62^", "+repage", ThisWorkbook.Path & "\" & a & ".jpg"

但遗憾的是,您无法在PowerView标签中捕获范围。此外,如果您使用ImageMagick,请务必使用xlBitmap,因为它无法识别xlPicture(不是位图)。