Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2006

Tip: Looking for answers? Try searching our database.

simulate tag clound in word doc with VBA

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sumGirl - 03 Nov 2006 22:02 GMT
Hey all. Is it possible to simulate a tag cloud in a word doc using
only VBA? I dont need the words to be hyperlinks, I would just like to
have the font size of each word set to a size based on the frequency
the word is used throughout the doc.

Thanks in advance!
-sG
Jezebel - 03 Nov 2006 23:34 GMT
Sure. Count the occurrence of each word, then set the font size accordingly.

> Hey all. Is it possible to simulate a tag cloud in a word doc using
> only VBA? I dont need the words to be hyperlinks, I would just like to
[quoted text clipped - 3 lines]
> Thanks in advance!
> -sG
sumGirl - 06 Nov 2006 15:56 GMT
Wow...what a useful reply. That never would have occurred to me. Maybe
someone with some VBA help could reply?

> Sure. Count the occurrence of each word, then set the font size accordingly.
>
[quoted text clipped - 5 lines]
> > Thanks in advance!
> > -sG- Hide quoted text -- Show quoted text -
Jean-Guy Marcil - 06 Nov 2006 21:04 GMT
sumGirl was telling us:
sumGirl nous racontait que :

> Wow...what a useful reply. That never would have occurred to me. Maybe
> someone with some VBA help could reply?

This reply was meant, I think, to show you that what you are asking is
nearly impossible as soon as you work with real document (Tables, sections
breaks, hyphens, page breaks, punctuation, etc.)
And what if a word is found 200 times? And in the same document another is
found only once? You will have 200 hundred words at font size 200 and one at
font size 1?

In any case, here's a little something to get you going.
You are re going to need to add error trapping of all sorts and try to
foresee all kinds of undesirable stuff that may end up in your document that
should be ignored.
I put the entire doc in a string otherwise the processing would take too
long...

'_______________________________________
Sub ProcessWords()

Dim strDoc As String
Dim strDocArray() As String
Dim i As Long
Dim k As Long

strDoc = ActiveDocument.Range.Text

strDoc = Replace(strDoc, Chr(13), " ")
'Remove all double spaces from string
Do While InStr(1, strDoc, "  ") > 0
   strDoc = Replace(strDoc, "  ", " ")
Loop
strDoc = Replace(strDoc, " ", "|")
'Convert to lowercase
strDoc = LCase(strDoc)
'Remove trailing "|"
Do While Right(strDoc, 1) = "|"
   strDoc = Left(strDoc, Len(strDoc) - 1)
Loop
Do While Left(strDoc, 1) = "|"
   strDoc = Right(strDoc, Len(strDoc) - 1)
Loop
strDocArray = Split(strDoc, "|")

WordBasic.SortArray strDocArray()

k = 1
i = 0

For i = 0 To UBound(strDocArray)
   If i < UBound(strDocArray) Then
       If strDocArray(i) <> strDocArray(i + 1) Then
           ReplaceFontSize strDocArray(i), k
       Else
           k = k + 1
       End If
   End If
Next

'One more time as the last one was not done
'because we got to the end of the array
ReplaceFontSize strDocArray(UBound(strDocArray)), k

End Sub
'_______________________________________

'_______________________________________
Private Sub ReplaceFontSize(ByRef strWord As String, ByRef lngFontSize As
Long)

With ActiveDocument.Range.Find
   .Text = strWord
   .Replacement.Text = strWord
   '5 is used so that we have a minimum font that is legible
   .Replacement.Font.Size = 5 + lngFontSize
   .Execute Replace:=wdReplaceAll
   lngFontSize = 1
End With

End Sub
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

sumGirl - 06 Nov 2006 21:47 GMT
Thank you! There are some rules tht can be used to keep a handle on
words that will get out of control, as well as common rules like [the,
a, is] and the trick is to pick the most common word and establish that
as your ceiling...or largest font'ed word. Everything else is downhill
from there by percentage.

> sumGirl was telling us:
> sumGirl nous racontait que :
[quoted text clipped - 85 lines]
> jmarcilREM...@CAPSsympatico.caTHISTOO
> Word MVP site:http://www.word.mvps.org
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.