Sunday, December 30, 2012

Automatic numbering of equations in MS Word with help of a VBA Macro

Imagine you have to compile a report in Office Word format and you have many equations which need to equation numbering. Microsoft does not provide an automated solution for this as many Project formatting guidelines require you to have equation alignment on center and equation number aligned on rights side.

Thought Microsoft provided an VBA script in their online web site. The script did not work the way it should be so I modified it and improved it a little bit.

Select equations you want to be captioned, and run this script  Sometimes you need to have "Enter" pressed after the equation or 2 or more enters. If something went wrong just press undo.

I've assigned CTRL+SHIFT+X to this Macro, and in a minute you can have all your equations numbered.

Enjoy




Sub CaptionRight()

   Dim Align As Integer
   On Error GoTo Bye
   Dim rng As Range, rngNew As Range

    If Selection.Range = "" Then
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    End If

    Set rng = Selection.Range
    
' If insertion point is in a table, show message and end macro.
   If Selection.Information(wdWithInTable) Then
      MsgBox "You are in a table. Please move outside of the " _
         & "table to run this macro.", vbInformation
      GoTo Bye
   End If


   ' Ask whether to center or left align equation.
   Align = MsgBox("Would you like the Equation to be " & "centered? " _
      & "(Selecting No will left-align the " & "Equation.)", vbYesNoCancel)

   If Align > 2 Then
      Selection.InsertParagraphAfter
      Selection.Collapse Direction:=wdCollapseEnd
      W = Selection.Sections(1).PageSetup.PageWidth
      L = Selection.Sections(1).PageSetup.LeftMargin
      R = Selection.Sections(1).PageSetup.RightMargin
      RTMarg = W - R - L
      CaptionLabels.Add Name:="("

      If Align = 6 Then
         tblT1 = Selection.Tables.Add(Selection.Range, 1, 3)
      Else
         tblT1 = Selection.Tables.Add(Selection.Range, 1, 2)
      End If

      tblT1.Select

      With Selection
         ' Do this if Center-align selected.
         If Align = 6 Then
            .Columns(1).Cells.Width = 20
            .Columns(3).Cells.Width = 50.4
            .Columns(2).Cells.Width = RTMarg - (50.4 + 20)

            'Represents 1.5" in Points
         Else
         ' Do this if Left-align selected.
            .Columns(2).Cells.Width = 50.4
            .Columns(1).Cells.Width = RTMarg - 50.4
            'Represents .75" in Points
         End If

         .InsertCaption Label:="(", _
            Position:=wdCaptionPositionBelow, Title:=" )"
         .HomeKey Unit:=wdLine, Extend:=wdExtend
         .Cut
         .MoveRight Unit:=wdCharacter, Extend:=wdExtend
         .Delete
         .MoveLeft Unit:=wdCharacter, Count:=2
         .Paste
         .Rows(1).Select

        ' Set the table borders to None.
         For Each x In Selection.Borders
            x.LineStyle = wdLineStyleNone
         Next x

         .Borders.Shadow = False
         .Cells(9 - Align).Select
         .ParagraphFormat.Alignment = wdAlignParagraphRight
         .Cells(1).VerticalAlignment = wdCellAlignVerticalCenter
         .Font.Bold = True
         .Rows(1).Select

         If Align = 6 Then
            .Cells(2).Select
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
         Else
            .Collapse
         End If
      End With
   End If

Bye:
Set rngNew = Selection.Range
rng.Select
rng.Cut
rngNew.Paste
End Sub

5 comments:

  1. I have one small suggestion for your blog. I suggest using a post title that briefly describes the content of the post, since the title becomes the post URL, and I am pretty sure that search engines look at the URL string when they crawl your site. That may make your site more visible in search results.

    ReplyDelete
  2. :D Thanks for suggestion. I just noticed my wierd URL.

    ReplyDelete
  3. I havent used vba before (but Im reasonably savvy with stuff like this) How would I go about implementing this? Is this a script I have to write in somewhere else and imports or what?

    ReplyDelete
  4. brilliant... you are my saviour.....

    ReplyDelete
  5. good info, If you have any issues with GPS, then dial GPS 1-916-209-6500 for GPS Technical Help Canada. Our technicians are well trained in fixing any issue you may have. No matter if you are having navigation issues, map update problems or anything else we are here to help.

    ReplyDelete