#1
|
|||
|
|||
Hello,
I have to know how many words I have to translate in code that looks like this: (see attachment). Now it looks like a mess but I only have to translate the words in black. But there are many long files, so counting the words in black manually would take way too long. I thought this would do the trick, but no: Code:
Sub countblackwords() ' ' countblackwords Macro ' ' Dim wd As Range Dim CountBlack As Long CountBlack = 0 For Each wd In ActiveDocument.Words If wd.Font.ColorIndex = wdBlack Then CountBlack = CountBlack + 1 End If Next wd MsgBox "Number of words in black: " & CountBlack End Sub Much appreciated, Michiel |
#2
|
||||
|
||||
This really has nothing to do with Word, since that's not the application you're using to view the file. Furthermore, the problem with what you're trying to do is that the colouring is applied by the software you're using - it's not present in the file itself. If you were to open the file in Word, for example, you'd see no colouring. That said, all is not lost. If you look at the image you've posted, you'll see that all the black text is between <value> & </value> tags. If you open the file in Word and do a wildcard Find, where:
Find = \<value\>*\</value\> and use the 'Reading Highlight' find option, Word will tell you how many such strings there are.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
thanks a million Paul but I do not know how to make it work
Hi Paul, thanks a lot, you are right the files first open in Wordpress software, but interestingly enough when I copy the content to Word the colors remain! (Or maybe it only seems that way and Word has the same recognition of code parts as the Wordpress software)
It took me some time to get Word to find anything, but at some point it worked but .... it counts the instances that there is text between <value> and </value>, which is useful but I need to know the amount of words in all these instances together. The amount of words between <value> and </value> differs greatly so I still do not have the final answer. But obviously this is a big step forwards. Maybe I can record a macro doing this find and then change the code but that is still not easy for me. See attachment. Again thanks a lot. |
#4
|
|||
|
|||
I recorded a macro and it turns out like this:
Sub SpecialFind() ' ' SpecialFind Macro ' ' Selection.Find.ClearFormatting With Selection.Find .Text = "\<value\>*\</value\>" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchAllWordForms = False .MatchSoundsLike = False .MatchWildcards = True End With End Sub Now all I have to do is count all the word in the selection(s) and then distract all instances of the word "value". I do not know how to do that yet but I will have a look. Any suggestion is welcome! |
#5
|
||||
|
||||
Try the following macro:
Code:
Sub Demo() Application.ScreenUpdating = False Dim i As Long With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "\<value\>*\</value\>" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With Do While .Find.Found i = i + 1 + UBound(Split(.Text, " ")) .Collapse wdCollapseEnd .Find.Execute Loop End With Application.ScreenUpdating = True MsgBox i & " words between <value> & </value> found." End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Paul, this works perfect! Thank you so much.
The only thing is that I do not understand HOW it works. I would expect an IF clause somewhere, something like if .text != value then i = i + 1 But for the main purpose it has, to count the black (to be translated) words in these files it is just PERFECT. thanks a million |
#7
|
||||
|
||||
Basically, the code goes through all the <value> ... </value> strings and uses UBound(Split(.Text, " ")) to count the number of spaces. The number of words is one more than that, hence: i = i + 1 + UBound(Split(.Text, " ")).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
font color, loop through all words, vba microsoft word 2013 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Count words between quotation marks | sojiro | Word VBA | 11 | 01-29-2017 12:59 PM |
Count number of words in closed docx files | Pecoflyer | Word VBA | 2 | 11-25-2016 01:07 AM |
PNG on black background printing transparency in different tone of black | NMGMarques | Word | 0 | 03-03-2014 05:19 AM |
How can I count multiple usage of the same words? | coffee_king | Word VBA | 1 | 03-24-2012 07:52 PM |
Creating Text to count words WITHOUT title page | ingmar.s | Word | 3 | 10-08-2009 10:23 AM |