I have been copying and pasting tables and equations in MS-Word for far too long. At last I have written some word macros to speed things up, so I can more easily produce correctly aligned equations like

and

I also wrote macros to create left / centre / right justified equations using font size 12 instead of 10 which MS always uses and I find too small.

So I now have a "Quick access toolbar" like this

After the AB icon, they have the following effects

The macros are in WordBasic not VBA which is a bit amateurish, but WordBasic is what the Word macro recorder writes.

If you would like to use these macros click Read more and copy and paste them into the Word Visual Basic editor on the Developer tab.

The version in Read more might be out of date. I am fairly diligent about keeping back up versions here.

'Macros to help with equation editing

'By George Keeling and on my blog at https://spacetime-and-geometry.blogspot.com/

'Please feel free to use, copy and give away.

'Version2 14/10/83 tidied up a bit and got 2 of 3 equations inserting correctly!

'Added vertical alignment

'Version 3 25/10/2018 Introduced EquationFontSize

'TO FIX

'InsertEquationL gets incorrect font!

'Inserting second equation in EquationTable3() does not work

Const EquationFontSize = 12 '**** Change this if you prefer another equation font size

Sub InsertEquationL()

'Insert left justified equation with font EquationFont

'Sadly this cannot be called from inside the table creation Macros, because the font comes out wrong

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="="

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Size = EquationFontSize

'Quite often the attempt to justify the equation fails!

On Error Resume Next

Selection.OMaths(1).ParentOMath.Justification = wdOMathJcLeft

Selection.Font.Size = SaveFontSize

End Sub

Sub CreateTable(Columns As Integer)

'Create table with Columns (Columns = 2 or 3) columns

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _

Columns, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

wdAutoFitFixed

With Selection.Tables(1)

If .Style <> "Table Grid" Then

.Style = "Table Grid"

End If

.ApplyStyleHeadingRows = True

.ApplyStyleLastRow = False

.ApplyStyleFirstColumn = True

.ApplyStyleLastColumn = False

.ApplyStyleRowBands = True

.ApplyStyleColumnBands = False

.Borders(wdBorderTop).LineStyle = wdLineStyleNone

.Borders(wdBorderLeft).LineStyle = wdLineStyleNone

.Borders(wdBorderBottom).LineStyle = wdLineStyleNone

.Borders(wdBorderRight).LineStyle = wdLineStyleNone

.Borders(wdBorderVertical).LineStyle = wdLineStyleNone

.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

End With

End Sub

Sub EquationTable2()

' Create a table with 2 columns suitable for equations of form

' x = sin a / cos a (9)

' x = tan a (10)

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

CreateTable (2)

Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(15.6)

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

'Insert equation

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="="

'Third cell

Call FinishOff(SaveFontSize)

End Sub

Sub EquationTable3()

' Create a table with 3 columns suitable for equations of form

' x = sin a / cos a (9)

' = tan a (10)

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

CreateTable (3)

'In first cell

Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints

Selection.Tables(1).Columns(1).PreferredWidth = CentimetersToPoints(3.38)

Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

'Insert right justified equation

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="x"

Selection.OMaths(1).ParentOMath.Justification = wdOMathJcRight

'Second cell

Selection.Move Unit:=wdColumn, Count:=1

Selection.SelectColumn

Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(11.62)

Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

'Insert left justified equation - For some reason this does not work! :-(

'The equation is not created

' Selection.Font.Size = EquationFontSize

' WordBasic.EquationEdit

' Selection.TypeText Text:="="

' Selection.OMaths(1).ParentOMath.Justification = wdOMathJcLeft

'Third cell

Call FinishOff(SaveFontSize)

End Sub

Sub FinishOff(FontSize)

'Do the last column which contains formula number. We start at second last column

'Also vertically align central (useful if formula gets tall with fractions or matrices etc)

Selection.Move Unit:=wdColumn, Count:=1

Selection.SelectColumn

Selection.Font.Size = FontSize

Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(1.38)

Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

Selection.TypeText Text:="(xx)"

Selection.Tables(1).Select

Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter

End Sub

Fig 1: Aligning equation numbers neatly using 2 x 1 tables |

Fig: LHS of equations unchanged and aligning equation numbers |

So I now have a "Quick access toolbar" like this

After the AB icon, they have the following effects

- Select table
- Add bars on table
- Remove bars from table
- Make table a box.
- Insert left justified equation (font size 12)
- Insert unbarred 2 x 1 table with centred justified equation in column 1, (xx) in column 2 (Fig 1)
- Insert unbarred 3 x 1 with right justified equation in column 1, (xx) in column 3 (Fig 2)

The macros are in WordBasic not VBA which is a bit amateurish, but WordBasic is what the Word macro recorder writes.

If you would like to use these macros click Read more and copy and paste them into the Word Visual Basic editor on the Developer tab.

The version in Read more might be out of date. I am fairly diligent about keeping back up versions here.

'Macros to help with equation editing

'By George Keeling and on my blog at https://spacetime-and-geometry.blogspot.com/

'Please feel free to use, copy and give away.

'Version2 14/10/83 tidied up a bit and got 2 of 3 equations inserting correctly!

'Added vertical alignment

'Version 3 25/10/2018 Introduced EquationFontSize

'TO FIX

'InsertEquationL gets incorrect font!

'Inserting second equation in EquationTable3() does not work

Const EquationFontSize = 12 '**** Change this if you prefer another equation font size

Sub InsertEquationL()

'Insert left justified equation with font EquationFont

'Sadly this cannot be called from inside the table creation Macros, because the font comes out wrong

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="="

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Size = EquationFontSize

'Quite often the attempt to justify the equation fails!

On Error Resume Next

Selection.OMaths(1).ParentOMath.Justification = wdOMathJcLeft

Selection.Font.Size = SaveFontSize

End Sub

Sub CreateTable(Columns As Integer)

'Create table with Columns (Columns = 2 or 3) columns

ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, NumColumns:= _

Columns, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

wdAutoFitFixed

With Selection.Tables(1)

If .Style <> "Table Grid" Then

.Style = "Table Grid"

End If

.ApplyStyleHeadingRows = True

.ApplyStyleLastRow = False

.ApplyStyleFirstColumn = True

.ApplyStyleLastColumn = False

.ApplyStyleRowBands = True

.ApplyStyleColumnBands = False

.Borders(wdBorderTop).LineStyle = wdLineStyleNone

.Borders(wdBorderLeft).LineStyle = wdLineStyleNone

.Borders(wdBorderBottom).LineStyle = wdLineStyleNone

.Borders(wdBorderRight).LineStyle = wdLineStyleNone

.Borders(wdBorderVertical).LineStyle = wdLineStyleNone

.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone

.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone

End With

End Sub

Sub EquationTable2()

' Create a table with 2 columns suitable for equations of form

' x = sin a / cos a (9)

' x = tan a (10)

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

CreateTable (2)

Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(15.6)

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

'Insert equation

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="="

'Third cell

Call FinishOff(SaveFontSize)

End Sub

Sub EquationTable3()

' Create a table with 3 columns suitable for equations of form

' x = sin a / cos a (9)

' = tan a (10)

Dim SaveFontSize

SaveFontSize = Selection.Font.Size

CreateTable (3)

'In first cell

Selection.Tables(1).Columns(1).PreferredWidthType = wdPreferredWidthPoints

Selection.Tables(1).Columns(1).PreferredWidth = CentimetersToPoints(3.38)

Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

'Insert right justified equation

Selection.Font.Size = EquationFontSize

WordBasic.EquationEdit

Selection.TypeText Text:="x"

Selection.OMaths(1).ParentOMath.Justification = wdOMathJcRight

'Second cell

Selection.Move Unit:=wdColumn, Count:=1

Selection.SelectColumn

Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(11.62)

Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft

'Insert left justified equation - For some reason this does not work! :-(

'The equation is not created

' Selection.Font.Size = EquationFontSize

' WordBasic.EquationEdit

' Selection.TypeText Text:="="

' Selection.OMaths(1).ParentOMath.Justification = wdOMathJcLeft

'Third cell

Call FinishOff(SaveFontSize)

End Sub

Sub FinishOff(FontSize)

'Do the last column which contains formula number. We start at second last column

'Also vertically align central (useful if formula gets tall with fractions or matrices etc)

Selection.Move Unit:=wdColumn, Count:=1

Selection.SelectColumn

Selection.Font.Size = FontSize

Selection.Columns.PreferredWidthType = wdPreferredWidthPoints

Selection.Columns.PreferredWidth = CentimetersToPoints(1.38)

Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

Selection.TypeText Text:="(xx)"

Selection.Tables(1).Select

Selection.Cells.VerticalAlignment = wdCellAlignVerticalCenter

End Sub

## No comments:

## Post a comment