Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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
Advanced Beginner
VBA and Omath object
 
Join Date: Jan 2016
Location: France
Posts: 33
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
  #2  
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



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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:38 AM.


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