How to get strings from equation through VBA code?

R

Robert L

Hi All,

I'd like to know if it is possible to get strings from equation that is
embedded in Word document using VBA code (Word 2000). I tried to record
macro, but it did not. Also I did not find any help on this. Does anyone
know it is possible at all and if so, how?

Thank you very much in advance.
Regards,
Robert
 
W

Word Heretic

G'day "Robert L" <[email protected]>,

Alt+F9 to reveal field codes.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Robert L reckoned:
 
M

macropod

hi Robert,

Try the following, which I picked up somewhere:

Sub FieldCodeToString()
Dim Fieldstring As String, NewString As String, CurrChar As String
Dim CurrSetting As Boolean, fcDisplay As Object
Dim MyData As DataObject, X As Integer
NewString = ""
Set fcDisplay = ActiveWindow.View
Application.ScreenUpdating = False
CurrSetting = fcDisplay.ShowFieldCodes
If CurrSetting <> True Then fcDisplay.ShowFieldCodes = True
Fieldstring = Selection.Text
For X = 1 To Len(Fieldstring)
CurrChar = Mid(Fieldstring, X, 1)
Select Case CurrChar
Case Chr(19)
CurrChar = "{"
Case Chr(21)
CurrChar = "}"
Case Else
End Select
NewString = NewString + CurrChar
Next X
Set MyData = New DataObject
MyData.SetText NewString
MyData.PutInClipboard
fcDisplay.ShowFieldCodes = CurrSetting
End Sub

Select the field then run the code. The code puts the field's coding into
the clipboard, from where you can paste it into whatever application you
want.

Cheers
PS: You'll need to include a vba reference to the Microsoft Forms 2.0 (or
later) object library.
 
J

Jay Freedman

hi Robert,

Try the following, which I picked up somewhere:

Sub FieldCodeToString()

All that is fine if Robert's "equation" is an EQ field (although even
then the field code doesn't look much like the printed equation). If
it's an Equation Editor object, all he'll get from the macro is the
string "EMBED Equation 3".

AFAIK, there's no way to extract the text of the equation from an
Equation Editor object. Maybe Bob Mathews of Design Science will drop
in to let us know whether MathType has an interface to VBA that can do
it.
 
R

Robert L

Hi,

thank you all three for your replies. But unfortunatelly the macropod's code
returns only "EMBED Equation 3", just like Jay supposed :) So it seems there
is no way for me to make my macro do this. It's a pity because the macro
extracts texts from pictures fine, but because of this, part of the job will
have to be done manually :(

Anyway, I thank you all three for your replies very much (and macropod for
the code. I will use it in some other macros:).
Best regards,
Robert
 
M

macropod

Hi Robert,

For future reference, a possible workaround is to insert your equations as
EQ fields, instead of via the Equation Editor. You can get much the same
result, in terms of appearance but, instead of a bitmap, you get a real
text-based field that will sit in-line with other text by default. EQ fields
will adopt whatever style/formatting you apply to the rest of the paragraph
(unless you add a Charformat switch). Plus, you can edit them!

Cheers
 
R

Robert L

Hi macropod,

Unfortunately, the Word documents that I run my macro on are created by
people in another company. So I cannot influence this. But the equations
appear in the docs rarely, so it should not be a big problem to retype texts
from equation(s) to another document manually.

Thank you very much once more.
Regards,
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top