Page 1 of 1

[Solved] Old macro for inserting Equation into Writer

Posted: Thu Nov 08, 2012 11:58 am
by rusty82
Hello.
I have some questions about a macro for inserting equation - Math object into a Writer document.

Until recently I was using OOo 3.2.1 and modified macro from Andrew Pitonyak:

Code: Select all

Sub InsertEquationAtViewCursor(oDoc, sFormula$)
  Dim oVC
  Dim oObj

  oVC = oDoc.CurrentController.getViewCursor()
  oVC.gotoRange(oVC.getEnd(), False)
  oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
  oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
  oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
  oVC.Text.insertTextContent(oVC, oObj, False)
  oObj.EmbeddedObject.formula = sFormula
End Sub
But now I installed the Apache OOo 3.4.1 and the macro doesn't work anymore.
It breaks with error message: 'BASIC runtime error. Property or method not found: formula.' at the line

Code: Select all

  oObj.EmbeddedObject.formula = sFormula
I searched the forum and found some examples:
http://forum.openoffice.org/en/forum/vi ... 30&t=30933
http://forum.openoffice.org/en/forum/vi ... 45&t=45282
http://forum.openoffice.org/en/forum/vi ... 45&t=55285

so I modified my old macro to this:

Code: Select all

Sub InsertEquationAtViewCursor(oDoc, sFormula$)
  Dim oVC
  Dim oObj

  oVC = oDoc.CurrentController.getViewCursor()
  oVC.gotoRange(oVC.getEnd(), False)
  oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
  oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
  oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
  oVC.Text.insertTextContent(oVC, oObj, False)
   TempSize = oObj.Size
   TempSize.Width = 2000
   TempSize.Height = 2000
   oObj.Size = TempSize
  oObj.Model.formula = sFormula
End Sub
It works now. But because I don't understand why and how, I have some questions. I would be happy if someone can answer or post some links where I find the answers I'm looking for:

1. Why does the old macro not work anymore.
2. Why do I have to specify the size of the math object - it's tiny not-readable object if I don't do?
3. Why can't I move the new math object in the document as before - in the old version?
4. Is the new modified macro OK now, or should change something more?

Re: Old macro for inserting Equation into Writer

Posted: Thu Nov 08, 2012 2:52 pm
by hanya
This works for me without to set size of the formula embedded on 3.4.1.

Code: Select all

oObj.EmbeddedObject.Component.formula = sFormula

Re: Old macro for inserting Equation into Writer

Posted: Thu Nov 08, 2012 3:18 pm
by Hagar Delest
Why don't you use some AutoText (like fn + F3)?

Re: Old macro for inserting Equation into Writer

Posted: Thu Nov 08, 2012 5:34 pm
by FJCC
I do seem to see the difference in behavior between 3.3 and 3.4.1. I don't have time to look at it in detail at the moment. I'll try to do that tonight.

Re: Old macro for inserting Equation into Writer

Posted: Fri Nov 09, 2012 7:36 am
by FJCC
If I run this code

Code: Select all

Sub InsertEquationAtViewCursor'(oDoc, sFormula$)
  Dim oVC
  Dim oObj
  oDoc = ThisComponent
  sFormula = "x over y"
  oVC = oDoc.CurrentController.getViewCursor()
  oVC.gotoRange(oVC.getEnd(), False)
  oObj = oDoc.CreateInstance("com.sun.star.text.TextEmbeddedObject")
  oObj.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997"
  oObj.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
  oVC.Text.insertTextContent(oVC, oObj, False)
  XRay oObj.EmeddedObject
in 3.3 and in 3.4.1 I get different XRay data on oObj.EmbeddedObject. In 3.3 the object is listed as an com.sun.star.comp.math.FormulaDocument with these Services:

Code: Select all

com.sun.star.document.OfficeDocument 
com.sun.star.formula.FormulaProperties 
In 3.4.1 the object is listed as No Name and no Services are listed. The 3.3 object does have a Formula property and the 3.4.1 object does not. I certainly can't account for the difference or the fact that Hanya doesn't have a problem in 3.4.1.
I do not have to set the size of the of oObj when using 3.4.1 to get a reasonably sized formula after setting oObj.Model.Formula. It is probably safest to set the size in any case, especially if you will not be the only user of the macro

Re: Old macro for inserting Equation into Writer

Posted: Fri Nov 09, 2012 1:03 pm
by B Marcelly
My understanding is :
Up to OOo 3.3.1
oObj.EmbeddedObject integrates the component of the equation. The property Formula is in fact one of the properties of the component.
From AOO 3.4
oObj.EmbeddedObject only provides the property Component, which gives access to the component of the equation.[/list]

To have a code compatible with both variants : try the new code within an error handler. If the code throws the errror, execute the old code.

I verified the new code in AOO 3.4.1 and in LibO 3.5.5.3.

Re: Old macro for inserting Equation into Writer

Posted: Sat Nov 10, 2012 12:56 pm
by rusty82
Thak you for the replies.

hanya: yes this works in 3.4.1

Hagar Delest: I use this macro for counting a creating 3 equations from 5 variables I put in a form and I think an AutoText can't do that.

FJCC:
Yes, you are right.
But when I use the new macro in OOo 3.2.1 with defined size I get the the math object with that size - in this case 2000x2000 - deformed equation object.
In AOOo 3.4.1 it looks like I can use any size and I get the math object in its normal size.

So when I put it together:
'oObj.EmbeddedObject.Formula = sFormula' OK in OOo 3.2.1; ERROR in 3.4.1
'oObj.EmbeddedObject.Component.Formula = sFormula' ERROR in OOo 3.2.1; OK in 3.4.1
'oObj.Model.formula = sFormula' OK in 3.2.1; OK in 3.4.1; BUT in both versions is a issue with the object size.

Is there a way to set the size to that it should be? - for use in older OOo versions?

To my question nr.3. - I realised I have to change the anchor type of the object to move it as I want.

Re: Old macro for inserting Equation into Writer

Posted: Sat Nov 10, 2012 7:07 pm
by B Marcelly
Each term in an equation (digit, sign, separator, exponent...) is sized relative to BaseFontHeight.

To decide of the size of the surrounding rectangle you need to know the overall complexity of the equation, for each equation.

A better and simpler solution is to specify the BaseFontHeight you want.
Use the same value for all your equations, to have a uniform look.

Code: Select all

' example for AOO 3.4.1.
oObj.EmbeddedObject.Component.BaseFontHeight = 10 ' default : 12

' example for OOo 3.3.1 and previous.
oObj.EmbeddedObject.BaseFontHeight = 10 ' default : 12