 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Should this be continued? |
| Yes |
|
93% |
[ 14 ] |
| No |
|
6% |
[ 1 ] |
|
| Total Votes : 15 |
|
| Author |
Message |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Wed May 12, 2010 3:02 pm Post subject: |
|
|
I've been playing around with this and having some trouble attaching to an existing Word doc. Can anyone help? What I want to do is:
Open an existing Word doc that is saved to a location.
Use Word_InsertText("")
Then save.
Here's what I have so far. Thanks to sinkfaze.
| Code: | one:="this is just a test to see if it's working."
two:=" Bold should be on."
three:=" Bold should be off. There! This now seems to be working perfectly! How nice."
COM_CoInitialize()
Word_Open()
FontSize:=10
Bold:=True
BoldOff:=False
Word_SetCurrentFont("",FontSize)
Word_InsertText(one)
Word_SetCurrentFont("",FontSize,Bold)
Word_InsertText(two)
Word_SetCurrentFont("",FontSize,BoldOff)
Word_InsertText(three)
COM_CoUninitialize()
return
Word_Open() {
wd := Word_Attach("N") ; I've tried changing this to "A" but it errors out.
COM_Invoke(wd, "Documents.Add")
; COM_Invoke(wDoc,"Add")
; COM_Invoke(wDoc,"Select")
COM_Release(wd)
return
}
Word_Attach(sInstance="A") {
SetWinDelay, 0
wd := (sInstance="N") ? COM_CreateObject("Word.Application") ; (sInstance="N")changing that to "A" erros out as well.
: COM_GetActiveObject("Word.Application")
COM_Invoke(wd,"Visible=",True)
COM_Invoke(wd,"Activate")
return wd
}
Word_InsertText(Text) {
wd := Word_Attach("A") ; Attach to Active Window
wd := COM_Invoke(wd,"Selection") ; Get Insertion Point or Selected text
COM_Invoke(wd,"TypeText", Text) ; Put the text there
COM_Release(wd)
return
}
Word_SetCurrentFont(Name="Calibri",Size="10",Bold=0,Italic=0,Caps=0) {
wd := Word_Attach("A") ; Attach to Active Window
wd := COM_Invoke(wd,"Selection.Font")
COM_Invoke(wd,"Name=", !Name ? "Calibri" : Name)
COM_Invoke(wd,"Size=", !Size ? 10 : Size)
COM_Invoke(wd,"Bold=", !Bold ? 0 : 1)
COM_Invoke(wd,"Italic=", !Italic ? 0 : 1)
COM_Invoke(wd,"AllCaps=", !Caps ? 0 : 1)
COM_Release(wd)
return
} |
_________________ -Jeremiah |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5044 Location: the tunnel(?=light)
|
Posted: Wed May 12, 2010 3:52 pm Post subject: |
|
|
What errors are you getting? I just tried your code, works perfectly fine. Have you tried rebooting?
BTW, True and False are recognized as their own values in an expression rather than as variables, so you don't need to pass them to a variable. You could handle the true/false in your particular code a few different ways:
| Code: | Bold := True
. . .
Word_SetCurrentFont("",FontSize,Bold)
. . .
Word_SetCurrentFont("",FontSize,!Bold) ; ! signifies the opposite of the value of Bold in this case |
| Code: | Word_SetCurrentFont("",FontSize,True)
. . .
Word_SetCurrentFont("",FontSize,False) |
| Code: | Word_SetCurrentFont("",FontSize,1)
. . .
Word_SetCurrentFont("",FontSize,0) |
| Code: | Word_SetCurrentFont("",FontSize,1)
. . .
Word_SetCurrentFont("",FontSize) ; parameter defaults to 0 |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Wed May 12, 2010 4:02 pm Post subject: |
|
|
First off, excellent information on the bold status. That is very helpfull. Thank you.
My code works great for creating a new instance of Word, but not grabbing a specific Word doc somewhere. For example "C:\MyDoc.docx" should be the word doc that opens up and is "attached" to. I can use Run to get it open but, the script doesn't grab that one. It creates a new one. I have changed all the instances of "N" to "A" and it still creates a new word doc. When this happens, I receive the error: "The COM Object may not be a valid Dispatch Object! First ensure that COM Library has been initialized through COM_Init()."
Any thoughts as to what I am sure I'm doing wrong? _________________ -Jeremiah |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5044 Location: the tunnel(?=light)
|
Posted: Wed May 12, 2010 4:21 pm Post subject: |
|
|
I think you're just overthinking it is all. The Word functions other than Word_Open are designed to attach to the active window. Since Word_Open by default opens up a new document it doesn't do you much good for an existing document, so it needs to be omitted.
| Code: | one := "this is just a test to see if it's working."
two := " Bold should be on."
three := " Bold should be off. There! This now seems to be working perfectly! How nice."
FontSize := 10
Run, C:\MyDoc.docx
WinWait, MyDoc.docx
WinActivate,
COM_CoInitialize()
Word_SetCurrentFont("",FontSize), Word_InsertText(one)
Word_SetCurrentFont("",FontSize,1), Word_InsertText(two)
Word_SetCurrentFont("",FontSize), Word_InsertText(three)
COM_CoUninitialize()
return
Word_Open() {
wd := Word_Attach("N") ; I've tried changing this to "A" but it errors out.
COM_Invoke(wd, "Documents.Add")
; COM_Invoke(wDoc,"Add")
; COM_Invoke(wDoc,"Select")
COM_Release(wd)
return
}
Word_Attach(sInstance="A") {
SetWinDelay, 0
wd := (sInstance="N") ? COM_CreateObject("Word.Application")
: COM_GetActiveObject("Word.Application")
COM_Invoke(wd,"Visible=",True)
COM_Invoke(wd,"Activate")
return wd
}
Word_InsertText(Text) {
wd := Word_Attach("A") ; Attach to Active Window
wd := COM_Invoke(wd,"Selection")
COM_Invoke(wd,"TypeText", Text)
COM_Release(wd)
return
}
Word_SetCurrentFont(Name="Calibri",Size="10",Bold=0,Italic=0,Caps=0) {
wd := Word_Attach("A")
wd := COM_Invoke(wd,"Selection.Font")
COM_Invoke(wd,"Name=", !Name ? "Calibri" : Name)
COM_Invoke(wd,"Size=", !Size ? 10 : Size)
COM_Invoke(wd,"Bold=", !Bold ? 0 : 1)
COM_Invoke(wd,"Italic=", !Italic ? 0 : 1)
COM_Invoke(wd,"AllCaps=", !Caps ? 0 : 1)
COM_Release(wd)
return
} |
Now this may not be a perfect solution since the document may still not have fully loaded before COM takes off, but you get the general idea. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
jethrow
Joined: 24 May 2009 Posts: 1907 Location: Iowa, USA
|
Posted: Wed May 12, 2010 5:19 pm Post subject: |
|
|
This should work as well: | Code: | oDoc := COM_GetObject("C:\MyDoc.docx")
wd := COM_Invoke( oDoc, "Application" )
COM_Release( oDoc ) |
_________________
- in case I forgot to smile
Basic Webpage Controls
COM Object Reference |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Wed May 12, 2010 6:08 pm Post subject: |
|
|
Wonderful! Thanks guys. I appreciate it. I'll let you know how it turns out  _________________ -Jeremiah |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Thu May 20, 2010 6:50 pm Post subject: |
|
|
sinkfaze, when using your code above for opening an existing word doc, I receive a COM error saying "Visible" might not be valid. I noticed there was an equals (=) sign after it, which, from what I've seen in previous codes is that it's not supposed to be there. But, I still receive the error. Any thoughts? Thanks again. _________________ -Jeremiah |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5044 Location: the tunnel(?=light)
|
Posted: Thu May 20, 2010 8:59 pm Post subject: |
|
|
I don't generally put the equals sign there when I write code but it doesn't hurt if it's there. I ran that code here and it worked fine, would you be able to reboot and try it again? _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Jeremiah
Joined: 20 Apr 2009 Posts: 797 Location: North Dakota, USA
|
Posted: Thu May 20, 2010 10:24 pm Post subject: |
|
|
Rebooted and retried. I didn't change a single line of code in your example above. I also created the MyDoc.docx document. I'm still receiving the "Invalid Dispatch Object" errors. However, when I click "Yes" through all of them, it works. _________________ -Jeremiah |
|
| Back to top |
|
 |
79516379
Joined: 22 Apr 2010 Posts: 15
|
Posted: Mon Jun 21, 2010 8:49 am Post subject: |
|
|
| This is awesome! Thank you. Keep up the good work. |
|
| Back to top |
|
 |
AutoPepe
Joined: 01 Jun 2007 Posts: 165
|
Posted: Mon Jun 21, 2010 10:08 pm Post subject: |
|
|
Great idea.
Best Regards |
|
| Back to top |
|
 |
AutoPepe
Joined: 01 Jun 2007 Posts: 165
|
Posted: Mon Jun 21, 2010 10:09 pm Post subject: |
|
|
How can I post yes to continue this project ?
I don't see the option
Best Regards |
|
| Back to top |
|
 |
The Unknown Jobber
Joined: 19 Nov 2009 Posts: 161 Location: Florida
|
Posted: Thu Sep 02, 2010 5:23 pm Post subject: |
|
|
I have a question regarding one of the excel functions...I've searched the entire thread for any issues with the function...
maybe I'm not using it correctly if so will someone please educate me?
If I use it as is:
| Code: |
Excel_GetActiveTitle(){
oExcel := COM_CreateObject("Excel.Application")
COM_Invoke(oExcel,"Visible=",True)
COM_Invoke(oExcel,"Activate")
oActiveWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
Name := COM_Invoke(oActiveWorkbook,"Name") ; Get the name
COM_Release(oActiveWorkbook)
COM_Release(oExcel)
return Name
}
|
It will just create the application but not get the active document...
so I changed it to
| Code: |
Excel_GetActiveTitle(){
oExcel := COM_GetActiveObject("Excel.Application")
COM_Invoke(oExcel,"Visible=",True)
COM_Invoke(oExcel,"Activate")
oActiveWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
Name := COM_Invoke(oActiveWorkbook,"Name") ; Get the name
COM_Release(oActiveWorkbook)
COM_Release(oExcel)
return Name
}
|
and I get the below error but it still grabs the document's name if I click yes:
| Code: |
---------------------------
COM Error Notification
---------------------------
Function Name: "Activate"
ERROR: Unknown name.
(0x80020006)
Will Continue?
---------------------------
Yes No
---------------------------
|
not trying to butcher code I'm just trying to grasp the function itself
EDIT: corrected my spelling |
|
| Back to top |
|
 |
Guest
|
|
| Back to top |
|
 |
The Unknown Jobber
Joined: 19 Nov 2009 Posts: 161 Location: Florida
|
Posted: Thu Sep 02, 2010 6:05 pm Post subject: |
|
|
Sorry for my ignorance, but I seem to be missing something, what exactly am I suppose to be pulling from that thread? I looked over it but it doesn't exactly give me what I need....
Keep in mind the first code I posted is the original function from the beginning of this thread, the second code I posted is what I modified for testing and isn't anything the author did either...
Last edited by The Unknown Jobber on Thu Sep 02, 2010 6:12 pm; edited 1 time in total |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|