|
本帖最后由 loquat 于 2023-8-20 00:23 编辑
楼主这个思路有新意。
- Option Explicit
- #If VBA7 And Win64 Then
- Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
- #Else
- Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
- #End If
- Sub EQ域转图片(oField As Word.Field)
- If oField.Type <> wdFieldFormula Then Exit Sub '仅处理EQ域
- Dim rng As Word.Range
- Dim i As Long
- Dim j As Long
- Dim k As Long
- Dim NewFontSize As Double
- Dim iWidth As Double
- Dim iStep As Double
- Dim leftDistance As Double
- Dim rightDistance As Double
- Dim lMargin As Double
- Dim ratio As Double
-
- Set rng = oField.Code
- rng.SetRange rng.Start - 1, rng.End + 1 '扩展到域的外面
- rng.InsertBefore vbLf '前后插入换行
- rng.InsertAfter vbLf
- j = rng.Start + 1 '记住当前位置,一会儿要用到
- k = rng.End - 1 '记住当前位置,一会儿要用到
- rng.SetRange j, k '铲掉前后换行符
- rng.ParagraphFormat.Reset '去除编号、项目符号等
- rng.Paragraphs(1).CharacterUnitFirstLineIndent = 0
- rng.Paragraphs(1).FirstLineIndent = 0
- rng.Paragraphs(1).Alignment = wdAlignParagraphCenter '居中对齐
- Do
- i = i + 1
- lMargin = rng.PageSetup.LeftMargin
- rng.Document.Range(j, j).Select '竟然不用Select得不出来正确的值,BUG
- leftDistance = Selection.Information(wdHorizontalPositionRelativeToPage) - lMargin ' 获取选定区域左侧与页面左边缘的距离(以磅为单位)
- If iWidth = 0 Then
- rng.Document.Range(k, k).Select
- rightDistance = Selection.Information(wdHorizontalPositionRelativeToPage) - lMargin
- iWidth = rightDistance - leftDistance '记录原来宽度
- End If
- If leftDistance < 5 Or i >= 128 Then
- rng.CopyAsPicture
- Sleep 1000
- rng.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=wdInLine
- rng.SetRange rng.Start - 1, rng.End '设置为图片范围
- With rng.InlineShapes(1)
- ratio = .Height / .Width
- .Width = iWidth
- .Height = iWidth * ratio
- End With
- rng.Document.Range(j + 1, j + 2).Delete
- rng.Document.Range(j - 1, j).Delete
- Exit Do
- End If
- NewFontSize = rng.Font.Size '当前字体大小
- iStep = leftDistance * 0.6 '放大60%
- '改天优化
- If leftDistance < 30 And iStep > 0 Then
- If iStep < 10 Then
- iStep = 10
- ElseIf iStep < 5 Then
- iStep = 5
- End If
- End If
- rng.Font.Size = NewFontSize + iStep
- Loop
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|