Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2001
    NY, New York, USA
    Thanked 0 Times in 0 Posts

    Word count to clipboard (Word XP)

    Is there any way to get the word count for a document onto the clipboard automatically?



  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Thanked 7 Times in 7 Posts

    Re: Word count to clipboard (Word XP)

    The word count is available as ActiveDocument.Words.Count.
    You can write text to the clipboard using a data object, and the PutInClipboard method, see for example <post#=71156>post 71156</post#>

    So we can write a simple macro as in.
    Sub WordCountToClipboard()
    Dim doClip As DataObject
    Set doClip = New DataObject <font color=448800>'instantiate DataObject</font color=448800>
    doClip.SetText Str$(ActiveDocument.Words.Count), 1 <font color=448800>'set dataobject to selected text</font color=448800>
    doClip.PutInclipboard <font color=448800>'transfer contents to clipboard</font color=448800>
    Set doClip = Nothing <font color=448800>'clear object</font color=448800>
    End Sub

    If you get an error when running this Macro, you may need to set a reference to the Microsoft Forms 2.0 object library from the VBE Tools > References menu.

    This just leaves the question of how to trigger the Macro. This depends on When you want the clipboard updated.


  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 94 Times in 90 Posts

    Re: Word count to clipboard (Word XP)

    In earlier versions of Word the Word Count features were somewhat buggy, causing grief for attorneys everywhere.

    In building your macro, bear in mind that the Words collection contains punctuation and paragraph symbols, so the count would not match a manual count. As an alternative, you can use the ComputeStatistics method of the relevant Range object. Because footnotes and other "stories" may not be included in the range that contains the main document text, this approach requires careful testing.

    For more information, see Microsoft Knowledge Base Article Q291447, "WD2002: Word Count Appears Inaccurate When You Use the VBA Words Property," and the references linked in that article.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts