Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through equations--open and close 1

Status
Not open for further replies.

dcompto

Technical User
Jul 5, 2001
751
US
I have inserted text and equations (Equation Editor 3.0)into a Word 2003 document. The inserted equations were done in a larger font so I need to loop through the equations, simply to open and close each one, in order to change the font size. I tried to record a maco and then add SendKey commands to replicate doing it manually, but to no avail. Can someone tell me what is wrong with my code?

Code:
Sub UpdateEqns()
Selection.GoTo What:=wdGoToEquation, Which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    SendKeys "%eoo"
    SendKeys "%fx"
End Sub
 
Hi dcompto,

I doubt that simply opening and closing the equation object is enough to change its size. To do that, I think you need something along the lines of:
Code:
Sub UpdateEqns()
Dim a As Single
Dim b As Single
Dim oShp As InlineShape
Dim Result As Integer
With ActiveDocument
    a = 18 'old font size
    b = 12 ' new font size
    If .InlineShapes.Count > 0 Then
        For Each oShp In .InlineShapes
            If oShp.OLEFormat.ClassType = "Equation.3" Then
                With oShp
                    .Height = .Height * b / a
                    .Width = .Width * b / a
                End With
            End If
        Next oShp
        MsgBox "Finished."
    End If
End With
End Sub
Cheers

[MS MVP - Word]
 
macroprod,

When doing it manually, I always open the first equation and change the size as desired. Then I just open and close all the other equations and they resize. Of course, handling the size in the macro is even better.

Thank you for your help. You went above the call (!!) I always first try to figure things out on my own because that's the best way I learn. I fervently wish it was easier for me to understand VBA !!!!!!

Thanks a million.
 
Well, after all was said and done, I couldn't get the macro to work. Still looking for code that will open and close Equation Editor, although I very much appreciate macropod's effort.
 
Hi dcompto,
I couldn't get the macro to work
So what didn't work? As coded, the macro should reduce the equation object's size by one-third. If you input your own former & current font sizes for a & b, respectively, the equation object should re-scale to suit.

Cheers

[MS MVP - Word]
 
It immediately pops up the message box, "Finished", but the cursor hasn't moved from its starting position, and none of the equations were changed from 18 pt to 10 pt (my own former & current font sizes which I specified in the macro).
 
Hi dcompto,

The only reason I can see for that code not working is if the objects aren't actually Equation Editor 3.0 objects. It certainly works for me with Word 2000.

Just in case the Equation Editor objects are another version, you could try changing:
Code:
If oShp.OLEFormat.ClassType = "Equation.3" Then
to
Code:
If InStr(oShp.OLEFormat.ClassType, "Equation") > 0 Then
You could also delete 'Dim Result As Integer' - it's left over from earlier testing.

Cheers


[MS MVP - Word]
 
Thanks, macropod, I'll try that tomorrow. I actually just finished figuring out everything I had originally intended (see below), except for the proper way to loop and error handle. I prefer the concept of your macro so I'll see if the proposed change will fix it.

Code:
Sub UpdateEqns()
'
' UpdateEqns Macro
' Update Equations
'
'Do
'    On Error GoTo Bye
    Selection.GoTo What:=wdGoToEquation, Which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
        SendKeys "%e", True
        SendKeys "o", True
        SendKeys "o", True
        SendKeys "%f", True
        SendKeys "x", True
    Selection.MoveRight Unit:=wdCharacter, Count:=1
'Loop
'
'Bye:
            
End Sub
 
Hi dcompto,

If you want to go down that path, you could delete the code relateing to the a & b variables and replace:
Code:
With oShp
    .Height = .Height * b / a
    .Width = .Width * b / a
End With
with
Code:
oShp.Select
SendKeys "%eoo"
SendKeys "%fx"
Cheers

[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top