|
本帖最后由 过客fppt 于 2023-8-25 17:53 编辑
各位老师,我又来了,因为表格上传到系统之后会直接消失,所以我还需要将表格也批量转化为图片,我目前完成的代码如下,遇到的问题有:
1、有些表格能居中,但是有些不能居中,代码会报错
2、生成图片之后,两边的区域不对称,导致对图片进行裁剪之后不准确
希望各位老师指点一下,先谢谢各位了
- Sub 全部表格转化为图片()
- Dim CropWidth As Double
- Dim myDoc, tempDoc As Document
- Dim tbl As Table
- Dim yuanRange As Range
- Dim l As Long
- Dim isNO1 As Boolean
- Dim i As Integer
-
- Set myDoc = ActiveDocument
- isNO1 = True
- For Each tbl In myDoc.Tables
- tbl.Select
- l = Selection.Range.Start
- '选中内容之后
- Set yuanRange = Selection.Range
-
- If isNO1 Then
- Set tempDoc = Documents.Add()
- isNO1 = False
- End If
- '将选中的内容到临时文档
- tempDoc.Content.FormattedText = yuanRange
-
- With Selection
- .WholeStory ' 选中全部
- .Range.Rows.Alignment = wdAlignRowCenter
- .ParagraphFormat.Alignment = wdAlignParagraphCenter
-
- '获取裁剪宽度CropWidth
- CropWidth = tempDoc.Content.Information(wdHorizontalPositionRelativeToTextBoundary)
- tempDoc.Tables(1).Select
- .Copy
- .EndKey Unit:=wdStory, Extend:=wdMove '到文档末端
- .PasteSpecial DataType:=wdPasteMetafilePicture
- End With
-
- If CropWidth > 0 Then
- tempDoc.InlineShapes(1).PictureFormat.CropRight = CropWidth
- tempDoc.InlineShapes(1).PictureFormat.CropLeft = CropWidth
- End If
- tbl.Delete
- myDoc.Range(l, l).FormattedText = tempDoc.InlineShapes(1).Range.FormattedText
- i = i + 1
- Next
-
- Set myDoc = Nothing
- Set tempDoc = Nothing
- MsgBox "已完成" & i & "个表格转化为图片!"
- End Sub
复制代码
|
|