• [^] # Re: Décompte des caractères dans OpenOffice

    Posté par . En réponse au journal Décompte des caractères dans OpenOffice. Évalué à 2.

    Bon, voila la macro modifie tiree de ton lien qui affiche aussi le nombre de caracteres si il y a une selection now :)
    Sub compte_mots
    	' Big Word Count macro by acb 
    	' This works on words and arbitrary ranges of text,
    	' and incorporates bits from Werner Roth's SO macro
    	Dim oDocument, oDesktop as Object
    	Dim oText, alpha, omega as Object
    	Dim oVCursor, mySelection As Object
    	Dim snot, countit as string
    	DIM lwords as long
    	Dim LWS as boolean
    	Dim NumFound as Integer
    	' the two following lines get the active document 
    	oDesktop = createUnoService("com.sun.star.frame.Desktop")
    	oDocument= oDesktop.getCurrentComponent()
    	oText = oDocument.Text
    	oVCursor = oDocument.currentcontroller.getViewCursor()
    	snot=oVCursor.getString()
    	alpha=oVCursor.getStart()
    	omega=oVCursor.getEnd()
    	If len(snot)>0 Then
    		'mySelection = oText.createTextCursorByRange(oVCursor.getstart())
    		'mySelection = oText.createTextCursorByRange(alpha)
    		'mySelection.goToRange(omega,true)
    		countit=oVCursor.getString()
    		msgbox(countit)
    		LWS = false
    		NumFound = 0
    		'Scan the selected text character for character
    		for i=1 to len(countit)
    			Select Case Mid(countit,i,1)
    			'Add your own seperators here
    			'chr(9) is a tab
    			'chr(10) and chr(13) are for Line- and Paragraph-ends
    			Case " ", ",", ";", ".", "-", chr(9), chr(10), chr(13)
    			'Increase the number of words only if the last character was not a seperator
    			if not LWS then
    				NumFound = NumFound + 1
    				LWS = true
    			Endif
    			'Character found, so this is no separator
    			Case Else
    			LWS = false
    			End Select
    		next i
    		msgbox "Mots comptés : " + str(NumFound) + " - Nb caracteres:" + len(countit),64,"Mots de la sélection"
    	else ' the cursor is a point
    		' count whole document
    		lwords=oDocument.WordCount
    		msgbox str(lwords)+ " Mots." ,64 ,"Mots dans le document"
    	End If
    End Sub