• Useful Word Macros (All)

    • This topic has 14 replies, 9 voices, and was last updated 20 years ago.
    Author
    Topic
    #415557

    Similar to my post 68178, I’ve been wondering what the Word experts out there consider their most useful word macros and customizations.

    Cheers

    Viewing 5 reply threads
    Author
    Replies
    • #927268

      I wouldn’t dare to call myself a Word expert, but here are a few I find useful; all have been assigned to custom toolbar buttons in my setup.

      Paste the contents of the clipboard as unformatted text:

      Sub PasteUnformatted()
      On Error Resume Next
      Selection.PasteSpecial DataType:=wdPasteText
      End Sub

      Protect a document for forms without losing values already entered (useful during testing); the macro actually toggles protection mode. In recent versions of Word, the Protect Form button on the Forms toolbar does the same, but in Word 97 that button cleared form fields when turning on protection.

      Sub SafeProtect()
      On Error Resume Next
      If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
      ActiveDocument.Unprotect
      Else
      ActiveDocument.Protect wdAllowOnlyFormFields, True
      End If
      End Sub

      Paste the contents of the clipboard as a linked object, to be updated manually, inline with the text, and set line spacing to single. Meant for Excel tables and charts, for example. (If line spacing is exact, only a small part of an inline object will be displayed)

      Sub PasteOLE()
      On Error GoTo Exit_Sub
      Selection.PasteSpecial Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine
      Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceSingle
      Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
      Selection.InlineShapes(1).LinkFormat.AutoUpdate = False
      Exit_Sub:
      End Sub

      • #935180

        Hans, even if you wouldn’t, I would certainly call you an expert, and I’m sure many others around here would agree.

    • #927287

      Many of my macros are long and involved although the most commonly used one is temporary and overwritten whenever I need a macro for the day and don’t care to reuse it in the future. This allows me to create a macro that already has a button and keyboard shortcut assigned so I don’t waste time creating ways to run it.

      Some of my more useful ‘longer term’ short macros are:

      '======================================================
      Sub NumberingAutoToHardcoded()
      'Andrew Lockton - Chrysalis Design
      'converts autonumbers to hard coded
        Dim iResp As Integer
        iResp = MsgBox("This macro converts automatic paragraph numbers to hard coded." _
          & vbCr & "Click Yes to convert the entire document." & vbCr & _
          "Click No to convert only the selected paragraphs." & vbCr & _
          "Click Cancel to stop the macro.", _
          vbYesNoCancel, "Delete Hard Numbers")
        If iResp = vbYes Then
          ActiveDocument.ConvertNumbersToText (wdNumberAllNumbers)
        ElseIf iResp = vbNo Then
          Selection.Range.ListFormat.ConvertNumbersToText (wdNumberAllNumbers)
        End If
      End Sub
      '====================================================
      Sub NumberingDeleteHardcoded()
        'Andrew Lockton - Chrysalis Design
        'Only acts on selected paragraphs
        Dim iResp As Integer
        iResp = MsgBox("This macro will remove all hardcoded paragraph numbers " _
          & vbCr & "from the SELECTED paragraphs. Click OK to continue.", _
          vbOKCancel, "Delete Hard Numbers")
        If iResp = vbOK Then
          WordBasic.ToolsBulletsNumbers Replace:=0, Type:=1, Remove:=1
        End If
      End Sub
      '==========================================================
      Sub AttachReportsTemplate()
      ' Macro created by Chrysalis Design
        Dim sTempPath As String
        sTempPath = Options.DefaultFilePath(wdUserTemplatesPath)
        With ActiveDocument
          .UpdateStylesOnOpen = False
          .AttachedTemplate = sTempPath & "Reports.dot"
        End With
        ActiveDocument.UpdateStyles
      End Sub
      • #927832

        I’ve used your macros for Auto to Hardcoded and Delete Hardcoded and they’ve really come in handy. Auto to Hardcoded is something we use quite a bit (especially when we want to use PDF Maker to give us an Acrobat file with bookmarks — bookmarks don’t have numbers if you have an automatically numbered document).

        Is there any way to confine to heading levels only if you’re doing a whole document (without selecting each heading separately)? For various reasons, sometimes we need to hardcode heading levels throughout a whole document but we don’t want bullets and other lists to become hardcoded. Wish I could figure it out myself, but I need the help. Thanks.

    • #927323

      Something I find useful for letter templates etc. is a “macro button” for inserting the current date as a hard-coded line, rather than a field that keeps on updating. It also adds a superscripted date ordinal suffix – st, nd, rd, th.

      The field code appears in the document as:
      {MACROBUTTON VerboseDate [Double click this line to insert hard date]}

      The macro code (not terribly optimized I’m afraid blush) is:

      Public Sub VerboseDate()
      '
      ' VerboseDate Macro -
      ' Inserts a date at the current insersion point, in the form:
      ' "6th January, 2003"
      ' with the date ordinal formatted in superscript.
      '

      Dim MyDay, MyOrdinal
      MyDay = Day(Date)

      Select Case MyDay
      Case 1, 21, 31
      MyOrdinal = "st"
      Case 2, 22
      MyOrdinal = "nd"
      Case 3, 23
      MyOrdinal = "rd"
      Case Else ' Other values.
      MyOrdinal = "th"
      End Select

      Selection.TypeText Text:=MyDay

      With Selection.Font
      .Superscript = True
      End With
      Selection.TypeText Text:=MyOrdinal
      With Selection.Font
      .Superscript = False
      End With

      Selection.InsertDateTime DateTimeFormat:=" MMMM, yyyy", _
      InsertAsField:=False

      End Sub

      Alan

    • #927331

      I don’t like to type in some long syntaxes over and over again. So I put “wrappers” around them, allowing for some checks to prevent errors too. Like these two examples:

      Public Function ExistsCDP(strName As Variant) As Boolean
      
      Dim prop As DocumentProperty
      
          ExistsCDP = False
          If Documents.Count < 1 Then Exit Function
          For Each prop In ActiveDocument.CustomDocumentProperties
              If UCase(prop.Name) = UCase(strName) Then
                  ExistsCDP = True
                  Exit For
              End If
          Next prop
      End Function
      
      
      Public Function GetValueOfCDP(strName As String) As String
      
      Dim prop As DocumentProperty
      
          GetValueOfCDP = ""
          If Documents.Count < 1 Then Exit Function
          For Each prop In ActiveDocument.CustomDocumentProperties
              If UCase(prop.Name) = UCase(strName) Then
                  GetValueOfCDP = ActiveDocument.CustomDocumentProperties(prop.Name).Value
                  Exit For
              End If
          Next
      End Function 
      • #927717

        Hmmm what do you mean by long syntaxes?

        • #927756

          Instead of strValue = ActiveDocument.CustomDocumentProperties(“MyCDP”).Value
          I now use strValue = GetValueOfCDP(“MyCDP)
          or If GetValueOfCDP(“MyCDP) = “Yes” Then

          In my view this makes code more readable.
          And – also important ! – testing if there’s at least 1 doc and looping through the collection prevent errors.

          • #935082

            Sorry, I know this going back about a month and it is not a problem question but a general question.

            Jan,

            Your macro is extremely useful cool clapping – the question I have is what is the impact of using code directly against the wrapper – the speed of execution & the size of the document? Does it make any difference at all?

            Thanks for the response.

            Robie

            • #935085

              Using the wrapper is slower, but the difference would only become noticeable if you had a macro that manipulates document properties in a loop that is executed thousands and thousands of times. If you need the value of a document variable occasionally, you won’t notice any difference at all.

            • #935161

              Couldn’t have said it better myself… laugh

    • #935175

      The macro that consistenly saves me the most trouble is one developed with the help of some fellow loungers that cleans up those “Char Char” styles Word XP and 2003 like to insert:
      http://www.windowsdevcenter.com/pub/a/wind…dex.html?page=2%5B/url%5D
      And of course, not wanting to miss the chance for a shameless plug, my book is full of useful macros written by a variety of contributors. There’s a link below in my signature.

      Cheers!

    • #946239

      Here are some useful macros from a site called the “FREE GERMAN WORD SHOP”. They offer various word useful macros. They have created a single download that compiles all the macro’s on affer into a single download that is to be added to the STARTUP folder! (Just click on the “MACRO GALLERY” link in the second paragraph to get there!
      Click here for Useful Word Macro’s

      • #946248

        Thanks Rudi. There’s some interesting looking stuff there. Also interesting is the use of the term “draw & drop” instead of “drag & drop”. Maybe that’s how it translates literally to German?

        Alan

    Viewing 5 reply threads
    Reply To: Reply #935082 in Useful Word Macros (All)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel