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 ?