AutoHotkey Community

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

All times are UTC [ DST ]




Post new topic Reply to topic  [ 296 posts ]  Go to page Previous  1 ... 8, 9, 10, 11, 12, 13, 14 ... 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: November 20th, 2009, 1:03 am 
Offline

Joined: February 12th, 2007, 7:54 am
Posts: 2462
ProtectionType is not an object, merely a pure number. It's a common mistake with a novice to COM: over-cleanup or under-cleanup. That's why I recommended COM_L/COM_U. Notice that there was no explicit call to COM_Invoke/COM_Release in my code. You no longer have to care about cleanup yourself with COM_L/COM_U. If you really want to do it yourself, just do it like:
Code:
ActiveDocument := ""


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 1st, 2009, 12:14 am 
Offline

Joined: July 25th, 2006, 7:37 pm
Posts: 490
Location: Midwest, USA
Sean> Sorry for the delayed response. I've been on vacation the last 10 days. :D

Knowing that helps on multiple levels. That explains why it was crashing. Also, not having to do COM_Release is nice as it makes for simpler code. I was just trying to apply what I had with what you had. So, yay for learning more about COM.

_________________
SilverEdge78


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 10th, 2009, 9:21 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
Here are a couple of functions I created for use with Excel that are of trivial significance. Since I often have to place data "dynamically" in worksheets I created functions to retrieve the row number and column letter/number of the active cell:

Code:
Excel_GetActiveCellRow() {

   if !oExcel := COM_GetActiveObject("Excel.Application") {
      MsgBox Could not find Excel Instance.
      Return
   }
   if !oActiveCell := COM_Invoke(oExcel,"ActiveCell") {
      MsgBox Could not get the active cell.
      Return
   }
   res:=COM_Invoke(oActiveCell,"Row")
   COM_Release(oActiveCell), COM_Release(oExcel)
   return res
 
}

Excel_GetActiveCellCol(alpha="") { ; specify anything for alpha to retrieve column letter(s)

   if !oExcel := COM_GetActiveObject("Excel.Application") {
      MsgBox Could not find Excel Instance.
      Return
   }
   if !oActiveCell := COM_Invoke(oExcel,"ActiveCell") {
      MsgBox Could not get the active cell.
      Return
   }
   n:=COM_Invoke(oActiveCell,"Column")
   COM_Release(oActiveCell), COM_Release(oExcel)
   if !alpha
      return n
   return n<=26 ? Chr(64+n)
    : (Chr(64+(!Mod(n,26) ? (n//26-1) : (n//26))) Chr(64+(!Mod(n,26) ? 26 : Mod(n,26))))
 
}


I kicked myself after I realized ahklerner's Excel_GetColumnIndex() converts the column letter(s) to the index number anyway but I was too proud of my funky little method to generate the column letter(s) to get rid of it.

On that note, if you plan to use Excel_GetActiveCellCol() to return digits to one of the other Excel functions that uses Excel_GetColumnIndex(), you'll need to modify Excel_GetColumnIndex() to support digits:

Code:
Excel_GetColumnIndex(Column) {

   t1:=t2:=0

   if RegExMatch(Column,"^\d+$")
      return Column

   StringUpper, Column, Column
   Loop, Parse, Column
      t%A_Index% := Asc(A_LoopField) - 64
   t2 != 0 ? Col := (26 * t1) + t2 : Col := t1
   Return Col

}

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 10th, 2009, 10:14 pm 
Online
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
i never convert them i just use range

_________________
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: December 14th, 2009, 7:19 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
I'm (again) trying to convert some Outlook VB code to create a new mail item in Outlook. I think I have the idea but I don't know if I'm calling the right functions to make it work. Here's the VB code:

Code:
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olMailItem)
myItem.Display


I assumed that this would be the proper COM method:

Code:
COM_Initialize()
if !Outlook:=COM_GetActiveObject("Outlook.Application")
  MsgBox, Failed to connect to Outlook.
if !NewMail:=COM_Invoke(Outlook,"CreateItem[olMailItem]")
  MsgBox, Failed to create a new mail item.
else
  COM_Invoke(NewMail,"Display")
COM_Release(NewMail),COM_Release(Outlook)
COM_CoUninitialize()
return


But that results in this error:

Code:
Function Name:   "CreateItem[olMailItem]"
ERROR:   Unknown name.
(0x80020006)


Followed by my "Failed to create a new mail item" message. I get the feeling that COM_Invoke is not the right function to call for CreateItem, though. Any suggestions?

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 14th, 2009, 7:22 pm 
Online
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
Code:
NewMail:=COM_Invoke(Outlook,"CreateItem",olMailItem)

the way you had it olMailItem was being taken as a string instead of an obj

_________________
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: December 14th, 2009, 7:25 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
That modification kicks back this bundle of joy:

Code:
Function Name:   "CreateItem"
ERROR:   Type mismatch.
   (0x80020005)

ERROR2:   Exception occurred.
   (0x80020009)

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 14th, 2009, 7:47 pm 
Online
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
you do know you also have to create the mail item here is a whole script

Code:
COM_COInitialize()
if !Outlook:=COM_GetActiveObject("Outlook.Application")
  MsgBox, Failed to connect to Outlook.
MailItem:=COM_CreateObject("Outlook.MailItem")
if !NewMail:=COM_Invoke(Outlook,"CreateItem",MailItem)
  MsgBox, Failed to create a new mail item.
COM_Invoke(NewMail,".HTMLBody","<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Please enter the message text here. </BODY></HTML>")
COM_Error(0)
  COM_Invoke(NewMail,"display",true)
COM_Release(NewMail),COM_Release(Outlook)
COM_CoUninitialize()

_________________
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: December 14th, 2009, 9:01 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
That was part of the code where I was getting tripped up as well, but I didn't know whether COM_CreateObject() was the right thing to call. Thanks for showing me the path, Jedi master. :)

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2009, 6:22 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
Another meager contribution...

Since Excel macros that set visible attributes in the page (set data in a cell, change cell color, add border, etc.) may execute more quickly when screen updating is disabled, this function will automate that process. Call it once right after COM_CoInitialize() and once right before COM_CoUninitialize() and it will take care of the rest:

Code:
Excel_ScreenUpdate() {

  static xl

  if !xl {
    xl:=COM_GetActiveObject("Excel.Application")
    COM_Invoke(xl,"ScreenUpdating","FALSE")
  } else {
    COM_Invoke(xl,"ScreenUpdating","TRUE")
    COM_Release(xl),VarSetCapacity(xl,0)
  }
  return

}

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


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

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
A contribution for Outlook and the large portion of the credit for this goes to tank.

Outlook_SetEmail() will set the To, Cc, Bcc, Subject and Body fields for an Outlook email. It can either create a new email to set the data in or set it in the current email. The To, Cc and Bcc parameters will all accept a comma-delimited list of recipients and Outlook will attempt to resolve each recipient using a helper function RecipientCheck(). I've also tried to add a little bit of smart handling for Body to try to identify when HTML is being used. Something about sending to "Bcc" in the current email is slightly buggy as it will occassionally send the last recipient to the "To" field, feel free to make any improvements:

Code:
!j::Outlook_SetEmail("true","chris@autohotkey.com","AutoHotkey"
   ,"AutoHotkey ROCKS!!!")

Outlook_SetEmail(NewMail="true",To="",Subj="",Body="",Cc="",Bcc="",Att="") {

   global itm
   olMailItem:=0,olTo:=1,olCC:=2,olBCC:=3
   bodyType:=(SubStr(Body,1,1)="<") ? "HTMLBody" : "Body"

   COM_CoInitialize()
   ol:=COM_GetActiveObject("Outlook.Application")
   if NewMail {
      itm:=COM_Invoke(ol,"CreateItem",olMailItem)
   }
   else
      itm:=COM_Invoke(ol,"ActiveInspector.CurrentItem")
   if To
      Loop, Parse, To, `,
         RecipientCheck(A_LoopField,olTo)
   if Cc
      Loop, Parse, Cc, `,
         RecipientCheck(A_LoopField,olCC)
   if Bcc
      Loop, Parse, Bcc, `,
         RecipientCheck(A_LoopField,olBCC)
   if Subj
      COM_Invoke(itm,"Subject",Subj)
   if Body
      COM_Invoke(itm,bodyType,Body)
   if Att {
      Loop % Att {
         FileSelectFile, oAtt, M
         Loop, Parse, oAtt, `n
         {
            if A_Index=1
               path:=A_LoopField
            else
               COM_Invoke(itm,"Attachments.Add",path "\" A_LoopField)
         }
      }
   }
   if NewMail {
      itm:=COM_Invoke(itm,"GetInspector")
      COM_Invoke(itm,"Display")
   }
   COM_Release(itm),COM_Release(ol)
   COM_CoUninitialize()
   return   

}

RecipientCheck(rec,type) {

   global itm

   COM_Error(0)
   n:=COM_Invoke(itm,"Recipients.Add",rec)
   COM_Invoke(n,"Resolve")
   COM_Invoke(n,"Type",type)
   COM_Release(n),VarSetCapacity(n,0)
   COM_Error(1)

}


12182009: Added a parameter to add attachments to emails(Att). The value to specify is the number of times you would like a file-attaching loop to call FileSelectFile. The multi-select option is also enabled in FileSelectFile so you can attach multiple files from multiple directories.

02102010: Corrected code to optimally produce a new email and connect to Inspector object so Display method can be properly called without the use of COM_Error().

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


Last edited by sinkfaze on February 10th, 2010, 11:43 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2009, 11:08 pm 
Offline

Joined: June 17th, 2008, 7:51 am
Posts: 243
And why not use
Code:
run mailto:chris@autohotkey.com?subject=AutoHotkey&body=AutoHotkey ROCKS!!&cc=a.b@c&bcc=x.y@z
?

_________________
Greetings
Rog


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2009, 11:27 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
ruespe wrote:
And why not use
Code:
run mailto:chris@autohotkey.com?subject=AutoHotkey&body=AutoHotkey ROCKS!!&cc=a.b@c&bcc=x.y@z
?


Well, for one thing, that doesn't require COM and since this is thread is for automating MS Office tasks using COM... :roll: :)

Other than that, this function adds the flexibility to set the data on an existing email rather than just to generate a new email. It also attempts to resolve email addresses and entered names via the address book, which AFAIK does not occur automatically when generating an email via command line.

I'm also not aware that HTML markup can be supported when generating an email from the command line, but I'm not an expert on that particular subject so I may stand corrected.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 18th, 2009, 4:27 am 
Online
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
mailto is depreciated for html5

edit seems some of the articals i read staateing the above are not verifyable

I also noticed this which is cool

http://jscode.com/generators/mailto_generator.shtml

also note the standard for mailto
http://www.faqs.org/rfcs/rfc2368.html

_________________
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 7th, 2010, 3:39 am 
Offline

Joined: March 19th, 2007, 12:43 am
Posts: 532
Can anyone explain why i get an error "function name: Display - Incorrect function" ???

Here's my code:
Code:
COM_Init()

objOutlook := COM_CreateObject("Outlook.Application")
objMailItem := COM_CreateObject("Outlook.MailItem")
objNewMail := COM_Invoke(objOutlook, "CreateItem", objMailItem)
COM_Invoke(objNewMail, "Subject=", "Yay it worked, this is my title!")
COM_Invoke(objNewMail, "Body=", "this is email text")
COM_Invoke(objNewMail, "Attachments.Add", "C:\Users\next\Desktop\test.ahk")
COM_Invoke(objNewMail, "Display")


COM_Release(objOutlook)
COM_Release(objNewMail)
COM_Release(objMailItem)


I also tried using COM_Invoke(objNewMail, "Display=", True) and it gave me the same error...


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 ... 8, 9, 10, 11, 12, 13, 14 ... 20  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Bing [Bot], Cristi®, Rajat, tidbit and 58 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