|
楼主 |
发表于 2024-3-5 19:15
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub EQ公式转图形V30(Optional ByVal waittime As Single = 0.2)
- '
- ' EQ公式转嵌入式图形
- '
- If Selection.Fields.count = 0 Then MsgBox "请选择含有公式的内容!": End
-
- ' On Error Resume Next
- 'On Error GoTo ErrorEnd
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
-
- Dim count%, rStart&, pos&, i&, Zoom!, isWord As Boolean
- Dim PSize%, PWidth%, LMargin%, RMargin%, LayoutWidth%, CropWidth%
- Dim Mydoc, MyDoc1, tempDoc As Document, myField As Field
-
-
- Set Mydoc = ActiveDocument
-
- Dim yuanRange As Range
- '选中内容之后
- Set yuanRange = Selection.Range
-
- ' With Selection.PageSetup
- ' PSize = .PaperSize: PWidth = .PageWidth
- ' LMargin = .leftMargin: RMargin = .rightMargin
- ' LayoutWidth = PWidth - LMargin - RMargin
- ' End With
- ' 输入标准宽度
- PSize = 7: PWidth = 595
- LMargin = 90: RMargin = 90
- LayoutWidth = PWidth - LMargin - RMargin
-
- Set MyDoc1 = Documents.Add(Visible:=True)
-
- '将选中的内容赋值给mydoc1
- MyDoc1.Content.FormattedText = yuanRange
-
- Selection.WholeStory ' 选中全部
-
-
- Set tempDoc = Documents.Add(Visible:=False)
- With tempDoc.PageSetup
- .PaperSize = PSize: .PageWidth = PWidth
- .leftMargin = LMargin: .rightMargin = RMargin
- End With
-
- MyDoc1.Activate
- MyDoc1.ActiveWindow.DisplayVerticalScrollBar = False ' 隐藏垂直滚动条(提高速度)
- isWord = CheckApplicationisword
- i = 1: rStart = -1
- zhunhuaCo = 0
- Call 是加图(True)
-
- With Selection
- ' 添加辅助段落符(解决当选区开始就是域代码时,只能转换第一个的问题)
- If .Start <> .End And .Start >= .Fields(1).Code.Start - 1 Then
- rStart = .Fields(1).Code.Start - 1
- ActiveDocument.Range(rStart, rStart).InsertParagraphBefore
- .SetRange rStart, .End
- End If
- .ParagraphFormat.BaseLineAlignment = wdBaselineAlignCenter ' 段落中心对齐
- Do
- Set myField = .Fields(i)
- If myField.Type <> wdFieldFormula Then
- i = i + 1
- Else
- ' 计算公式宽度
- With tempDoc
- .Fields.Add .Content, wdFieldEmpty, "", False
- .Fields(1).Code.FormattedText = myField.Code
- .Fields(1).ShowCodes = False
- With .Content
- .ParagraphFormat.Alignment = wdAlignParagraphRight
- CropWidth = .Information(wdHorizontalPositionRelativeToTextBoundary) - 1
- ' CropWidth = CropWidth * 0.85213 '适配在WPS中的运行
- End With
- End With
- Line1:
-
- ' 公式转图形
- With myField
- pos = .result.Start: .Copy: Wait (IIf(isWord, 0.05, waittime))
- End With
- MyDoc1.Range(pos, pos).PasteSpecial _
- DataType:=wdPasteEnhancedMetafile, Placement:=wdInLine
-
- Dim i1 As Integer
-
- If 是加图(False) = 0 Then
- i1 = i1 + 1
- If i1 > 16 Then
- MsgBox "无法粘贴为图片,请检查!"
- End If
- GoTo Line1
- End If
-
- count = MyDoc1.Range(0, pos).InlineShapes.count + 1
- MyDoc1.InlineShapes(count).Title = "QQ84299244_EQ" ' 设置名称便于以后统一对图形进行再处理
-
- If CropWidth > 0 Then
- With MyDoc1.InlineShapes(count)
- If isWord Then
- .PictureFormat.CropRight = CropWidth
- Else
- .Reset: Zoom = .Width / LayoutWidth
- .PictureFormat.CropRight = CropWidth * Zoom
- .Width = .Width / Zoom: Wait (0.1)
- End If
- End With
- End If
- zhunhuaCo = zhunhuaCo + 1
-
- If zhunhuaCo Mod 20 = 0 Then
- Wait (2)
- Debug.Print "累计已完成公式转化数量:" & zhunhuaCo
- End If
-
- myField.Delete
- End If
- Loop Until i > .Fields.count
- If rStart >= 0 Then ActiveDocument.Range(rStart, rStart).Delete ' 删除辅助段落符
- End With
- tempDoc.Close False
- MyDoc1.ActiveWindow.DisplayVerticalScrollBar = True ' 恢复垂直滚动条
-
- ErrorEnd:
- Set Mydoc = Nothing
- Set MyDoc1 = Nothing
- Set tempDoc = Nothing
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- Err.Clear: On Error GoTo 0
- Debug.Print "已完成" & zhunhuaCo & "个公式的转化!请注意另存文件,用于上传到系统!"
- ' MsgBox "已完成" & zhunhuaCo & "个公式的转化!请注意另存文件,用于上传到系统!"
- End Sub
- Function 是加图(初始化 As Boolean) As Boolean
- ' '初始化
- ' If 初始化 = True Then
- ' tupianCo0 = 现在图片总数(100)
- ' tupianCo1 = 0
- ' tupianCo2 = 0
- ' index = 0
- ' 是加图 = False
- ' Else
- ' tupianCo2 = 现在图片总数(100) - tupianCo0 + tupianCo2
- ' '逻辑判断
- ' If tupianCo2 > tupianCo1 Then
- 是加图 = True
- ' Else
- ' 是加图 = False
- ' End If
- ' index = index + 1
- ' tupianCo1 = tupianCo2
- ' End If
- ' Debug.Print "运行第" & index & "次,图片增加了? " & 是加图
- End Function
- Public Function 现在图片总数(宽度大于 As Integer) As Integer
- Dim shp As InlineShape
- Dim i As Integer
- For Each shp In ActiveDocument.InlineShapes
- If shp.Width > 宽度大于 Then
- i = i + 1
- End If
- Next shp
- 现在图片总数 = i
- End Function
- Sub Wait(t As Single)
- Dim time1!, time2!
- time1 = Timer
- Do
- DoEvents
- time2 = Timer - time1
- If time2 < 0 Then time2 = time2 + 86400 ' 86400=24*3600
- Loop While time2 < t
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|