|
可试试如下代码,是用GetPoint方法获得区域位置参数并借用网上截屏代码编写,只作简单测试,效率不高。Win11系统运行时库文件出错
- Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, _
- ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
- Enum JpMode
- theScreen = 0 '全屏截图
- theForm = 1 '当前焦点窗口截图
- End Enum
- Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
- Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
- Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, _
- RefIID As Guid, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
- Private Const CF_BITMAP = 2
- Private Type PicBmp
- Size As Long
- Type As Long
- hBmp As Long
- hPal As Long
- Reserved As Long
- End Type
- Private Type Guid
- Data1 As Long
- Data2 As Integer
- Data3 As Integer
- Data4(0 To 7) As Byte
- End Type
-
- Function ApiGetClipBmp() As IPicture
- On Error Resume Next
- Dim Pic As PicBmp, IID_IDispatch As Guid
- OpenClipboard 0
- With IID_IDispatch
- .Data1 = &H20400
- .Data4(0) = &HC0
- .Data4(7) = &H46
- End With
- With Pic
- .Size = Len(Pic)
- .Type = 1
- .hBmp = GetClipboardData(CF_BITMAP)
- End With
- OleCreatePictureIndirect Pic, IID_IDispatch, 1, ApiGetClipBmp
- CloseClipboard
- End Function
-
- Function KeyJp(Optional ByVal TheMode As JpMode = theScreen) As IPictureDisp
- '版权所有,请保留作者信息.QQ:1085992075 '原声明
- Call keybd_event(vbKeySnapshot, TheMode, 0, 0) '
- DoEvents
- End Function
- Function RangeToPic(rngTarget As Range) As Variant
- Dim i%, l&, t&, w&, h&, isWps As Boolean
- If InStr(LCase(Application.Path), "wps") > 0 Then isWps = True
- 'rngTarget.Select
- With ActiveDocument
- .ActiveWindow.View.Zoom = 100
- .ActiveWindow.GetPoint l, t, w, h, Selection.Range
- Selection.Collapse wdCollapseEnd
- Call KeyJp(theScreen)
- Call ApiGetClipBmp
- rngTarget.Select
- Selection.PasteSpecial DataType:=wdPasteBitmap
- If isWps Then Selection.MoveStart 1, -1 Else Selection.MoveEnd 1, 1
- With Selection.InlineShapes(1)
- .LockAspectRatio = msoFalse
- .ScaleWidth = 100
- .ScaleHeight = 100
- .PictureFormat.CropLeft = PixelsToPoints(l)
- .PictureFormat.CropTop = PixelsToPoints(t)
- .PictureFormat.CropRight = .Width - PixelsToPoints(w)
- .PictureFormat.CropBottom = .Height - PixelsToPoints(h)
- .Range.ParagraphFormat.BaseLineAlignment = wdBaselineAlignCenter
- .Select
- End With
- SendKeys "{ENTER}"
- Application.CommandBars.Item("Picture").Controls.Item(10).Execute '压缩图片
- End With
- End Function
- Sub FieldFormulaToPic()
- '将域公式内容转换为图片,仅页内矩形区域;测试环境:win10 x64, WPS 11.1
- Dim aField As Field
- For Each aField In ActiveDocument.Fields
- If aField.Type = wdFieldFormula Then
- aField.Select
- Call RangeToPic(Selection.Range)
- End If
- Next
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|