How to operate with EQ-field long result?

V

viter.alex

I insert EQ-field into the document using VBA. Sometimes the result
doesn't fit to page and it causes an error. For Example this field
code:
EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s\up8(н)\s
\do8(47)+\x\bo(S)\s\up8(н)\s\do8(37) = 24.86 -j16.66+(27.82 -
j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;

If EQ-field result can take only one line? If there is any chance to
put the result into the several lines?
 
D

Doug Robbins - Word MVP

Show us the vba code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
I insert EQ-field into the document using VBA. Sometimes the result
doesn't fit to page and it causes an error. For Example this field
code:
EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s\up8(н)\s
\do8(47)+\x\bo(S)\s\up8(н)\s\do8(37) = 24.86 -j16.66+(27.82 -
j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;

If EQ-field result can take only one line? If there is any chance to
put the result into the several lines?
 
M

macropod

Hi viter.alex,

It seems to me you need two EQ fields for that expression, plus some plain text:
{EQ \x\bo(S)\s\up8(к)\s\do8(17)}
=
EQ \x\bo(S)\s\do8(7) +\x\bo(S)\s\up8(н)\s \do8(47)+\x\bo(S)\s\up8(н)\s\do8(37)}
= 24.86 - j16.66 + (27.82 - j6.07)+(25.65 - j3.02)} = 78.33 - j25.75 МВÐ

--
Cheers
macropod
[Microsoft MVP - Word]


I insert EQ-field into the document using VBA. Sometimes the result
doesn't fit to page and it causes an error. For Example this field
code:
EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s\up8(н)\s
\do8(47)+\x\bo(S)\s\up8(н)\s\do8(37) = 24.86 -j16.66+(27.82 -
j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;

If EQ-field result can take only one line? If there is any chance to
put the result into the several lines?
 
V

viter.alex

Here is a code

Set oRng = ActiveDocument.Paragraphs
(ActiveDocument.Paragraphs.Count).Range

sEQ = "EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s
\up8(н)\s \do8(47)+\x\bo(S)\s\up8(н)\s\d o8(37) = 24.86 -j16.66+(27.82
- j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;"

With oRng
Set oFld = .Fields.Add(oRng, , sEQ, False)
.Fields.Update
.InsertParagraphAfter
End With
End Sub
 
D

Doug Robbins - Word MVP

Taking the lead from fellow MVP, Macropod, try:

Dim oRng As Range
Dim sEQ1 As String, sEQ2 As String

Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range

sEQ1 = "EQ \x\bo(S)\s\up8(k)\s\do8(17)"
With oRng
Set oFld = .Fields.Add(oRng, , sEQ1, False)
.Fields.Update
.InsertAfter " = "
End With
Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
oRng.Collapse wdCollapseEnd
sEQ2 = "EQ
\x\bo(S)\s\do8(7)+\x\bo(S)\s\up8(h)\s\do8(47)+\x\bo(S)\s\up8(h)\s\do8(37)"
With oRng
Set oFld = .Fields.Add(oRng, , sEQ2, False)
.Fields.Update
End With
Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
oRng.Collapse wdCollapseEnd
oRng.InsertAfter " = 24.86 -j16.66+(27.82 - j6.07)+(25.65 -j3.02) =
78.33 -j25.75 MBA;" & vbCr


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Here is a code

Set oRng = ActiveDocument.Paragraphs
(ActiveDocument.Paragraphs.Count).Range

sEQ = "EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s
\up8(н)\s \do8(47)+\x\bo(S)\s\up8(н)\s\d o8(37) = 24.86 -j16.66+(27.82
- j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;"

With oRng
Set oFld = .Fields.Add(oRng, , sEQ, False)
.Fields.Update
.InsertParagraphAfter
End With
End Sub
 
D

Doug Robbins - Word MVP

Sorry if this is a repeat; I received a connection to the server was
terminated unexpectedly on the first try.

Taking the lead from fellow MVP, Macropod, try:

Dim oRng As Range
Dim sEQ1 As String, sEQ2 As String

Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range

sEQ1 = "EQ \x\bo(S)\s\up8(k)\s\do8(17)"
With oRng
Set oFld = .Fields.Add(oRng, , sEQ1, False)
.Fields.Update
.InsertAfter " = "
End With
Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
oRng.Collapse wdCollapseEnd
sEQ2 = "EQ
\x\bo(S)\s\do8(7)+\x\bo(S)\s\up8(h)\s\do8(47)+\x\bo(S)\s\up8(h)\s\do8(37)"
With oRng
Set oFld = .Fields.Add(oRng, , sEQ2, False)
.Fields.Update
End With
Set oRng = ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range
oRng.Collapse wdCollapseEnd
oRng.InsertAfter " = 24.86 -j16.66+(27.82 - j6.07)+(25.65 -j3.02) =
78.33 -j25.75 MBA;" & vbCr

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
Here is a code

Set oRng = ActiveDocument.Paragraphs
(ActiveDocument.Paragraphs.Count).Range

sEQ = "EQ \x\bo(S)\s\up8(к)\s\do8(17) = \x\bo(S)\s\do8(7) +\x\bo(S)\s
\up8(н)\s \do8(47)+\x\bo(S)\s\up8(н)\s\d o8(37) = 24.86 -j16.66+(27.82
- j6.07)+(25.65 -j3.02) = 78.33 -j25.75 МВÐ;"

With oRng
Set oFld = .Fields.Add(oRng, , sEQ, False)
.Fields.Update
.InsertParagraphAfter
End With
End Sub
 
V

viter.alex

Thanks, but I want to know where exactly I should split the code
string. I tried to find out if there is an error in the result of
equation, but I couldn't. I have a lot of such strings and I need to
know if code string will make an error and then split it
 
D

Doug Robbins - Word MVP

Probably the EQ field is mis-named. It does not really allow you to insert
a complete equation in the sense that an equation has a left hand side and a
right hand side with the sides separated by an = sign.

What it does allow you to do is construct the expression that forms the left
or the right hand side of an equation. In the code that I gave you, sEQ1
creates the expression for the left hand side. The code then inserts an =
sign and then sEQ2 creates the expression for the right hand side which is
inserted after the equal sign.

The code then inserts as ordinary text the (assumed to be) evaluated result
of each of those expressions.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
J

Jack Donald

let me introduce myself - i've been digging this problem together
with topic starter for a while
as you see i use EQ field to form mathematical expressions in Word.
You've seen the problem - sometimes it's just too long to fit the page
- and thus i got an error on the screen.
What it does allow you to do is construct the expression that forms the left
or the right hand side of an equation. In the code that I gave you, sEQ1
creates the expression for the left hand side. The code then inserts an =
sign and then sEQ2 creates the expression for the right hand side which is
inserted after the equal sign.

I can't use plain text for part of the expression for number of
reasons
- right hand side can contain complex formatting. fractions,
underlining etc
example posted is a easiest one.
- there're certain rules i have to obey when carry forward a line: i
can only split the line after "-","+","*" characters and i have to
duplicate these symbols at a new line. it's impossible with plain text

obvious solution for this problem is to split the code line into
several EQ fields. and we've got code for this task
Code:
Function mySplit(ByVal S As String, ByVal MaxLen As Long)
Dim i As Long, a() As String, p As Long
ReDim a(0)
a(0) = S
Do
If Len(a(UBound(a))) > MaxLen Then
p = 0
znak1 = ""
For i = 1 To Len(a(UBound(a)))
znak = Mid(a(UBound(a)), i, 1)
Select Case znak
Case "-", "+", "=" 'characters line can be split at
p = i
znak1 = znak
'Debug.Print znak1
Case Else

If i >= MaxLen Then Exit For
End Select
Next i
If p > 0 Then
ReDim Preserve a((UBound(a) + 1))
a(UBound(a)) = "EQ " & znak1 & VBA.Mid(a(UBound(a) - 1), p + 1)
a(UBound(a) - 1) = VBA.Left(a(UBound(a) - 1), p)
End If
Else
Exit Do
End If
Loop While p <> 0
mySplit = a
End Function

Function eq_text(ByVal text As String, ByVal limit As Integer, ByVal
oRng As Object)
If Not (limit > 0) Then limit = 220

Dim a
a = mySplit(text, limit)

For i = 0 To UBound(a)
eq_text = eq_text_add(a(UBound(a) - i), oRng, False)
Next
With app
.InsertParagraphAfter
End With

End Function

Function eq_text_add(ByVal text As String, ByVal oRng As Object, ByVal
newparagraph As Boolean)

Dim oFld As Object
With oRng
Set oFld = .Fields.Add(oRng, , text, False)
.Fields.Update
If newparagraph Then
.InsertParagraphAfter
End If
End With
End Function

it's called like
text = "EQ =(95350088+1146600+1980000)·(1+0.1) +
(200502400+2285280+3540000)·(1+0.1) + (146171408+1664400+2500000)·
(1+0.1) + (45147332+946080+567000)·(1+0.1) + (45045408+1350000+787500)·
(1+0.1) = 108324356.8 + 226960448 + 165369388.8 + 51326453.2 +
51901198.8"
limit = 80
Call eq_text(text, limit, oDoc.Paragraphs
(oDoc.Paragraphs.Count).Range)
where oDoc - any text file
limit is an approximiate place to split the line

eq_text calls mySplit function that splits expression into several
ones. then eq_text_add executed for each line

The real problem is to guess that 'limit'. There can be a lot of
'code' characters in the EQ field. after field is updated text on the
screen can be _much_ shorter
compare for example
"EQ -\x\bo(S)\s\do8(12)•\x\bo(Z)\s\do8(12) +\x\bo(S)\s\do8(14)•\x\bo
(Z)\s\do8(14) -\x\bo(S)\s\do8(23)•\x\bo(Z)\s\do8(23) -\x\bo(S)\s\do8
(47)•\x\bo(Z)\s\do8(47) +\x\bo(S)\s\do8(35)•\x\bo(Z)\s\do8(35) +\x\bo
(S)\s\do8(57)•\x\bo(Z)\s\do8(57)=0"

"EQ \x\bo(S)\s\up8(н)\s\do8(47) = \x\bo(S)\s\do8(7) -\x\bo(S)\s\up8(н)
\s\do8(57) = 44.8 -j17.1-(26.3 -j21.93) = 18.5 -j4.83 МВÐ"

"EQ ΔQ\s\do8(57)=\f(P\s\up6(2)+Q\s\up6(2);U\s\up8(2))•X\s\do8(57) = \f
(26.3\s\up6(2)+21.93\s\up6(2);110\s\up8(2))•13.59=1.32 Мвар; "

i guess i need approximate analysis how long will be the text on the
screen to find where to split the line... any help with this ?

PS: But maybe i'm doing everything wrong at all ? maybe there're
better solutions for my task ? i usually use Mathtype to make
expressions like that by hand - but i found no way to automate it
under VBA. Any ideas ?
 
P

Peter Jamieson

I haven't been following this too closely, but a couple of thoughts:
a. if the EQ field result is wider than the width available on the
line, the field result appears to be "Error!"
b. otherwise, you should be able to determine the width of the field
result by looking at (say)

Information(wdHorizontalPositionRelativeToTextBoundary)

for the range/selection immediately before and after the field. However,
it might be more useful to check the vertical position of the text
immediately before the field, and the text immediately after, using one
of the vertical positioning Information values.

Armed with that, you could consider iteratively inserting the field,
testing the result for "Error!" and/or checking relocation, shortening
the EQ code until it ends up on the same line as any introductory text.

Peter Jamieson

http://tips.pjmsn.me.uk

Jack said:
let me introduce myself - i've been digging this problem together
with topic starter for a while
as you see i use EQ field to form mathematical expressions in Word.
You've seen the problem - sometimes it's just too long to fit the page
- and thus i got an error on the screen.
What it does allow you to do is construct the expression that forms the left
or the right hand side of an equation. In the code that I gave you, sEQ1
creates the expression for the left hand side. The code then inserts an =
sign and then sEQ2 creates the expression for the right hand side which is
inserted after the equal sign.

I can't use plain text for part of the expression for number of
reasons
- right hand side can contain complex formatting. fractions,
underlining etc
example posted is a easiest one.
- there're certain rules i have to obey when carry forward a line: i
can only split the line after "-","+","*" characters and i have to
duplicate these symbols at a new line. it's impossible with plain text

obvious solution for this problem is to split the code line into
several EQ fields. and we've got code for this task
Code:
Function mySplit(ByVal S As String, ByVal MaxLen As Long)
Dim i As Long, a() As String, p As Long
ReDim a(0)
a(0) = S
Do
If Len(a(UBound(a))) > MaxLen Then
p = 0
znak1 = ""
For i = 1 To Len(a(UBound(a)))
znak = Mid(a(UBound(a)), i, 1)
Select Case znak
Case "-", "+", "=" 'characters line can be split at
p = i
znak1 = znak
'Debug.Print znak1
Case Else

If i >= MaxLen Then Exit For
End Select
Next i
If p > 0 Then
ReDim Preserve a((UBound(a) + 1))
a(UBound(a)) = "EQ " & znak1 & VBA.Mid(a(UBound(a) - 1), p + 1)
a(UBound(a) - 1) = VBA.Left(a(UBound(a) - 1), p)
End If
Else
Exit Do
End If
Loop While p <> 0
mySplit = a
End Function

Function eq_text(ByVal text As String, ByVal limit As Integer, ByVal
oRng As Object)
If Not (limit > 0) Then limit = 220

Dim a
a = mySplit(text, limit)

For i = 0 To UBound(a)
eq_text = eq_text_add(a(UBound(a) - i), oRng, False)
Next
With app
.InsertParagraphAfter
End With

End Function

Function eq_text_add(ByVal text As String, ByVal oRng As Object, ByVal
newparagraph As Boolean)

Dim oFld As Object
With oRng
Set oFld = .Fields.Add(oRng, , text, False)
.Fields.Update
If newparagraph Then
.InsertParagraphAfter
End If
End With
End Function

it's called like
text = "EQ =(95350088+1146600+1980000)·(1+0.1) +
(200502400+2285280+3540000)·(1+0.1) + (146171408+1664400+2500000)·
(1+0.1) + (45147332+946080+567000)·(1+0.1) + (45045408+1350000+787500)·
(1+0.1) = 108324356.8 + 226960448 + 165369388.8 + 51326453.2 +
51901198.8"
limit = 80
Call eq_text(text, limit, oDoc.Paragraphs
(oDoc.Paragraphs.Count).Range)
where oDoc - any text file
limit is an approximiate place to split the line

eq_text calls mySplit function that splits expression into several
ones. then eq_text_add executed for each line

The real problem is to guess that 'limit'. There can be a lot of
'code' characters in the EQ field. after field is updated text on the
screen can be _much_ shorter
compare for example
"EQ -\x\bo(S)\s\do8(12)•\x\bo(Z)\s\do8(12) +\x\bo(S)\s\do8(14)•\x\bo
(Z)\s\do8(14) -\x\bo(S)\s\do8(23)•\x\bo(Z)\s\do8(23) -\x\bo(S)\s\do8
(47)•\x\bo(Z)\s\do8(47) +\x\bo(S)\s\do8(35)•\x\bo(Z)\s\do8(35) +\x\bo
(S)\s\do8(57)•\x\bo(Z)\s\do8(57)=0"

"EQ \x\bo(S)\s\up8(н)\s\do8(47) = \x\bo(S)\s\do8(7) -\x\bo(S)\s\up8(н)
\s\do8(57) = 44.8 -j17.1-(26.3 -j21.93) = 18.5 -j4.83 МВÐ"

"EQ ΔQ\s\do8(57)=\f(P\s\up6(2)+Q\s\up6(2);U\s\up8(2))•X\s\do8(57) = \f
(26.3\s\up6(2)+21.93\s\up6(2);110\s\up8(2))•13.59=1.32 Мвар; "

i guess i need approximate analysis how long will be the text on the
screen to find where to split the line... any help with this ?

PS: But maybe i'm doing everything wrong at all ? maybe there're
better solutions for my task ? i usually use Mathtype to make
expressions like that by hand - but i found no way to automate it
under VBA. Any ideas ?
 
Top