|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- <div class="blockcode"><blockquote>Private Sub Document_Open() '打开事件
- '为宏指定快捷键(Alt+Q\B)
- KeyBindings.Add wdKeyCategoryMacro, "通过API访问粘贴板并另存图片数据", BuildKeyCode(wdKeyAlt, wdKeyQ)
- KeyBindings.Add wdKeyCategoryMacro, "复制到Excel后输出1", BuildKeyCode(wdKeyAlt, wdKeyB)
- MsgBox "创建的快捷键如下:" & Chr(13) & "方法1:通过API访问粘贴板并另存图片数据一>Alt+Q" & Chr(13) _
- & "方法2:复制到Excel后输出1一>Alt+B", , "快捷键"
- End Sub
- Sub 查找和清除快捷键()
- MsgBox FindKey(BuildKeyCode(wdKeyF1)).Command '查找
- FindKey(BuildKeyCode(wdKeyAlt, wdKeyQ)).Clear '清除
- End Sub
- Private Type GUID
- Data1 As Long
- Data2 As Integer
- Data3 As Integer
- Data4(0 To 7) As Byte
- End Type
- Private Type GdiplusStartupInput
- GdiplusVersion As Long
- DebugEventCallback As Long
- SuppressBackgroundThread As Long
- SuppresexternalCodecs As Long
- End Type
- Private Type EncoderParameter
- GUID As GUID
- NumberOfValues As Long
- type As Long
- Value As Long
- End Type
- Private Type EncoderParameters
- Count As Long
- Parameter As EncoderParameter
- End Type
- Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
- Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
- Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByValhpal As Long, Bitmap As Long) As Long
- Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
- Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal FileName As Long, clsidEncoder As GUID, encoderParams As Any) As Long
- Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
- Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Long
- Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As Long
- Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function CloseClipboard Lib "user32" () As Long
- Const CF_BITMAP = 2
- '①通过Windows API访问粘贴板中的图片数据
- '基本思路是在doc中找到需要保存的图片,复制到粘贴板后,调用API从粘贴板中抓出数据并另存为。
- '此方法中Inlineshape和shape会表现不同的特性,具体讲,就是前者会以原始分辨率输出,后者以doc中所表现的分辨率输出。
- '但相对于第一种方法,可加入图片筛选等行为。
- '下面调用API的代码,实现从单个doc中导出全部图片。
- Sub 通过API访问粘贴板并另存图片数据()
- Dim tSI As GdiplusStartupInput
- Dim lRes As Long
- Dim lGDIP As Long
- Dim lBitmap As Long
- Dim hBitmap As Long
- Dim FSO As Object, WordDoc As Object, ExcelApp As Object, sFolder$, sPath$, FileName$, inShp As InlineShape, N As Long
- Set FSO = CreateObject("Scripting.FileSystemObject") '创建文件系统对象
- sPath = ThisDocument.Path '代码所在的Word文件路径
- sFolder = sPath & "" & Split(ThisDocument.Name, ".")(0) '文件夹路径
- If FSO.FolderExists(sFolder) Then FSO.DeleteFolder sFolder '当文件夹存在时,则删除文件夹
- FSO.CreateFolder sFolder '创建文件夹
- ' MkDir sFolder '创建文件夹
- Set WordDoc = Documents.Open(sPath & "" & "如何导出图片并自动命名.docx", Visible:=False) '打开指定路径下的Word文件,并不显示
- Set ExcelApp = CreateObject("Excel.Application") '创建ExcelApp程序
-
- '获取所有图片要重命名的名称
- Dim arr(), hhhh&, kkkk&, rrrr&, jjjj&, jj&, iiii&
- hhhh = WordDoc.InlineShapes.Count 'Word文件中所有的Inlineshape图片
- ReDim Preserve arr(1 To hhhh) '重定义数组
- For kkkk = 1 To WordDoc.Tables.Count '在Word文件中所有的表格中循环
- For rrrr = 1 To WordDoc.Tables(kkkk).Range.Rows.Count '在每个表格的所有行中循环
- jjjj = WordDoc.Tables(kkkk).Cell(rrrr, 2).Range.InlineShapes.Count '获取每个表格中第2栏中单元格中的图片数量
- If jjjj > 1 Then '当图片数量> 1 时
- For jj = 1 To jjjj
- iiii = iiii + 1
- A = WordDoc.Tables(kkkk).Cell(rrrr, 1).Range.Text & jj '图片要重命名的名称+序号
- arr(iiii) = ExcelApp.WorksheetFunction.Clean(A) '目的提取不带不可见的字符
- Next
- ElseIf jjjj = 1 Then '当图片数量=1 时
- iiii = iiii + 1
- A = WordDoc.Tables(kkkk).Cell(rrrr, 1).Range.Text
- arr(iiii) = ExcelApp.WorksheetFunction.Clean(A)
- Else
- End If
- Next
- Next
-
- For Each inShp In WordDoc.InlineShapes '在打开的Word文件中所有的【嵌入型】图片中遍历
- '如果是shape,此处改为 ActiveDocument.Shapes,当然inShp的声明也要改
- N = N + 1
- ' A = inShp.Range.Cells(1).Range.Text '图片所在的表格中的单元格的文本
- ' AA = Trim(Mid(A, 2, Len(A) - 3)) '目的提取不带不可见的字符
- AA = arr(N)
- FileName = sFolder & "" & AA & ".jpg"
- ' FileName = sFolder & "" & Format(N, "000.jpg") '代码Word文件所在的路径+流水号+(.gif、.jpg、.jpeg、.png、.bmp、.tif等)
- inShp.Range.CopyAsPicture '复制为图片
- '如果为shape,CopyAsPicture方法无效,可能因为shape本身就是pic,
- '所以对于shape直接select,再copy,即以图片形式装入了粘贴板,
- '后续操作相同。
- OpenClipboard 0& '打开粘贴板
- hBitmap = GetClipboardData(CF_BITMAP) '获取粘贴板的数据
- CloseClipboard '关闭粘贴板
- tSI.GdiplusVersion = 1
- lRes = GdiplusStartup(lGDIP, tSI, 0)
- If lRes = 0 Then
- lRes = GdipCreateBitmapFromHBITMAP(hBitmap, 0, lBitmap)
- If lRes = 0 Then
- Dim tJpgEncoder As GUID
- Dim tParams As EncoderParameters
- CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder
- tParams.Count = 1
- With tParams.Parameter
- CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
- .NumberOfValues = 1
- .type = 4
- .Value = VarPtr(100)
- End With
- lRes = GdipSaveImageToFile(lBitmap, StrPtr(FileName), tJpgEncoder, tParams)
- GdipDisposeImage lBitmap
- End If
- GdiplusShutdown lGDIP
- End If
- Next inShp
- WordDoc.Close (0) '关闭活动Word文件,并不保存
- Set ExcelApp = Nothing
- MsgBox "图片已另存为成功!" & Chr(13) & "一共 " & N & " 个", 64, "提示"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|