Microsoft Office Forums VBA and Omath object

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2016, 07:10 AM
jpl jpl is offline VBA and Omath object Windows 7 64bit VBA and Omath object Office 2010 32bit
Novice
VBA and Omath object
 
Join Date: Jan 2016
Location: France
Posts: 13
jpl is on a distinguished road
Default VBA and Omath object

Bonjour à vous.
I try to write equations by means of macro.
To insert structures into my equations, I use the OMathFunctions. Add method.
My macros work most of the time, except when I use the OmathFunctionText function with the constant wdOMathFunctionText.

Macro QuiMarche makes for what I expect from it:


Code:
Sub QuiMarche()
      Dim monEq As OMath, maFonction As OMathFunction, maPlage As Range
      ActiveDocument.Range.Delete
      Set monEq = ActiveDocument.OMaths.Add(Selection.Range).OMaths(1)
      
      'on insère le radical
      Set maPlage = monEq.Range
      Set maFonction = monEq.Functions.Add(Range:=maPlage, Type:=wdOMathFunctionRad)
      maFonction.Rad.Deg.Range.Text = "3": maFonction.Rad.e.Range.Text = "8"
      
      'on insère la suite
      Set maPlage = maFonction.Range
      maPlage.Collapse direction:=wdCollapseEnd
      maPlage.Text = " est égal à 2."
      Set maFonction = monEq.Functions.Add(Range:=maPlage, Type:=wdOMathFunctionNormalText)
  End Sub
If I modify the last line, the macro emits an error message which I do not understand:

Erreur d'exécution '6219' : Impossible d'appliquer la fonction texte à une sélection.

Code:
Sub QuiNeMarchePas()
      Dim monEq As OMath, maFonction As OMathFunction, maPlage As Range
      ActiveDocument.Range.Delete
      Set monEq = ActiveDocument.OMaths.Add(Selection.Range).OMaths(1)
      
      'on insère le radical
      Set maPlage = monEq.Range
      Set maFonction = monEq.Functions.Add(Range:=maPlage, Type:=wdOMathFunctionRad)
      maFonction.Rad.Deg.Range.Text = "3": maFonction.Rad.e.Range.Text = "8"
      
      'on insère la suite
      Set maPlage = maFonction.Range
      maPlage.Collapse direction:=wdCollapseEnd
      maPlage.Text = "=2"
      Set maFonction = monEq.Functions.Add(Range:=maPlage, Type:=wdOMathFunctionText)
  End Sub
If I neutralize the last line, the macro works.

I have never managed to use the OmathFunctionText function without that error message. In what circumstances is it possible to use this function, and what is its utility?

In French
Je cherche à écrire des équations à l'aide de macros.
Pour insérer des structures dans mes équations, j'utilise la méthode OMathFunctions.Add.
Mes macros fonctionnent la plupart du temps, sauf quand j'utilise la fonction OmathFunctionText avec la constante wdOMathFunctionText.
La macro QuiMarche fait ce que j'en attends :

Si je modifie la dernière ligne, la macro émet un message d'erreur que je ne comprends pas :
Erreur d'exécution '6219' : Impossible d'appliquer la fonction texte à une sélection.
....
Si je neutralise la dernière ligne, la macro fonctionne.

Je n'ai jamais réussi à utiliser la fonction OmathFunctionText sans le même message d'erreur. Dans quelles circonstances est il possible d'utiliser cette fonction, et quelle est son utilité ?
Reply With Quote
  #2  
Old 10-18-2019, 05:55 AM
George Keeling George Keeling is offline VBA and Omath object Windows 10 VBA and Omath object Office 2019
Novice
 
Join Date: Oct 2019
Posts: 4
George Keeling is on a distinguished road
Default Bonjour, I have this problem too! Did you find an solution?

I have this problem too! Did you find an solution?
I am using this code which is very similar to yours:
Code:
Sub ExampleWriteExpression()
    'insertion point should be in equation. Metric expression written (wdOMathFunctionScrSub) at end. Then
    Dim LetterCode As Integer, Letter0 As Integer
    Dim Equation As OMath
    Dim MathTerm As OMathFunction
    Dim MyRange As Range
    
    If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
    Set Equation = Selection.OMaths(1)
    Set MyRange = Equation.Range
    MyRange.Collapse (wdCollapseEnd)
    Set MathTerm = Equation.Functions.Add(MyRange, wdOMathFunctionScrSub)
    MathTerm.ScrSub.E.Range = "g"
    MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
    Set MathTerm = Equation.Functions.Add(MyRange, wdOMathFunctionNormalText) 'does not add anything to Equation.Functions
    Set MathTerm = Equation.Functions.Add(MyRange, wdOMathFunctionText) 'error 6219
                                                                        'The text function cannot be applied to a selection
    'We do want to add a wdOMathFunctionText, because that is the kind of function we had 
    'when the original equation just contains X or X= as can be seen in the debugger
End Sub
Reply With Quote
  #3  
Old 10-19-2019, 01:41 AM
jpl jpl is offline VBA and Omath object Windows 7 64bit VBA and Omath object Office 2010 32bit
Novice
VBA and Omath object
 
Join Date: Jan 2016
Location: France
Posts: 13
jpl is on a distinguished road
Default

Votre message ne me rajeunit pas !
Mes conclusions sont les suivantes :
Dans une équation, on peut insérer 21 structures, qui correspondent à l'énumération WdOMathFunctionType.
Par défaut, on insère du texte au format math (qui correspond à la constante wdOMathFunctionText).
On n'a donc pas besoin d'utiliser OMath.Functions.Add pour insérer du texte au format math.
La macro suivante montre la différence entre les deux formes de texte dans une équation.
Code:
Sub TexteMathTexteNormal()
   Dim monEq As OMath, maFonction As OMathFunction, maPlage As Range
   ActiveDocument.Range.Delete
   Set monEq = ActiveDocument.OMaths.Add(Selection.Range).OMaths(1)
   
   'on insere du texte au format math : on n'a pas besoin de la méthode Add
   Set maPlage = monEq.Range
   maPlage.Text = "type=wdOMathFunctionText"
   
   'on insere du texte au format normal : on utilise la méthode Add
   maPlage.Collapse (wdCollapseEnd)
   maPlage.Text = "type=wdOMathFunctionNormalText"
   Set maFonction = monEq.Functions.Add(Range:=maPlage, Type:=wdOMathFunctionNormalText)
  End Sub
Si on met un point d'arrêt à la dernière instruction, on voit bien que le texte "type=wdOMathFunctionNormalText" est inséré au format math, avant d'être converti en texte normal.
Reply With Quote
  #4  
Old 10-19-2019, 06:35 AM
George Keeling George Keeling is offline VBA and Omath object Windows 10 VBA and Omath object Office 2019
Novice
 
Join Date: Oct 2019
Posts: 4
George Keeling is on a distinguished road
Default Another solution

Merci jpl. Peut-être que cela vous rajeunira!
Code:
Sub ExampleWriteExpression()
    'insertion point should be in equation. Metric + inverse metric inserted
    Dim Equation As OMath
    Dim MathTerm As OMathFunction

    If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
    Set Equation = Selection.OMaths(1)
    Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSub)
    MathTerm.ScrSub.E.Range = "g"
    MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
 
    Selection.TypeText ("+")
 
'    Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionText) still gets error

    Set MathTerm = Equation.Functions.Add(Selection.Range, wdOMathFunctionScrSup)
    MathTerm.ScrSup.E.Range = "g"
    MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)
End Sub
I believe that the Add method is defective when type=wdOMathFunctionText is used.
The above is another solution. But it does not solve the problem of aligning the selection in the equation. I have documented the problem more fully here and presented some code for expanding the Christoffel symbol.
Reply With Quote
  #5  
Old 10-19-2019, 12:02 PM
jpl jpl is offline VBA and Omath object Windows 7 64bit VBA and Omath object Office 2010 32bit
Novice
VBA and Omath object
 
Join Date: Jan 2016
Location: France
Posts: 13
jpl is on a distinguished road
Default

Dans la macro ExampleWriteExpression, on peut éviter l'utilisation de l'instruction Selection.TypeText en utilisant un objet Range (que l'on modifie après chaque utilisation de la méthode Add) à la place de l'objet Selection.
La macro modifiée insère la chaîne Metric + inverse metric - metric à la position courante du curseur dans l'équation.


Les 4 lignes de codes de la modification peuvent être remplacées par une procédure prenant en paramètre un objet Range, et une chaîne représentant le signe + ou - à insérer.


Je ne sais pas si ceci est transposable à votre code.

Code:
Sub ExampleWriteExpression()
    'insertion point should be in equation. Metric + inverse metric - metric inserted
    Dim Equation As OMath
    Dim MathTerm As OMathFunction
    Dim plage As Range
   
    '    If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
    Set Equation = Selection.OMaths(1)
    
    'Initialisation de l'objet plage  à la position du curseur dans l'équation
    Set plage = Selection.Range
   
    Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSub)
    MathTerm.ScrSub.E.Range.Text = "g"
    MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
   
    'remplace "Selection.TypeText"
    Set plage = MathTerm.Range
    plage.Collapse (wdCollapseEnd)
    plage.Text = "+"
    plage.Collapse (wdCollapseEnd)


    Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSup)
    MathTerm.ScrSup.E.Range = "g"
    MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)
    
    'remplace "Selection.TypeText"
    Set plage = MathTerm.Range
    plage.Collapse (wdCollapseEnd)
    plage.Text = "-"
    plage.Collapse (wdCollapseEnd)


    Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSub)
    MathTerm.ScrSub.E.Range.Text = "g"
    MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
  End Sub
Reply With Quote
  #6  
Old 10-19-2019, 02:13 PM
jpl jpl is offline VBA and Omath object Windows 7 64bit VBA and Omath object Office 2010 32bit
Novice
VBA and Omath object
 
Join Date: Jan 2016
Location: France
Posts: 13
jpl is on a distinguished road
Default

Nouvelle version
Code:
Sub ExampleWriteExpression()
  'insertion point should be in equation. Metric + inverse metric - metric inserted
  Dim Equation As OMath
  Dim MathTerm As OMathFunction
  Dim plage As Range

  '    If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
  Set Equation = Selection.OMaths(1)
  Set plage = Selection.Range

  Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSub)
  MathTerm.ScrSub.E.Range.Text = "g"
  MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)

  'remplace "Selection.TypeText"
  Set plage = Insere(MathTerm.Range, "+")

  Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSup)
  MathTerm.ScrSup.E.Range = "g"
  MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)

  'remplace "Selection.TypeText"
  Set plage = Insere(MathTerm.Range, "-")

  Set MathTerm = Equation.Functions.Add(plage, wdOMathFunctionScrSub)
  MathTerm.ScrSub.E.Range.Text = "g"
  MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
End Sub

Function Insere(plage As Range, signe As String) As Range
  plage.Collapse (wdCollapseEnd)
  plage.Text = signe
  plage.Collapse (wdCollapseEnd)
  Set Insere = plage
End Function
Reply With Quote
  #7  
Old 10-21-2019, 06:44 AM
George Keeling George Keeling is offline VBA and Omath object Windows 10 VBA and Omath object Office 2019
Novice
 
Join Date: Oct 2019
Posts: 4
George Keeling is on a distinguished road
Default

Thank you, Merci and Danke, jpl! It is certainly a great improvement to avoid using Selection.TypeText but if you remove the line
Code:
Set plage = Insere(MathTerm.Range, "+")
then the inverse metric appears before the metric! Grrr. So I have made what I hope is a further improvement so that functions and text can be added in the expected order:
Code:
Function InsertFunction(Functions As OMathFunctions, InsertionPoint As Range, FuncType As WdOMathFunctionType) As OMathFunction
    'Add a function in Functions at InsertionPoint and move InsertionPoint to after the function, ready for next
    Dim NewFunction As OMathFunction
    Set NewFunction = Functions.Add(InsertionPoint, FuncType)
    Set InsertionPoint = NewFunction.Range
    InsertionPoint.Collapse (wdCollapseEnd)
    Set InsertFunction = NewFunction
End Function

Sub InsertText(InsertionPoint As Range, MyText As String)
    'Inserts MyText at InsertionPoint and returns moves InsertionPoint to after that text
    'so this is very similar to
    'Equation.Functions.Add(InsertionPoint, wdOMathFunctionNormalText) .... which does not work!!
    InsertionPoint.Text = MyText
    InsertionPoint.Collapse (wdCollapseEnd)
End Sub

Sub ExampleWriteExpression()
    'insertion point should be in equation. Metric + inverse metric inserted
    Dim Equation As OMath
    Dim MathTerm As OMathFunction
    Dim InsertionPoint As Range

    'If Selection.OMaths.Count <> 1 Then ExpanderFatalError ("Cursor must be in an equation.")
    Set Equation = Selection.OMaths(1)
    Set InsertionPoint = Selection.Range
    
    Set MathTerm = InsertFunction(Equation.Functions, InsertionPoint, wdOMathFunctionScrSub)
    MathTerm.ScrSub.E.Range = "g"
    MathTerm.ScrSub.Sub.Range = ChrW(&H3BC) & ChrW(&H3BD)
    
    Call InsertText(InsertionPoint, "+")

    Set MathTerm = InsertFunction(Equation.Functions, InsertionPoint, wdOMathFunctionScrSup)
    MathTerm.ScrSup.E.Range = "g"
    MathTerm.ScrSup.Sup.Range = ChrW(&H3BC) & ChrW(&H3BD)
End Sub
This works perfectly for expanding horrid symbols and tensors in General Relativity which is what I want to do.
*********************
There is only one catch! If you insert brackets (wdOMathFunctionDelim) then you need to back up one character to get inside the brackets. Something like:
Code:
    InsertionPoint.Start = InsertionPoint.Start - 1       'get into the bracket
    InsertionPoint.Collapse (wdCollapseStart)
I will also update the page on my website that I referenced above. It inserts brackets.
Thanks again! A great example of international collaboration.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables in Equation using OMath object pooley343 Word VBA 0 01-09-2015 10:26 AM
VBA and Omath object Click an object on a slide, show another object on the same slide. meppley7 PowerPoint 2 04-28-2014 01:44 PM
tool tip for object slain Outlook 0 05-19-2011 06:06 AM
Problem: object library invalid or contains references to object definitions aligahk06 Office 0 08-19-2010 12:29 PM
Could not load this object as this object is not present in your computer k.gaurav Office 0 08-17-2009 09:57 PM


All times are GMT -7. The time now is 09:59 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft