Should be simple VBA

Associate
Joined
27 Dec 2004
Posts
76
Hey,

I am wanting to take lines of code that I have pasted from mathtype and concatenate each of the lines in word using VBA.

This is a possible example of what I might have to start with.

<math>
<semantics>
<mi>a</mi>
<annotation encoding='MathType-MTEF'>
</annotation>
</semantics>
</math>

I want it to change into
<math><semantics><mi>a</mi><annotation encoding='MathType-MTEF'></annotation></semantics></math>

and if possible remove the <math>, <semantics> and
</math>, </semantics> tags.


I don't know much about VBA but I thought that something using the trim function may be useful when applied to each line of code. Then you could stick all of the lines together.

Does anyone have any ideas? Also is it possible to make it available by a button in word, that then creates an input prompt?

It would be excellent if you could help me out :)
 
I am working with javascript and MathMl (the code written above which is a reasonably new code for displaying maths on a webpage).

Math type is like equation editor but with more features and you can copy the symbols and then paste their mathml code into word say.

I want to take this code that I will have already got by this point, which will look like my last post except the lines of code don't start at the beginning of the line.

<math>
....<semantics>
.........<mi>a</mi>
..............<annotation encoding='MathType-MTEF'>
.................</annotation>
</semantics>
</math>

Maybe like that (where the dots are spaces). I want to remove any unnecessary spaces between the code so it is put side by side like

<math><semantics><mi>a</mi><annotation encoding='MathType-MTEF'></annotation></semantics></math>

But also remove <math><semantics> and </math></semantics> from the ends

I would like this to be done in vba so that I can apply a macro to any code I paste into word from mathtype (~equation editor). This will save a lot of time, deleting spaces and hammering the backspace button!
 
Hey I have written some code, but the "Do" section isn't working, any ideas?? By the way I don't program in vba so you can probably tidy it up a lot!

Once the loop is working then it will be finished and I can relax a bit. I need it to be done asap so please if you can, help me out :)
**************************************************************************************************


Sub Tidyup()

Dim nLines As Variant
Dim i As Variant

Dim AD As Document
Dim DP As Object

Set AD = ActiveDocument
Set DP = AD.BuiltInDocumentProperties


Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=DP("Number Of Lines")

i = 0

While i < DP("Number Of Lines")
i = i + 1
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Wend

Selection.HomeKey Unit:=wdLine
Selection.MoveUp Unit:=wdLine, Count:=DP("Number Of Lines")


i = 0

Do While i < DP("Number Of Lines")

i = i + 1

Selection.EndKey Unit:=wdLine, Extend:=wdExtend

If Selection = "math>" Then
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
End If

If Selection = "<semantics>" Then
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
End If

If Selection = "</semantics>" Then
Selection.TypeBackspace
Selection.MoveDown Unit:=wdLine, Count:=1
Else
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.TypeBackspace
End If

Exit Do
Loop

End Sub
 
Back
Top Bottom