Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-19-2017, 08:40 AM
miguelholandes miguelholandes is offline How to count the words in black Windows 10 How to count the words in black Office 2013
Novice
How to count the words in black
 
Join Date: Mar 2017
Posts: 4
miguelholandes is on a distinguished road
Default

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
Please, anybody help me?


Much appreciated,

Michiel
Attached Images
File Type: png howmanywordsinblack.png (217.1 KB, 23 views)
Reply With Quote
  #2  
Old 03-19-2017, 02:51 PM
macropod's Avatar
macropod macropod is offline How to count the words in black Windows 7 64bit How to count the words in black Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 03-20-2017, 02:19 AM
miguelholandes miguelholandes is offline How to count the words in black Windows 10 How to count the words in black Office 2013
Novice
How to count the words in black
 
Join Date: Mar 2017
Posts: 4
miguelholandes is on a distinguished road
Default 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.
Attached Images
File Type: png wordproblem.png (180.9 KB, 22 views)
Reply With Quote
  #4  
Old 03-20-2017, 02:32 AM
miguelholandes miguelholandes is offline How to count the words in black Windows 10 How to count the words in black Office 2013
Novice
How to count the words in black
 
Join Date: Mar 2017
Posts: 4
miguelholandes is on a distinguished road
Default

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!
Reply With Quote
  #5  
Old 03-20-2017, 04:20 AM
macropod's Avatar
macropod macropod is offline How to count the words in black Windows 7 64bit How to count the words in black Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 03-20-2017, 06:28 AM
miguelholandes miguelholandes is offline How to count the words in black Windows 10 How to count the words in black Office 2013
Novice
How to count the words in black
 
Join Date: Mar 2017
Posts: 4
miguelholandes is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 03-20-2017, 03:04 PM
macropod's Avatar
macropod macropod is offline How to count the words in black Windows 7 64bit How to count the words in black Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
Reply

Tags
font color, loop through all words, vba microsoft word 2013



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the words in black Count words between quotation marks sojiro Word VBA 11 01-29-2017 12:59 PM
How to count the words in black 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 to count the words in black How can I count multiple usage of the same words? coffee_king Word VBA 1 03-24-2012 07:52 PM
How to count the words in black Creating Text to count words WITHOUT title page ingmar.s Word 3 10-08-2009 10:23 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:12 PM.


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