AutoHotkey Community

It is currently May 27th, 2012, 3:19 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 296 posts ]  Go to page Previous  1 ... 14, 15, 16, 17, 18, 19, 20  Next

Should this be continued?
Poll ended at June 18th, 2008, 11:34 pm
Yes 93%  93%  [ 14 ]
No 7%  7%  [ 1 ]
Total votes : 15
Author Message
 Post subject:
PostPosted: May 12th, 2010, 4:02 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 12th, 2010, 4:52 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 12th, 2010, 5:02 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 12th, 2010, 5:21 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 12th, 2010, 6:19 pm 
Offline
User avatar

Joined: May 24th, 2009, 5:35 am
Posts: 2099
Location: Iowa, USA
This should work as well:
Code:
oDoc := COM_GetObject("C:\MyDoc.docx")
wd := COM_Invoke( oDoc, "Application" )
COM_Release( oDoc )

_________________
Image
Recommended: AutoHotkey_L
Basic Webpage Controls


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 12th, 2010, 7:08 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
Wonderful! Thanks guys. I appreciate it. I'll let you know how it turns out :)

_________________
-Jeremiah


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2010, 7:50 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2010, 9:59 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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?

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 20th, 2010, 11:24 pm 
Offline

Joined: April 20th, 2009, 1:10 pm
Posts: 817
Location: North Dakota, USA
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2010, 9:49 am 
Offline

Joined: April 22nd, 2010, 5:29 pm
Posts: 15
This is awesome! Thank you. Keep up the good work.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2010, 11:08 pm 
Offline

Joined: June 1st, 2007, 2:00 pm
Posts: 180
Great idea.

Best Regards


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2010, 11:09 pm 
Offline

Joined: June 1st, 2007, 2:00 pm
Posts: 180
How can I post yes to continue this project ?

I don't see the option

Best Regards


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2010, 6:23 pm 
Offline

Joined: November 19th, 2009, 6:23 pm
Posts: 163
Location: Florida
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2010, 6:56 pm 
You may want to read through this thread: The Com Object may not be a valid Dispatch Object!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 2nd, 2010, 7:05 pm 
Offline

Joined: November 19th, 2009, 6:23 pm
Posts: 163
Location: Florida
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 frescalus on September 2nd, 2010, 7:12 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 296 posts ]  Go to page Previous  1 ... 14, 15, 16, 17, 18, 19, 20  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Stigg, tidbit, Yahoo [Bot] and 11 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