gonzax wrote:Here it is, it's part of a much bigger code but the part I need is this::
Code: Select all
; this is a gui for choosing the text I need to paste later
gui,32: destroy
GUI, 32: Default
Gui, Font, S16
Gui, Add, Text,, SELECT TEXT
Gui, Add, ListBox, vSENT gCHOOSESENT w600 r25 0x100
Gui, Add, Button, Default gCOPY, OK
Loop, G:\FILESTOSELECT\*.*
{
GuiControl,, sent, %A_LoopFileName%
}
Gui,32: Show
return
CHOOSESENT:
if A_GuiEvent <> DoubleClick
return
COPY:
GuiControlGet, sent
gui, destroy
Run, G:\FILESTOSELECT\%sent%,, UseErrorLevel
if ErrorLevel = ERROR
{
MsgBox CAN'T BE READ
return
}
; copying the contents of the chosen word doc
Progress, m2 b W400 H60 C1 ZY22 ct1A00FF CWC0C0C0 WM1000 zh0, ¡¡READING TEXT!!
Sleep 2500
clipboard=
oWord := ComObjActive("Word.Application")
oDoc := oword.documents[1]
oDoc.Range.FormattedText.Copy
Progress off
; finding out the position of the first word from which I must copy the text
StringGetPos ,delpos,clipboard,FIRSTWORD
delpos++
; final text without first X characters
oDoc.Range(delpos,oDoc.Content.End).Copy
ClipSen := ClipboardAll ;empties clipboard in case it is too big and copies its contents for later use
clipboard=
winclose, a ; close current word doc
sleep 200
;
WinActivate ,ahk_class OpusApp
; Gosub grabnames
; "Grabnames" is a routine that deletes the contents of the current word document I am working on (which is already open, even before the beginning of this script where I open the 2nd word doc and crop its text) and pastes a different header which consists of about 10 lines of a predefined text.
; so just imagine the current word document as a few lines of text with names and the script continues to paste the cropped text
; from before
; The text I am working on has wider margins than the text I am pasting later
Sleep 200
clipboard := ClipSen ; retrieve the contents of the cropped text from before to paste on the already open document
oWord := ComObjActive("Word.Application")
oDoc2 := oword.documents[1]
;oDoc2 .PageSetup := oDoc .PageSetup ; this line does nothing in my script
; this part pastes a few additional lines of text between the header and the cropped text
oWord.Selection.Font.Bold := 1
oWord.Selection.TypeText("`n`n"some predefined text)
oWord.Selection.Font.Bold := false
oWord.Selection.ParagraphFormat.Alignment := 3
oWord.Selection.TypeText("`n`n")
oDoc2.Range(oDoc2.Content.End -1, oDoc2.Content.End -1).Paste ;paste the initial cropped text
Things I would suggest:
Code: Select all
oWord := ComObjActive("Word.Application")
oDoc := oword.documents[1]
StringGetPos, delpos, oDoc.Range.Text, FIRSTWORD
delpos++
; delpos := InStr(oDoc.Range.Text, FIRSTWORD) + 1 ; Recommend using functions when possible
You don't need to copy to clipboard and then use clipboard like a variable. You can get the text from Word directly.
Code: Select all
winclose, a ; close current word doc
If you close the first document you cannot access it later which is why
;oDoc2 .PageSetup := oDoc .PageSetup fails. oDoc is closed and no longer accessable.
I don't know how this Word document got open but I would have opened it with COM and kept a handle to it to start with. Something like:
Code: Select all
wdDoc2 := wdApp.Documents.Open(filename)
You would not normally use this twice
oWord := ComObjActive("Word.Application"). Once you have a handle to the Word application, you can close and open as many documents as you need without shutting down or opening a new version of Word program.
For example:
oDoc2 := oword.documents[1] this does not have to be "1". You can refer to documents by index number or by name.
You could do something like this to get handles to open documents:
Code: Select all
oDoc1 := oWord.Documents("Report.doc")
oDoc2 := oWord.Documents("Summary.doc")
This does not open documents it just gets handles to ones already open.
It is important to realize that I often use wdApp (Word Application). Some use oWord (Object Word). The first "thing" before the dot is just a variable name and can be whatever helps you to keep things straight.
AnotherVariableName := VariableName.Documents[1]
Don't think that I am being too critical. You are doing great compared to alot of people. Just trying to help you and others see the power of COM. AHK is often alot about working with the right window which is great when people get a good handle on that but then COM takes it to a different level where it is no longer about windows but working with objects. The various Win commands are rarely needed with COM. Send, Sleep, even Clipboard, are rarely needed. This make COM very fast and efficient as it is one program talking directly to another program (directly like talking to someone on the telephone, Windows is doing alot of work like the phone company to make it happen).
FG