Giving Macros a Try

To learn some macro basics, click here: Intro to Macros. There you will find some simple explanations and detailed steps to try using macros. The link also contains some excellent resources for learning more about the power of macros in MS Word. For simpler macros than the ones in the PDF, try the two style sheet macros below!

Revised, simple style-sheet macro

Use the following macro to copy whatever text you have selected in one file, paste it into your style sheet, and return to the file you were working on. Remember, only copy the "filling" of the macro sandwich. Don't copy the "bread" (the "Sub Style_Sheet()" or the "End Sub" lines) when creating your macro.

Sub Style_Sheet()

' Style_Sheet Macro
' Adds selected term to style sheet and returns to original file.
    Dim currentWindow As Window
    Set currentWindow = ActiveDocument.ActiveWindow
    Selection.Copy
    Windows("Style Sheet").Activate
    Selection.EndKey Unit:=wdStory
    Selection.TypeParagraph
    Selection.Paste
    currentWindow.Activate
   
End Sub

 


This macro selects your current window, and after it copies the term to your style sheet, it switches back to that original window. So you only have to change "Style Sheet" as the hard-coded part, but can run the same macro from any file.

Revised, simple macro for pasting inverted name into style sheet.

This simple macro, adapted for me by my colleague Noah Haibach, enables you to copy a personal name (e.g., Chris Smith) from one file, paste it inverted (Smith, Chris) into your style sheet, and return to the file you were working on. Remember, only copy the "filling" of the macro sandwich. Don't copy the "bread" (the "Sub_InvertNameToStyle()" or the "End Sub" lines) when creating your macro.

Sub InvertNameToStyle()

' Inserts name in style sheet and inverts it.
' Style sheet filename must be "Style Sheet" (w/out quotes)
  
    Dim currentWindow As Window
    Set currentWindow = ActiveDocument.ActiveWindow
   
    Dim currentName As String
    currentName = Selection.Text
    ' Remove both line feeds and carriage returns (in Windows can be both)
    currentName = Replace(currentName, vbLf, "")
    currentName = Replace(currentName, vbCr, "")
    currentName = Trim(currentName)
   
    Dim currentNameLen As Integer
    currentNameLen = Len(currentName)
   
    Dim spacePosition As Integer
    spacePosition = InStrRev(currentName, " ")
   
    Dim formattedName As String
    ' Right gives us the number of characters from the right side; last name
    ' Left gives us the number of characters from the left; first (and middle)
    ' We subtract one from Left side to remove the space between the names
    formattedName = Right(currentName, currentNameLen - spacePosition) _
       & ", " & Left(currentName, spacePosition - 1)
       
   
    Windows("Style Sheet").Activate
    Selection.EndKey Unit:=wdStory
   
    Selection.TypeParagraph
    Selection.TypeText (formattedName)
   
    currentWindow.Activate
End Sub

 


© 2018 by PATRICIA E. BOYD.

  • Twitter Clean