Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    Well I had a thought last night.


    Create a function to copy & paste the text into Word and then use whatever property holds the word count to get it's value?

    That way you will always get what Word believes the count is.?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  2. #17
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    Hmm, I pasted some text into word and then Debug.Print ActiveDocument.Words.Count and got 75, yet the status bar shows 69?

    A Split() on a single space produces a Ubound of 68?, so 69 in all?

    Found out why the discrepancy.

    https://support.microsoft.com/en-us/...a-9da4ca22e3ff
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #18
    jimk100189 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2021
    Posts
    4
    I'm a novice about how to create functions. (Never did this before). Where would I start? If I went out to a 3rd party to create one for me to do that, how would I specify?

  4. #19
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    You do not need a third party, you have to start sometime.

    I knocked this up to test my theory.

    This will give you a start? Put the code into a module, then you can use it from anywhere?

    If the Split function is what you need then, something along the lines of MyWordCount will suffice. It will also need a tweak to get rid of multiple spaces? Now amended to do the check

    If you need the Word version (MyWordCount2), it is slower as it has to open word, plus you need the reference to the Word library.?

    Code:
    Option Compare Database
    Option Explicit
    
    Public Function MyWordCount(strText As String)
    Dim strWords() As String
    
    Do While InStr(1, strText, "  ") > 0 ' two spaces check
        strText = Replace(strText, "  ", " ")
    Loop
    strWords = Split(strText, " ")
    MyWordCount = UBound(strWords) + 1
    End Function
    
    
    
    Public Function MyWordCount2(strText As String)
    Dim objWord As Word.Application
    Dim strToCount As String
    
    Set objWord = CreateObject("Word.Application")
    objWord.Documents.Add
    objWord.Selection.TypeText (strText)
    objWord.ActiveDocument.Range.Select
    MyWordCount2 = objWord.ActiveDocument.Range.ComputeStatistics(wdStatisticWords)
    Set objWord = Nothing
    
    End Function
    You then call it like so
    Code:
    Private Sub Command59_Click()
    Dim lngWordCount As Long
    lngWordCount = MyWordCount(Me.History96)
    MsgBox lngWordCount
    lngWordCount = MyWordCount2(Me.History96)
    MsgBox lngWordCount
    
    End Sub
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #20
    jimk100189 is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jul 2021
    Posts
    4
    I do appreciate the assistance, but I am not familiar with access programming. Can you direct me to a tutorial to get me started?

    Lacking this, the code and instructions you have provided me (thank you, by the way), won't point me in the right direction.

  6. #21
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    You will have to say how you want to use it?

    Pretend you have a form that holds the text in a memo field that is in a control txtNotes.
    You also have a textbox txtWordCount that shows the number of words in txtNotes when you move out of txtNotes.
    in the Control Source for txtWordCount you would put

    Code:
    =MywordCount(Me.txtNotes)
    if you wanted it all within Access
    or
    Code:
    =MywordCount2(Me.txtNotes)
    if you wanted the Word version.

    You put all of the code in the first block into a module.
    Alt + F11
    Insert/Module
    Then copy and paste all the first block into that new module, probably called Module1
    You can rename it to whatever you want, just not the same as any of the function names (or any other names you have)


    Have a look here https://www.youtube.com/results?sear...+for+beginners
    HTH
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 07-23-2019, 10:27 AM
  2. Replies: 2
    Last Post: 11-01-2016, 07:32 AM
  3. Replies: 4
    Last Post: 08-07-2015, 07:49 AM
  4. Replies: 1
    Last Post: 08-04-2011, 04:17 PM
  5. OLE and Text Field with MS Word
    By WBlohm in forum Programming
    Replies: 0
    Last Post: 09-09-2010, 12:56 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums