AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

MS Office Automation Functions (via COM) [thanks Sean]
Goto page Previous  1, 2, 3, 4, 5 ... 18, 19, 20  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  

Should this be continued?
Yes
93%
 93%  [ 14 ]
No
6%
 6%  [ 1 ]
Total Votes : 15

Author Message
Krogdor



Joined: 18 Apr 2008
Posts: 1390
Location: The Interwebs

PostPosted: Mon Aug 25, 2008 4:56 am    Post subject: Reply with quote

ahklerner -

Made a couple of Word automation functions for myself, figured I might as well post them here. Feel free to add them to the library or not. I used the same format as you did for the function description.

Code:
; **************************************************************************
; Author:   Krogdor
; Language:   AutoHotkey v1.0.47.06
; Creation Date:   08/24/2008
; Function Name:   Word_Align()
;
; Align specified paragraph (Right/Center/Left, and Justify)
;
; Syntax:
;    Word_Align(Paragraph,Alignment)
; Parameters:
; 1)   Paragraph = The paragraph number that is to be aligned. Any places where
;      there is a newline counts as a new paragraph, so blank lines count as well.
; 2)   Alignment  = How to align it:
;        0 = Left (default)
;        1 = Center
;        2 = Right
;        3 = Justify
; Return:
;    Success = nothing
;    Failure = nothing
; **************************************************************************

Word_Align(Para = 1, Alignment = 0) {
  If (Alignment < 0 || Alignment > 3)
    Alignment = 0
  Word_Attach("A")
  ActiveDocument := COM_Invoke(Word,"ActiveDocument")
  Paragraphs     := COM_Invoke(ActiveDocument, "Paragraphs", Para)
  COM_Invoke(Paragraphs,"Alignment=",Alignment)
  COM_Release(Word)
  COM_Release(ActiveDocument)
  COM_Release(Paragraphs)
}


; **************************************************************************
; Author:   Krogdor
; Language:   AutoHotkey v1.0.47.06
; Creation Date:   08/22/2008
; Function Name:   Word_Header()
;
; Insert a header into the document with specified text.
;
; Syntax:
;    Word_Header(Text,Style)
; Parameters:
; 1)   Text   = the text string to insert into the document
; 2)   Style  = which pages to insert on:
;        1 = All pages  (default)
;        2 = First page
;        3 = Even pages
; Return:
;    Success = nothing
;    Failure = nothing
; **************************************************************************

Word_Header(Text, Style = 1, Word = "") {
  If (Style < 1 || Style > 3)
    Style = 1
  Word_Attach("A")
  ActiveDocument := COM_Invoke(Word,"ActiveDocument")
  Section        := COM_Invoke(ActiveDocument,"Sections",1)
  COM_Invoke(COM_Invoke(COM_Invoke(Section,"Headers",Style),"Range"),"Text",Text)
  COM_Release(ActiveDocument)
  COM_Release(Word)
  COM_Release(Section)
}
Back to top
View user's profile Send private message AIM Address
rares
Guest





PostPosted: Mon Aug 25, 2008 12:14 pm    Post subject: help with script Reply with quote

hello i have the following script:

COM_Init()
Sleep 1000
Word_InsertText("cat")
COM_Term()
exitapp

#include com.ahk
Word_InsertText(Text){
oWord := COM_GetActiveObject("Word.Application") ; Attach to Active Window
oSelection := COM_Invoke(oWord,"Selection") ; Get Insertion Point or Selected text
COM_Invoke(oSelection,"TypeText",dog) ; Put the text there
COM_Release(oSelection) ; cleanup
COM_Release(oWord) ; cleanup
}

Word_Open(){
oWord := COM_CreateObject("Word.Application") ; Attach to New Window
oDocuments := COM_Invoke(oWord, "Documents")
COM_Invoke(oWord,"Visible=",True)
COM_Invoke(oWord,"Activate")
COM_Invoke(oDocuments,"Add") ; Add a new document
COM_Invoke(oDocuments,"Select") ; Select it
COM_Release(oDocuments) ; cleanup
COM_Release(oWord) ; cleanup
}

it's sopose to put the words specified by me after certain words that it finds in the word document . For example, the script finds the word " dog ", I want it to paste the word "cat " after the word "dog"!

I want you guys to help me, if you can. I want to add more words for the computer to search and add after them the words i specify, resembling auto complete in Opera or the magic wand.
Back to top
ABCza



Joined: 03 Jun 2008
Posts: 75
Location: Italy

PostPosted: Sat Nov 29, 2008 3:09 am    Post subject: Reply with quote

Hi Smile
I'm not much involved in COM things; there's a way to loop the document paragraphs in MS Word? I mean something like:

Paragraph_1
ssdfjhsdfjksdhfhsjkdhfkjhfsjkdfkjsdhfjsdfdkhfhsdjfhsdkfjkdfsdj sdfsdjkfkj sdfjkhsdfkhsdjf sdfjksdhfhsdfjksdhf sdfjsdfh

Paragraph_2
sdfsdfhjsdhfsdjkf sdfjkhsdjkfsdjhfjksdhfjkshdfjks sdfjksdjkfdjkfdjk
skdjfjksdfjksjkdhfjkfsdjk sdjksdjkfhjk

Paragraph_3
dkjfgkljdfgjdfjgkljdfgkldfjklgd weruioruu werwuriouwer iowe mklmdgkldfklgjklj werpoweirpwero m,.fng,dfnfm,gm
_________________
All my scripts/snippets are released under the WTFPL: http://sam.zoy.org/wtfpl/COPYING
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sinkfaze



Joined: 18 Mar 2008
Posts: 5044
Location: the tunnel(?=light)

PostPosted: Tue Jan 20, 2009 4:21 am    Post subject: Reply with quote

So if I don't want to load a macro into an Excel spreadsheet but I want to run an existing one (in the interests of creating a hotkey), I can essentially trim the code down to this for example?

Code:
COM_Init()
Excel_Run("PERSONAL.XLS!Anything")
COM_Term()
return

Excel_Run(sFunction)
   if oExcel := COM_GetActiveObject("Excel.Application")
      COM_Invoke(oExcel,"Run", sFunction)
   COM_Release(oExcel)


You'll have to forgive me, I've tried to read up but COM/expressions are largely beyond me, so the syntax could be obviously incorrect and I wouldn't know.
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message Send e-mail
enigmatiqk



Joined: 13 Apr 2007
Posts: 28

PostPosted: Fri Mar 13, 2009 9:52 am    Post subject: Reply with quote

aqtd wrote:
An error occurred while debugging
Function Name:Activate
ERROR


hello, nice job for all work !!!

but i have a lot of error:
1) i have the same error about function name activate (i have passed out with ";")

2) Excel_GetCell : "could not get cell" but i don't know why (she exist and have data), before this i have an com error with an hexa code...

3) excel close don't close my excel instance
Back to top
View user's profile Send private message MSN Messenger
shellcode



Joined: 28 Feb 2009
Posts: 1

PostPosted: Mon Mar 16, 2009 2:53 pm    Post subject: Reply with quote

so cool.


thanks for script man
Back to top
View user's profile Send private message
truevis



Joined: 08 Sep 2008
Posts: 31

PostPosted: Wed Mar 18, 2009 1:08 am    Post subject: Re: MS Office Automation Functions (via COM) [thanks Sean] Reply with quote

ahklerner wrote:
[I will make the post better later.. Smile ]
Tested on Office 2003 Pro...
I'm getting a nonexistent function error on this line:
Code:
sCaption := "[" . UUID() . "]"
Please advise. (Where can I get the UUID function?)
Back to top
View user's profile Send private message Visit poster's website
truevis



Joined: 08 Sep 2008
Posts: 31

PostPosted: Wed Mar 18, 2009 1:09 pm    Post subject: Reply with quote

I added this to my Word.ahk script and it seems to work, now.
Code:
uuid() { ; v1.0 - by Titan
   static n, l
   n += l = A_Now
   f := A_FormatInteger, t := A_Now, s := ""
   SetFormat, Integer, H
   t -= 1970, s
   t := (t . A_MSec) * 10000 + 122192928000000000
   Random, x, 0x100, 0xfff
   Random, y, 0x10000, 0xfffff
   Random, z, 0x100000, 0xffffff
   l := A_Now, t += n
   SetFormat, Integer, %f%
   Return, SubStr(t, 10) . s . SubStr(t, 6, 4) . s . 1 . SubStr(t, 3, 3)
      . s . 9 . SubStr(x, 3) . s . 1 . SubStr(y, 3). SubStr(z, 3)
   }
I can now understand the Id of Unitarian Universalists. Shocked
Back to top
View user's profile Send private message Visit poster's website
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sat Mar 28, 2009 1:07 pm    Post subject: Reply with quote

enigmatiqk wrote:
aqtd wrote:
An error occurred while debugging
Function Name:Activate
ERROR


hello, nice job for all work !!!

but i have a lot of error:
1) i have the same error about function name activate (i have passed out with ";")

2) Excel_GetCell : "could not get cell" but i don't know why (she exist and have data), before this i have an com error with an hexa code...

3) excel close don't close my excel instance


1)The workbooks seems to have no Activate method, I alter it to
COM_Invoke(oWorkbooks, "Worksheets[Sheet1].Activate")

2)I also have tried it. there's the same error. Then I fixed the code as follow:
oCells := COM_Invoke(oWorkbooks,"Worksheets[Sheet1].Cells.Item[1].Item[1]")
Value := COM_Invoke(oCells,"Value")
Msgbox, %Value%

It works correctly.
Maybe the fucntion Cells(col, row) can't be used. However, Cells.Item[col].Item[row] is not so conviencent sometimes.
But I don't know why. Who can tell me?

sorry for my poor english.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Sat Mar 28, 2009 3:48 pm    Post subject: Reply with quote

Use Range.
Code:
Value := COM_Invoke(oWorkbooks,"Worksheets[Sheet1].Range[A1].Value")
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sat Mar 28, 2009 5:12 pm    Post subject: Reply with quote

Sean wrote:
Use Range.
Code:
Value := COM_Invoke(oWorkbooks,"Worksheets[Sheet1].Range[A1].Value")


But why
Code:

Value := COM_Invoke(oWorkbooks,"Worksheets[Sheet1].Cells(1,1).Value"


can't return correct value?
We can write it as above in VBA.
It's easier to loop through every cell using Cells(row,col) method than Range
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Sat Mar 28, 2009 11:55 pm    Post subject: Reply with quote

Use like this, and read the corresponding thread about what you can or can't use.
Code:
oCell := COM_Invoke(oWorkbooks,"Worksheets[Sheet1].Cells",1,1)
Value := COM_Invoke(oCell,"Value")
COM_Release(oCell)
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Sun Mar 29, 2009 2:39 pm    Post subject: Reply with quote

In fact, I have tried both of methods you wrote. They throw the same error notification:
Quote:

Function Name: "Value"
ERROR: The COM Object may not be a valid Dispatch Object!


It seems that oCell return null

I use Office2003, winxp.
Back to top
View user's profile Send private message
Sean



Joined: 12 Feb 2007
Posts: 2462

PostPosted: Mon Mar 30, 2009 12:27 am    Post subject: Reply with quote

hughman wrote:
It seems that oCell return null

It used to work, so I didn't care about it but the recent change broke it. It's because the property Cells allows variable number of indices, thus the supposed PropertyGet is actually carried out as PropertyPut. It was a logical possibility, however, I thought it was rather unlikely and wasn't detected in my test as I changed the function Cells to Evaluate for some reason in my Excel script, unfortunately.
Now it turns out that there is absolutely no way to completely eliminate an artificial suffix in the name! The remained question is to choose which between two: retain the current one and introduce another suffix, or roll back to the older one. I'll announce it in the thread of COM.ahk if I decided.
Back to top
View user's profile Send private message
hughman



Joined: 11 Feb 2007
Posts: 166

PostPosted: Mon Mar 30, 2009 5:28 am    Post subject: Reply with quote

I have noticed that you update the thread of COM.ahk. And I have tried it with the same result.
Now I know that the round brackets can't be used for Com_Invoke. ^_^

My friend remind me of the fact that if in vba Cells is a funtion or not.
Maybe Cells not be treated as a funtion? So we can't use it like other object.function(param) as below? And so the funtion Excel_GetCell supplied by this thread's owner can not work correctly too. This is what I guess. I am just a newbie:)
Code:

oCell := COM_Invoke(oWorkbooks, "Worksheets[Sheet1].Cells", 1, 1)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5 ... 18, 19, 20  Next
Page 4 of 20

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group