Monday, March 8, 2010

MS Word 2007 Macros

As the documents that I am writing keep getting bigger, simple tasks, like reformatting every table in the document, are getting harder. So, I turned to Word macros for help. The list below contains some of the macros that I have found that make (my) life easier.





Word Tips



  1. How do I print multiple copies of a single page on 1 page?
  2. How do I copy footnotes to another document?
  3. How do I paste without formatting (quickly)?
  4. How do I reformat all the tables in a document?
  5. How do I update all the fields in a document?
  6. How do I make all the references (Figures, Tables, etc.) formatted in Italics?





  1. How do I print multiple copies of a single page on 1 page?

    In "Pages" fill in something like: 1,1,1,1,2,2,2,2

    For "Zoom", choose 4 pages per sheet.

    This prints 2 pages, the 1st with 4 copies of page 1 and the 2nd with 4 copies of page 2.



    [Tip source Graham Mayor]




    back



  2. How do I copy footnotes to another document?


    Sub
    CopyFootnotes()


    Dim sDoc
    As Document

    Dim tDoc
    As Document


    Dim sId
    As String

    Set sDoc =
    ActiveDocument

    Set tDoc =
    Documents.Add


    For i = 1
    To sDoc.Footnotes.Count

        sId = sDoc.Footnotes(i).Index

        sDoc.Footnotes(i).Range.Select


        Selection.Copy

        tDoc.Activate

       
    With Selection


            .Style = "Footnote Text"

            .Font.Superscript =
    True


            .TypeText sId & " "


            .Font.Superscript =
    False


            .Paste

            .TypeParagraph


       
    End With


        sDoc.Activate

    Next i

    tDoc.Activate


    End Sub


     




    [Tip source Graham Mayor]



    back


  3. How do I paste without formatting (quickly)?

    Use this macro:


    Sub pasteWithoutFormatting()
    '
    ' pasteWithoutFormatting Macro
    '
    '
    Selection.PasteSpecial DataType:=wdPasteText
    
    End Sub
    
    I assign this macro to "Cntl+Shift+v" so that I can use it quickly.

    [Source Microsoft]



    back



  4. How do I reformat all the tables in a document?


    Sub reformatAllTables()
    '
    ' reformatAllTables Macro
    ' Changes formatting on all tables in the document
    '
    Dim myTable As Table
       For Each myTable In ActiveDocument.Tables
          myTable.Select
            'myTable.PreferredWidthType = wdPreferredWidthPoints
            'myTable.PreferredWidth = CentimetersToPoints(16)
            myTable.AutoFitBehavior (wdAutoFitWindow)
            
          
           With Selection.ParagraphFormat
            .LeftIndent = CentimetersToPoints(0)
            .RightIndent = CentimetersToPoints(0)
            .SpaceBefore = 0
            .SpaceBeforeAuto = False
            .SpaceAfter = 0
            .SpaceAfterAuto = False
            .LineSpacingRule = wdLineSpaceSingle
            .WidowControl = True
            .KeepWithNext = True
            .KeepTogether = True
            .FirstLineIndent = CentimetersToPoints(0)
            .CharacterUnitLeftIndent = 0
            .CharacterUnitRightIndent = 0
            .CharacterUnitFirstLineIndent = 0
        End With
        
       '   Selection.Style = ActiveDocument.Styles("Normal")
       Next myTable
       ActiveDocument.Repaginate
    
    End Sub
    


    back



  5. How do I update all the fields in a document?

    Sub UpdateAllFields()
    '
    ' updateAllFields Macro
    '
    '
    
    Dim oStory As Range
    Dim oHeader As HeaderFooter
    Dim oFooter As HeaderFooter
    Dim oSection As Section
    
    For Each oStory In ActiveDocument.StoryRanges
        oStory.Fields.Update
        If oStory.StoryType <> wdMainTextStory Then
            While Not (oStory.NextStoryRange Is Nothing)
            Set oStory = oStory.NextStoryRange
            oStory.Fields.Update
            Wend
        End If
    Next oStory
    
    For Each oSection In ActiveDocument.Sections
    
        For Each oHeader In oSection.Headers
            If oHeader.Exists Then
            For Each oField In oHeader.Range.Fields
            oField.Update
            Next oField
            End If
        Next oHeader
    
        For Each oFooter In oSection.Footers
            If oFooter.Exists Then
            For Each oField In oFooter.Range.Fields
            oField.Update
            Next oField
        End If
        Next oFooter
        
    Next oSection
    
    Set oStory = Nothing
    
    End Sub
    


    back




  6. How do I make all the references (Figures, Tables, etc.) formatted in Italics?

    Sub formatReferenceFields()
    '
    ' formatReferenceFields Macro
    '
    ' Finds all references and changes the formatting to italics
    '
    Dim pRange As Word.Range
    Dim oFld As Word.Field
    Dim iLink As Long
    iLink = ActiveDocument.Sections(1).Headers(1).Range.StoryType
    For Each pRange In ActiveDocument.StoryRanges
        Do
            For Each oFld In pRange.Fields
                Select Case oFld.Type
                    Case wdFieldRef
                        oFld.Select
                        With Selection
                            .Font.Italic = True
                            .Font.Bold = False
                        End With
                    Case Else
                        'Do nothing
                End Select
            Next
            Set pRange = pRange.NextStoryRange
        Loop Until pRange Is Nothing
    Next
    
    End Sub
    

    [Tip source Greg Maxey. I modified it to change the formatting]



    back


No comments:

Post a Comment