Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-20-2023, 09:48 AM
zelarra821 zelarra821 is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2021
Novice
Use a custom function in Word to count specific characters
 
Join Date: Jul 2023
Posts: 5
zelarra821 is on a distinguished road
Default Use a custom function in Word to count specific characters

Hello. Thank you very much for accepting me.

First of all, this is my first time doing something in VBA for Word, so I don't have much of an idea. Yes, it is true that I have worked with VBA for Excel and Access.

Let me tell you.

I have a Word document in which I have created, using VBA, a function that tells me the number of times certain characters are repeated.

Code:
Function Hecho() As Integer 

Dim iCount As Long 

Dim strSearch As String 

 

strSearch = "**" 

iCount = 0 

 

With ActiveDocument.Content.Find 

    .Text = strSearch 

    .Format = False 

    .Wrap = wdFindStop 

    Do While .Execute 

        iCount = iCount + 1 

    Loop 

End With 

 

Hecho = iCount / 2 

 

End Function
Now, I would like to display the result of that function in the document. I'm looking for information on Google and I can't find anything about it.

Could anyone help me?



Thank you so much.
Reply With Quote
  #2  
Old 07-20-2023, 10:31 AM
gmaxey gmaxey is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,600
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Function ScratchMacro()
'A basic Word Macro coded by Gregory K. Maxey
Dim strSearch As String
Dim iCount As Long, Hecho As Single
  strSearch = "**"
  iCount = 0
  With ActiveDocument.Content.Find
    .Text = strSearch
    .Format = False
    .Wrap = wdFindStop
    Do While .Execute
        iCount = iCount + 1
    Loop
End With
  Hecho = iCount / 2
  'at end of document.
  ActiveDocument.Range.InsertAfter vbCr & Hecho
  'in a content control titled Hecho.
  ActiveDocument.SelectContentControlsByTitle("Hecho").Item(1).Range.Text = Hecho
  'in an table (first table, first cell)
  ActiveDocument.Tables(1).Cell(1, 1).Range.Text = Hecho
End
lbl_Exit:
  Exit Function
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-20-2023, 11:16 AM
zelarra821 zelarra821 is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2021
Novice
Use a custom function in Word to count specific characters
 
Join Date: Jul 2023
Posts: 5
zelarra821 is on a distinguished road
Default

Thank you so much.

Yes, and it wasn't what I was looking for.

I explain.

I wanted something that would update only when I added those characters.

The document in which I am going to apply it is a list of objectives, and I want to obtain the percentage of those that I am achieving. That's why I want it to update as it marks completion.

You understand me?
Reply With Quote
  #4  
Old 07-20-2023, 01:08 PM
gmaxey gmaxey is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,600
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

No I don't. Your function already counted the number of occurrences. You said you want to display that value in the document. In the document where? When will be when you run the code.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 07-20-2023, 01:21 PM
zelarra821 zelarra821 is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2021
Novice
Use a custom function in Word to count specific characters
 
Join Date: Jul 2023
Posts: 5
zelarra821 is on a distinguished road
Default

If the function is ok. It shows me the value at the end of the document, which is where I want it to show it. What you would want is for that value to update only as you add the characters that are in the function.
Reply With Quote
  #6  
Old 07-20-2023, 02:33 PM
gmaxey gmaxey is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,600
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I don't want anything. You are the one who came here looking for help. You mention that you have experience with Excel and Access. A function in Word is nothing like a function that you might enter into an Excel field.


Typically with a Word VBA "function," you will pass arguments to it and it returns a value:
Code:
Sub Demo()
  MsgBox Add(2.5, 1.5), vbOKOnly, "SUM"
End Sub


Function Add(a, b) As Single
  Add = a + b
End Function

Your function will return the value (and place it at the end of a document, a table cell, a content control or whatever target you define) each time you run it. Word has no built-in change event that automatically detects a user adding text (or specific characters in your case) or calls a procedure e.g., your function.


Now, if you put a content control adjacent to each of your objectives and use it to enter your "**" marks then you could use the Document _ContentControlOnExit event to call your function.



Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
'Call your function here.
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 07-20-2023, 02:48 PM
zelarra821 zelarra821 is offline Use a custom function in Word to count specific characters Windows 10 Use a custom function in Word to count specific characters Office 2021
Novice
Use a custom function in Word to count specific characters
 
Join Date: Jul 2023
Posts: 5
zelarra821 is on a distinguished road
Default

Quote:
Originally Posted by gmaxey View Post
I don't want anything
Sorry, I mistranslated what I wanted to say.

Indeed, that is what I wanted to know: that there is no event that detects changes as there is in Excel. That forces you to have to create, for example, a check box that, when updated, gives you the result with the event you propose.

Thank you so much. Fixed then.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a custom function in Word to count specific characters Select a specific number of characters in word using VBA mrwoggie Word VBA 1 01-18-2022 03:08 PM
Looking for a macro for word count in one specific column of table, only rows with white background mobj Word VBA 2 10-17-2019 03:09 AM
Use a custom function in Word to count specific characters Deleting Characters in a specific location in Word 2010 ppayaw Word VBA 8 12-13-2016 08:11 AM
Use a custom function in Word to count specific characters Custom ribbon button or shortcut for a specific function Nicobisgaard Word 6 04-22-2015 04:39 AM
Use a custom function in Word to count specific characters Count characters on the fly NobodysPerfect Word VBA 1 04-20-2014 03:39 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft