[Solved] Old macro for inserting Equation into Writer

Creating a macro - Writing a Script - Using the API

[Solved] Old macro for inserting Equation into Writer

Postby rusty82 » Thu Nov 08, 2012 11:58 am

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   Expand viewCollapse view
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   Expand viewCollapse view
  oObj.EmbeddedObject.formula = sFormula


I searched the forum and found some examples:
viewtopic.php?f=30&t=30933
viewtopic.php?f=45&t=45282
viewtopic.php?f=45&t=55285

so I modified my old macro to this:
Code: Select all   Expand viewCollapse view
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?
Last edited by rusty82 on Wed Nov 14, 2012 11:06 am, edited 2 times in total.
OpenOffice 3.4 on Windows 7
rusty82
 
Posts: 2
Joined: Thu Nov 08, 2012 11:47 am
Location: Slovakia

Re: Old macro for inserting Equation into Writer

Postby hanya » Thu Nov 08, 2012 2:52 pm

This works for me without to set size of the formula embedded on 3.4.1.
Code: Select all   Expand viewCollapse view
oObj.EmbeddedObject.Component.formula = sFormula
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Old macro for inserting Equation into Writer

Postby Hagar Delest » Thu Nov 08, 2012 3:18 pm

Why don't you use some AutoText (like fn + F3)?
AOO 4.1.6 on Xubuntu 19.04 and 4.1.5 on Windows 7 (with winPenPack port).
User avatar
Hagar Delest
Moderator
 
Posts: 28561
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Old macro for inserting Equation into Writer

Postby FJCC » Thu Nov 08, 2012 5:34 pm

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.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7315
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Old macro for inserting Equation into Writer

Postby FJCC » Fri Nov 09, 2012 7:36 am

If I run this code
Code: Select all   Expand viewCollapse view
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   Expand viewCollapse view
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
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7315
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Old macro for inserting Equation into Writer

Postby B Marcelly » Fri Nov 09, 2012 1:03 pm

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.
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area

Re: Old macro for inserting Equation into Writer

Postby rusty82 » Sat Nov 10, 2012 12:56 pm

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.
OpenOffice 3.4 on Windows 7
rusty82
 
Posts: 2
Joined: Thu Nov 08, 2012 11:47 am
Location: Slovakia

Re: Old macro for inserting Equation into Writer

Postby B Marcelly » Sat Nov 10, 2012 7:07 pm

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   Expand viewCollapse view
' 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
Bernard

OpenOffice.org 1.1.5 / Apache OpenOffice 4.1.1 / LibreOffice 5.0.5
MS-Windows 7 Home SP1
B Marcelly
Volunteer
 
Posts: 1160
Joined: Mon Oct 08, 2007 1:26 am
Location: France, Paris area


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests