shows Word Selection Summary Ms Access Gurus

help support this site, thank you

Word Selection Summary

get summary information about a Word selection such as where and what it is, and number of paragraphs, words, characters, bookmarks, comments, and fields

selection summary information for 2 different selections in a Word document

Quick Jump

Goto the Very Top  


Download

Download zipped BAS file you can import into your Word documents: mod_Word_SelectionSummary_s4p__BAS.zip

If you have trouble with a download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto the Top  

VBA

Standard module

This code runs in Word and gets information about the selection.

Option Explicit 

'*************** Code Start *****************************************************
' module name: mod_Word_SelectionSummary_s4p
'-------------------------------------------------------------------------------
' Purpose  : read information about the selection
'              where and what it is, and # of:
'              paragraphs, words, characters,
'              bookmarks, comments, fields
' Author   : crystal (strive4peace)
' Code List: https://msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Word_SelectionSummary.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Use at your own risk.
'-------------------------------------------------------------------------------
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           Word_SelectionSummary_s4p
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub Word_SelectionSummary_s4p() 
'230826 s4p

   'CALLS
   '  GetStoryType
   
   On Error GoTo Proc_Err 
   
   Dim sDocumentName As String _ 
      ,sMsg As String _ 
      ,sStartsWith As String _ 
      ,nCountPara As Long _ 
      ,nCountWord As Long _ 
      ,nCountChar As Long _ 
      ,nCountBookmark As Long _ 
      ,nCountComment As Long _ 
      ,nCountField As Long _ 
      ,nStoryType As Long _ 
      ,nPageNumber As Long 
   
   sDocumentName = ActiveDocument.Name 
   sMsg =  "*** selection in " & sDocumentName &  " ***"
   
   With Selection.Range 
      nStoryType = .StoryType 
      ' wdActiveEndPageNumber = 3
      nPageNumber = .Information(wdActiveEndPageNumber) 
      
      sStartsWith = Left(.Paragraphs(1).Range.Text,50) 
      
      nCountPara = .Paragraphs.Count 
      nCountWord = .Words.Count 
      nCountChar = .Characters.Count 
      On Error Resume Next 
      nCountBookmark = .Bookmarks.Count 
      nCountComment = .Comments.Count 
      nCountField = .Fields.Count 
   End With  'Selection.Range
   
   On Error GoTo Proc_Err 
   
   sMsg = sMsg _ 
      & vbCrLf &  "  StoryType= " & nStoryType &  " " & GetStoryType(nStoryType) _ 
      & vbCrLf &  "  Page Number = " & nPageNumber _ 
      & vbCrLf &  "  Starts With = " & sStartsWith _ 
      & vbCrLf &  "  #Paragraphs= " & Format(nCountPara, "#,##0") _ 
      & vbCrLf &  "  #Words= " & Format(nCountWord, "#,##0") _ 
      & vbCrLf &  "  #Characters= " & Format(nCountChar, "#,##0") _ 
      & vbCrLf &  "  #Bookmarks= " & nCountBookmark _ 
      & vbCrLf &  "  #Comments= " & nCountComment _ 
      & vbCrLf &  "  #Fields= " & nCountField 

   Debug.Print sMsg 
   MsgBox sMsg,, "Word_SelectionSummary_s4p"
      
Proc_Exit: 
   On Error GoTo 0 
   Exit Sub 
  
Proc_Err: 
   MsgBox Err.Description _ 
       ,, "ERROR " & Err.Number _ 
        &  "   Word_SelectionSummary_s4p"

   Resume Proc_Exit 
   Resume 
End Sub 

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'           GetStoryType
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Function GetStoryType(pWdStoryType As Long) As String 
'230826 s4p
   Select Case pWdStoryType 
      Case 1: GetStoryType =  "Main text"
      Case 2: GetStoryType =  "Footnotes"
      Case 3: GetStoryType =  "Endnotes"
      Case 4: GetStoryType =  "Comments"
      Case 5: GetStoryType =  "Text frame"
      Case 6: GetStoryType =  "Even pages header"
      Case 7: GetStoryType =  "Primary header"
      Case 8: GetStoryType =  "Even pages footer"
      Case 9: GetStoryType =  "Primary footer"
      Case 10: GetStoryType =  "First page header"
      Case 11: GetStoryType =  "First page footer"
      Case 12: GetStoryType =  "Footnote separator"
      Case 13: GetStoryType =  "Footnote continuation separator"
      Case 14: GetStoryType =  "Footnote continuation notice"
      Case 15: GetStoryType =  "Endnote separator"
      Case 16: GetStoryType =  "Endnote continuation separator"
      Case 17: GetStoryType =  "Endnote continuation notice"
      Case Else: GetStoryType = pWdStoryType 
   End Select  'pWdStoryType
End Function 

'*************** Code End *******************************************************
' Code was generated with colors using the free Color Code add-in for Access

Goto Top  

Reference

Microsoft Learn

from these links, you can find the rest :)

Range.Paragraphs property (Word)

Range.Text property (Word)

Range.Information property (Word)

WdInformation enumeration (Word)

WdStoryType enumeration (Word)

Goto Top  

Backstory

I'm currently writing code from Access to make template documents that will be substituted for sections when new documents are made. To make it easy to identify what to substitute, the user can make a selection in Word and save it.

My first step was to write code in Word to find out about the selection. This code is in my Normal.dotm template so it can be run on any selection. Later, I will use parts of this code in Access.

If you like this site, please Donate, thank you

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/Word_SelectionSummary.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/Word_SelectionSummary.htm

Get Help with Word and Access

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

Do you want to do more with Word (or Access)? I'd love to help you. Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top