Excel是否可以实现屏幕任意区域截图?听起来似乎有些不务正业,已经有那么多截图软件,并且微信、QQ等也都提供了截图功能,但是存在的就是合理的,借助强大的PowerShell就可以实现任意区域截图功能。

示例代码如下。

Sub ScreenShot(strFile, pos_x, pos_y, image_width, image_height)
    Dim strArgs As String
    Dim strPSCmd As String
    strArgs = Join(Array("'" & strFile & "'", pos_x, pos_y, image_width, image_height))
    strPSCmd = "function SSRegion($strFile, $pos_x, $pos_y, $image_width, $image_height) {" & _
            "Add-type -AssemblyName System.Drawing;" & _
            "$graphic = [System.Drawing.Graphics]::FromImage($bitmap);" & _
            "$graphic.CopyFromScreen($pos_x, $pos_y, 0, 0, $bitmap.Size);" & _
            "$bitmap = New-Object System.Drawing.Bitmap $image_width, $image_height;" & _
            "$bitmap.Save($strFile);}; SSRegion "
    Shell "powershell -Command " & strPSCmd & strArgs, vbHide
End Sub

【代码解析】
第4行代码将参数组合为一个空格分隔的字符串。
第5~10行代码为PowerShell命令。
第11行代码调用Shell执行PowerShell命令。
第14行代码调用ScreenShot过程完成截屏,截屏区域区域左上角屏幕坐标为(100,100),截图区域宽度和高度均为300像素。

运行Demo过程,悄无声息完成截图,是不是有点儿酷!

Sub Demo()
    ScreenShot "C:\Temp\SquareScrShot.png", 100, 100, 300, 300
End Sub

代码仅为功能演示,并未考虑截图区域是否会超出屏幕分辨率等细节问题,大家可以自行完善。

Excel是否可以实现屏幕任意区域截图?听起来似乎有些不务正业,已经有那么多截图软件,并且微信、QQ等也都提供了截图功能,但是存在的就是合理的,借助强大的PowerShell就可以实现任意区域截图功能。
Private Declare Sub keybd event Lib “user32” (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Sub CopyAndPaste() Workbooks(“拷贝源文件.xlsx”).Activate Workbooks(“拷贝源文件.xlsx”).Sheets(1).Select Application.Wait (Now + Tim
原贴http://club.excelhome.net/thread-1193134-1-1.html 可以全屏截图和当前窗口截图 Private Declare Sub keybd_event Lib "user32" _ (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As... Option Explicit Private Type PointX As DoubleY As DoubleEnd Type Private Type LinesP1 As PointP2 As PointEnd Type Const CROSS As Long = 0 '相交Const COLINE As Long = 1 '共线Const PARALLEL As L... 块Module1.bas,以下程序放入标准模块内 '创建BMP位图 Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture Dim r As Long Dim Pic As PicBmp Dim IPic As IPicture Dim IID_IDispatch As GUID '填充IDispatch界面 With IID_IDispatch .Data1 = &H20400; .Data4(0) = &HC0; .Data4(7) = &H46; End With '截图处理 Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal _ LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc _ As Long) As Picture Dim tmpPicture As Picture 'Public Const snapFolder = "c:/Snap" '截图保存位置 '捕捉活动窗口 Set tmpPicture = CaptureActiveWindow() '捕捉整个屏幕 Set tmpPicture = CaptureScreen() SavePicture tmpPicture, "FileName完整路径名称"
Introduces the basic concepts of VBA, the main characteristics and the basic steps VBA application development and application software. I. Overview 1. Excel 2000 Office 2000 Chinese version of Microsoft s first completely finished the Office suite, is one under Windows 95 / Windows NT environment, the most widely used software running on
Range 对象是 Excel 应用程序中最常用的对象,一个 Range 对象代表一个单元格、一 行、一列、包含一个或者更多单元格区域(可以是连续的单元格,也可以是不连续的单元格) 中选定的单元格,甚至是多个工作表上的一组单元格,在操作 Excel 内的任何区域之前都 需要将其表示为一个 Range 对象,然后使用该 Range 对象的方法和属性
1. 首先需要导入Windows API库: Private Declare PtrSafe Function BitBlt Lib "GDI32" Alias "BitBlt" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Private Declare PtrSafe Function GetDC Lib "user32" Alias "GetDC" (ByVal hWnd As Long) As Long Private Declare PtrSafe Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hWnd As Long, ByVal hdc As Long) As Long 2. 接下来,需要使用以下代码来获取屏幕截图并将其保存到文件中: Public Sub ScreenShot() Dim hwnd As Long Dim hdcSrc As Long Dim hdcDest As Long Dim nWidth As Long Dim nHeight As Long Dim nLeft As Long Dim nTop As Long Dim lRet As Long '获取当前窗口句柄 hwnd = GetDesktopWindow() '获取屏幕DC hdcSrc = GetDC(hwnd) hdcDest = CreateCompatibleDC(hdcSrc) nWidth = 500 '截取的宽度 nHeight = 500 '截取的高度 nLeft = 500 '截取的左边距 nTop = 500 '截取的上边距 '创建一个设备兼容的DC Dim BMP As Byte ReDim BMP(nWidth * nHeight * 4) BMP(0) = &HFF BMP(1) = &HFF BMP(2) = &HFF BMP(3) = &H0 lRet = BitBlt(hdcDest, 0, 0, nWidth, nHeight, hdcSrc, nLeft, nTop, vbSrcCopy) 'ReleaseDC Call ReleaseDC(hwnd, hdcSrc) Call DeleteDC(hdcDest) 'Save to file Open "C:\myimage.bmp" For Output As #1 Put #1, , BMP Close #1 End Sub 3. 在执行完以上代码后,将会在C盘根目录下生成一个名为“myimage.bmp”的文件,其中就保存了屏幕截图的指定区域。 需要注意的是,由于屏幕分辨率不同,如果要在不同的屏幕上使用截图功能,可能需要根据实际情况进行一些参数调整。 Dim ra As Range Dim allrowscount As Integer allrowscount = ActiveSheet.UsedRange.Rows.Count Range("a2:a" & allrowscount).SpecialCells(xlCellTypeVisible).Select MsgBox Selection.Cells.Count End Sub VBA获取筛选后记录数量 秋-Cool: 不知为何结果是错误的 VBA轻松实现多色渐变填充 王新峰447: 如何使Office文档默认“不压缩文件中的图像” 幻灭星宇: 可以在HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\PowerPoint\Options里面进行设置,就可以让PPT也不压缩了