|
把下面这个代码复制到标准模块中,然后运行ShowEqText()过程,可以在立即窗口中输出ThisDocument中所有公式中包含的文本,不同公式之间有“-----”隔开
- '内存函数
- Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
- Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
- '剪贴板函数
- Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function CloseClipboard Lib "user32" () As Long
- Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
- Private Const CF_ENHMETAFILE = 14
- '设备上下文函数
- Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
- Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
- Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
- Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
-
- '增强图元文件函数
- Private Declare Function CopyEnhMetaFile Lib "gdi32" Alias "CopyEnhMetaFileA" (ByVal hemfSrc As Long, ByVal lpszFile As String) As Long
- Private Declare Function DeleteEnhMetaFile Lib "gdi32" (ByVal hEmf As Long) As Long
- Private Declare Function EnumEnhMetaFile Lib "gdi32" (ByVal hdc As Long, ByVal hEmf As Long, ByVal lpEnhMetaFunc As Long, lpData As Any, lpRect As RECT) As Long
- Private Type ENHMETARECORD
- itype As Long
- nSize As Long
- End Type
-
- Private Type RECT
- Left As Long
- Top As Long
- Right As Long
- Bottom As Long
- End Type
- Private Const EMR_EXTTEXTOUTA = 83
- Private Const EMR_EXTTEXTOUTW = 84
- Sub ShowEqText()
- Dim hMem As Long
- Dim hEmf As Long
- Dim hdc As Long
- Dim hDCMem As Long
- Dim oRect As RECT
- Dim lRet As Long
-
- For i = 1 To ThisDocument.InlineShapes.Count
- If ThisDocument.InlineShapes(i).Field.Code.Text = " EMBED Equation.3 " Then
- ThisDocument.InlineShapes(i).Field.Copy
- If OpenClipboard(ByVal 0&) Then
- hMem = GetClipboardData(CF_ENHMETAFILE)
- If CBool(hMem) Then
- hEmf = CopyEnhMetaFile(hMem, vbNullString)
-
- End If
- CloseClipboard
- End If
- If hEmf <> 0 Then
- hdc = GetDC(0)
- hDCMem = CreateCompatibleDC(hdc)
- ReleaseDC 0, hdc
- With oRect
- .Left = 0
- .Top = 0
- .Right = 1
- .Bottom = 1
- End With
- Dim s As String
- lRet = EnumEnhMetaFile(hDCMem, hEmf, AddressOf EnhMetaFileProc, s, oRect)
- DeleteEnhMetaFile hEmf
- DeleteDC hDCMem
- Debug.Print "----------"
- End If
- End If
- Next
- End Sub
- Private Function EnhMetaFileProc(ByVal hdc As Long, ByVal lpHTable As Long, ByVal lpEMFR As Long, ByVal nObj As Long, lParam As String) As Long
- Dim nSize As Long
- Dim bytStr() As Byte
- Dim itype As Long
- Dim s As String
- Dim iASC As Integer
-
- CopyMemory itype, ByVal lpEMFR, 4
- If itype = EMR_EXTTEXTOUTW Then
- CopyMemory nSize, ByVal lpEMFR + 72, 4
- nSize = nSize - 76
- ReDim bytStr(1 To nSize) As Byte
- CopyMemory bytStr(1), ByVal lpEMFR + 76, nSize
- For i = 1 To nSize Step 2
- CopyMemory iASC, bytStr(i), 2
- s = s & ChrW(iASC)
- Next
- Debug.Print s
- ElseIf itype = EMR_EXTTEXTOUTA Then
- CopyMemory nSize, ByVal lpEMFR + 72, 4
- nSize = nSize - 76
- ReDim bytStr(1 To nSize) As Byte
- CopyMemory bytStr(1), ByVal lpEMFR + 76, nSize
- For i = 1 To nSize Step 2
- CopyMemory iASC, bytStr(i), 2
- s = s & Chr(iASC)
- Next
- End If
- EnhMetaFileProc = 1
- End Function
复制代码 |
|