AutoHotkey Community

It is currently May 27th, 2012, 12:26 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: January 11th, 2009, 1:40 pm 
Offline

Joined: March 17th, 2008, 10:02 pm
Posts: 21
Location: Netherlands
Hi

I want to count chars in text docs, including word.
The following code fails for *.doc -- others (*.txt, *.htm(l), *.php) are ok:
only one line is counted, with 6 chars.

Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; telt aantal tekens (chars) x in bestand regel na regel en stopt ze in ArrayRegel(t)
; in = bestand  uit = lijst


FileSelectFile, SelectedFile, 3, , Open het bestand, Text Documenten (*.txt; *.doc; *.htm; *.html;

*.php;)
if SelectedFile =
{
    MsgBox, Niets geselecteerd
   ExitApp
}

Source = %SelectedFile%
ArrayRegel =
Opgeteld =
x = 0
t = 1
y = 0
Loop Read, %Source%
{
   Loop Parse, A_LoopReadLine,
   {
   x = %A_Index%

   }
     ;msgbox,,, x is %x% index is %A_Index% en t = %t%
   t = %A_Index%
   ArrayRegel%t% := x
   
}
x = 1
Gui, Add, ListView, r20 w470, regel|aantal
Loop, %t%
{
   tmp := ArrayRegel%x%
   Opgeteld += %tmp%
   ;msgbox,,, aantal tekens in ArrayRegel %x% = %tmp% opgeteld = %Opgeteld%
   LV_Add("", x, tmp)
   x += 1
}
LV_ModifyCol(1, "50 Integer")
LV_ModifyCol(2, "50 Integer")   
LV_Add("", "Totaal", Opgeteld)
Gui, Add, Text,, druk escape om te sluiten
Gui, Show

Input, OutputVar,, {Escape}
GuiClose:

Exitapp


Someone an idea ?[/b]

_________________
your eyes are like bright stars in the sea,
you clean the dishes and I watch tv...


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 11th, 2009, 3:00 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
sterrenstof ('stardust') wrote:
Hi

I want to count chars in text docs, including word.
Word is not a plain text document which is what your looking at with loop parse

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2009, 3:05 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
tank was faster than me, posted while I was logging in. Anyway, better consider to use Word COM.
Code:
COM_Init()
pdoc :=   COM_GetObject(SelectedFile)
cchr :=   COM_Invoke(pchr:=COM_Invoke(pdoc, "Characters"), "Count"), COM_Release(pchr)
COM_Invoke(pdoc, "Close")
COM_Release(pdoc)
COM_Term()
MsgBox % cchr


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2009, 6:37 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Sean may have been slower but he is definately less lazy than I

FYI
except for opening the document and selecting all and then copy
Seans method is probably the only method that would be easy and consistent to implement since many people dont have the same tool bars enabled and there are so many versions of word
Seans should work with all versions of wrod that are likely to be found in use 2k and up

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 13th, 2009, 10:22 am 
Tnx Tank & Sean !
but
got an COM err Notification when running with test.doc (3 lines, 15 chars):

<
Function Name: "Characters"
ERROR: The COM object may not be a valid one !
()
will continue ?
/>

same with "Count" and "Close"
tried to change some code (like rem brackets), but no positive results.

i considered before to copy content to clipboard, but don't like that kind of 'workarounds'. if it can be done nicer.

sorry that i'm not familiar with COM objects, i'm not lazy... but have to depend on you,
:?

ps
in vbs something like:
Code:
Set Count = CreateObject("Scripting.Dictionary")

In = WScript.StdIn.ReadAll

Lines = Split(In, vbCrLf)

For Each L in Lines
    Line = Trim(LCase(L))
    For B = 1 To Len(Line)
        C = Asc(Mid(Line, B, 1))
        If C <> Asc(" ") And (C < Asc("a") Or C > Asc("z")) Then
            'WSCript.Echo(Line)
            'WScript.Echo(String(B-1, " ") & "^")
            Line = Left(Line, B-1) & " " & Mid(Line, B+1)
            'WSCript.Echo(Line)
            'WScript.Echo(String(B-1, " ") & "^")
        End If
    Next

    Words = Split(Line, " ")
    For Each Word in Words
        If Word <> " " And Word <> "" Then
            If Count.Exists(Word) Then
                Count.Item(Word) = Count.Item(Word) + 1
            Else
                Count.Item(Word) = 1
            End If
        End If
    Next   
...


maybe the best way is to use vbs inside ahk ?


Report this post
Top
  
Reply with quote  
PostPosted: January 13th, 2009, 10:32 am 
ps2
Code:
SelectedFile = "test.doc"

output in msgbox is empty


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 13th, 2009, 6:59 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
strange pdoc is 0 which explains why the rest fails
even this fails but does activate the open word document
Code:
COM_Init()
  ObjWord := COM_GetActiveObject("Word.Application")
  COM_Invoke(ObjWord,"Visible=",True)
  COM_Invoke(ObjWord,"Activate")

pdoc_ :=   COM_Invoke(ObjWord,"document")
MsgBox   %   pdoc


which should work just fine
since the msdn says document is a member of application
Code:
---------------------------
COM Error Notification
---------------------------
Function Name:   "document"
ERROR:   Unknown name.

   (0x80020006)

Will Continue?
---------------------------
Yes   No   
---------------------------

Ok so i give

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 13th, 2009, 10:47 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
heh
got it
Code:
COM_Init()
ObjWord := COM_GetActiveObject("Word.Application")
COM_Invoke(ObjWord,"Visible=",True)
COM_Invoke(ObjWord,"Activate")
pdoc:=com_invoke(ObjWord,"ActiveDocument")
cchr :=   COM_Invoke(pchr:=COM_Invoke(pdoc, "Characters"), "Count")
COM_Release(pchr)
COM_Release(pdoc)
COM_Term()
MsgBox % cchr

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 13th, 2009, 11:22 pm 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
sterrenstof wrote:
Code:
SelectedFile = "test.doc"

It should be (or better use the full path).
Code:
SelectedFile := "test.doc"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2009, 12:57 am 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
hah shows how dumb i am i didnt point SelectedFile at anything.
Mostly because i wasnt paying attention to the origional script and because im dumb
tested with the fileselectfile line yours dows work as well

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Compliments
PostPosted: January 14th, 2009, 10:24 am 
Offline

Joined: March 17th, 2008, 10:02 pm
Posts: 21
Location: Netherlands
It works ! :P
Great Cohelpers you are... tnx a lot

You're right, pdoc needs the full path. Even when test.doc is in the same dir. Otherwise pdoc is zero.
The code of Sean works then ok.

The code of tank also works fine.

I would really like to understand/ working with com, write my own. But the content off calls (parameters) - and how to use them in right conjunction - is often unclear for 'juniors' like me.

_________________
your eyes are like bright stars in the sea,
you clean the dishes and I watch tv...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 14th, 2009, 1:55 pm 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
MSDN helps alot

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 16th, 2009, 12:24 pm 
Offline

Joined: March 17th, 2008, 10:02 pm
Posts: 21
Location: Netherlands
tank wrote:
MSDN helps alot


I'll give it a try and come back toU
(in the meanwhile, i admire com.ahk) :wink:

_________________
your eyes are like bright stars in the sea,
you clean the dishes and I watch tv...


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: chaosad, Exabot [Bot], Google Feedfetcher, jrav and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group