Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

COM Object Reference [AutoHotkey v1.1+]


  • Please log in to reply
233 replies to this topic
Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

does this work with Outlook Express, too?

COM Object: Outlook
Purpose: Demonstrate sending an email with attachments using Outlook
System Requirements: Windows 2000 or higher
Documentation Link: Outlook Object Model
Other Links: CreateItem Method, OlItemType constants, Attachments Collection, Recipients Collection
Basic Code Example: The following code copies the files from he A:\ drive to a backup folder, then emails them as attachments to the specified recipient.
 

#NoEnv
#SingleInstance force
SetBatchLines -1
ListLines Off
SendMode Input
SetWorkingDir %A_ScriptDir%

; Autoexecute
global DataDir := A_MyDocuments "\Reports\"
global FileList := ""

gosub MakeSplashGUI

CopyFiles()
SendEmail()
ExitApp
return

CopyFiles()
{
if not FileExist("A:\*.*") {
MsgBox 4144, No Files Found
, There are no files found on drive A:\`n`nOperation aborted!
ExitApp
}

GuiControl Splash:, SplashText, Moving Files
Gui Splash:Show

; Move files from drive A:\ to backup
Loop A:\*.*
{
FileMove % A_LoopFileFullPath, % DataDir, 1
FileList .= (FileList = "") ? "" : "`n"
FileList .= A_LoopFileName
}

Gui Splash:Cancel
}

SendEmail()
{
if (FileList = "")
return

; Create email message
GuiControl Splash:, SplashText, Creating Email
Gui Splash:Show

omsg := ComObjCreate("Outlook.Application")
NewMail := omsg.CreateItem(0) ; 0 = olMailItem
NewMail.Subject := "Reports"
NewMail.Body := ""

Recipient := NewMail.Recipients.Add("Somebody's Name")
Recipient.Type := 1 ; To: CC: = 2 BCC: = 3

; Add attachments
Loop Parse, FileList, `n, `r
{
NewMail.Body .= A_LoopField "`n"
NewMail.Attachments.Add(DataDir . A_LoopField)
}

; Send the email
GuiControl Splash:, SplashText, Sending Email
NewMail.Send()

GuiControl Splash:, SplashText, Email Sent
Sleep 2000
Gui Splash:Cancel
}

MakeSplashGUI:
Gui Splash:+AlwaysOnTop -Caption +Border
Gui Splash:Font, bold
Gui Splash:Add, Text
, r2 w250 vSplashText
,
return


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011

does this work with Outlook Express, too?


No, Outlook Express has no exposed COM. These methods are for Outlook only.

My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


rbrtryn
  • Members
  • 1177 posts
  • Last active: Sep 11 2013 08:04 PM
  • Joined: 22 Jun 2011
Re-posting this without all the annoying color tags.
 
  • COM Object: Ahk.ComSurrogate.32/64
  • Purpose: Create COM objects of a specified bitness
  • System Requirements: ComDispatch.ahk (with the included ComRemote.ahk library)
  • Description:This is a simple COM script that proxies the creation of 32-bit only objects on 64-bit scripts and viceversa.
;
; File encoding:  UTF-8
; Author: fincs
;
; COM 32bit<->64bit object creation surrogate
;

#NoEnv
SendMode Input
SetWorkingDir, %A_ScriptDir%

#Persistent
;#NoTrayIcon
#SingleInstance Off
#Include ComRemote.ahk

; CLSID and APPID for this script: don't reuse, please!
if A_PtrSize = 4
{
	CLSID_ThisScript := "{EB42F5B6-31A0-4B9D-8C6D-757FE4136B2C}"
	APPID_ThisScript := "Ahk.ComSurrogate.32"
}else if A_PtrSize = 8
{
	CLSID_ThisScript := "{CC2D93BB-3818-40E6-9405-804512FF23DC}"
	APPID_ThisScript := "Ahk.ComSurrogate.64"
}else
{
	MsgBox, 16,, What kind of weird AHK build are you running?!
	ExitApp
}

ComObjError(false)
test := ComObjActive(APPID_ThisScript)
if IsObject(test)
	ExitApp ; already running
ComObjError(true)

; Register our CLSID and APPID
OnExit, IDCleanup
RegisterIDs(CLSID_ThisScript, APPID_ThisScript)

; Create a dispatch object for exposing
myObj := ComDispatch("", "ObjCreate=Wrap_ObjCreate, Close=Wrap_Close")
; Expose it
if !(hRemote := ComRemote(myObj, CLSID_ThisScript))
{
	MsgBox, 16, %A_ScriptName%, Can't remote the object!
	ExitApp
}

; End of auto-run section
return

IDCleanup:
; Remove the CLSID and APPID from the registry
RevokeIDs(CLSID_ThisScript, APPID_ThisScript)
ExitApp

RegisterIDs(CLSID, APPID)
{
	RegWrite, REG_SZ, HKCU, Software\Classes\%APPID%,, %APPID%
	RegWrite, REG_SZ, HKCU, Software\Classes\%APPID%\CLSID,, %CLSID%
	RegWrite, REG_SZ, HKCU, Software\Classes\CLSID\%CLSID%,, %APPID%
}

RevokeIDs(CLSID, APPID)
{
	RegDelete, HKCU, Software\Classes\%APPID%
	RegDelete, HKCU, Software\Classes\CLSID\%CLSID%
}

Str2GUID(ByRef var, str)
{
	VarSetCapacity(var, 16)
	DllCall("ole32\CLSIDFromString", "wstr", str, "ptr", &var)
	return &var
}

; --------------------------------------------

Wrap_ObjCreate(this, ProgIDorCLSID)
{
	return ComObjCreate(ProgIDorCLSID)
}

Wrap_Close(this)
{
	Critical
	SetTimer, _Close, -0
}

_Close:
ExitApp
You need to have it running in the background using the AHK_L build of the appropriate bitness (running both AHK_L builds at the same time is supported), then you can use these functions to create objects:
ComCreateObj32(ProgIDorCLSID)
{
	static _sur := A_PtrSize = 8 ? ComObjActive("Ahk.ComSurrogate.32") : ""
	return A_PtrSize = 8 ? _sur.ObjCreate(ProgIDorCLSID) : ComObjCreate(ProgIDorCLSID)
}

ComCreateObj64(ProgIDorCLSID)
{
	static _sur := A_PtrSize = 4 ? ComObjActive("Ahk.ComSurrogate.64") : ""
	return A_PtrSize = 4 ? _sur.ObjCreate(ProgIDorCLSID) : ComObjCreate(ProgIDorCLSID)
}
Example: ScriptControl on 64-bit AHK_L:
oSC := ComCreateObj32("ScriptControl"), oSC.Language := "VBScript"
oSC.ExecuteStatement("MsgBox ""Hello World!""")


My Scripts are written for the latest released version of AutoHotkey.

Need a secure, accessible place to backup your stuff? Use Dropbox!


jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

COM Object: VBScript.RegExp
Purpose: VBS Regular Expressions
System Requirements: General
Documentation Link: Regular Expression Object Properties and Methods
Basic Code Example: Global Regex match & replace
 

haystack := "<A id='MyLink'>one<SPAN id='MyText'>two</SPAN>three</A>"

regex := ComObjCreate("VBScript.RegExp")
regex.Global := true
regex.IgnoreCase := true
regex.Pattern := "<.*?(id='\w+')?>"

match := regex.Execute(haystack)
t := "Haystack: " haystack "`nNeedle:`t " regex.Pattern "`n`n"

t .= "`tPos (-1)`tLen`tVal`n`t------------------------------------------`n"
for item in match {
	_ := "[" A_Index-1 "]`t" 
	t .= _ item.FirstIndex "`t" item.Length "`t" item.value "`n"
	s .= _ item.SubMatches(0) "`n"
}
;// first character in the string is identified as 0
MsgBox,, % match.count " Matches", %t%
MsgBox,, SubPatterns, %s%
MsgBox,, Tags Replaced, % regex.Replace(haystack, "|")


Hamlet
  • Members
  • 302 posts
  • Last active: Mar 23 2014 03:37 PM
  • Joined: 22 Jan 2009

 

COM Object: VBSript.RegExp
Purpose: VBS Regular Expressions
System Requirements: General
Documentation Link: Regular Expression Object Properties and Methods
Basic Code Example: Global Regex match & replace
 

 

 

I have had no chance to say you (guyS) some short words "THANK YOU VERY MUCH".

I am using the COM information you (guyS) posted very frequently in everyday works.

 

The above information reminds me a tiny post of mine^^.

It took me more than a year to go there.

 

 

Thanks again !!!



stevenp
  • Members
  • 197 posts
  • Last active: Sep 23 2014 05:47 PM
  • Joined: 28 Aug 2006

Just upgraded to the 64-bit version of AHK and have found that the VBScript.RegExp object is not working anymore.

Thanks to this topic I've downloaded and run the BypassCOM.exe which registers a CLSID that creates a COM 32bit<->64bit object surrogate mentioned few posts earlier. 

 

Even though the ScriptControl object creation is successful, i'm unable to create the VBScript.RegExp object with it

regEx := ComCreateObj32("VBScript.RegExp")
regEx.ignoreCase := true
regEx.pattern    := "find"
match := regEx.execute("str find")

:S  


"Simplifying complexity is not simple"

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

VBScript.RegExp object works fine on my pc using 64-bit AHK.



stevenp
  • Members
  • 197 posts
  • Last active: Sep 23 2014 05:47 PM
  • Joined: 28 Aug 2006

hmm,

jethrow, i would appreciate if you could provide a snippet of how it was loaded and what surrogate script did you used

Thanks


"Simplifying complexity is not simple"

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

Let me rephrase - this example works as expected using 64-bit AHK on my pc - no surrogate script needed. What exactly do you mean the VBScript.RegExp object isn't working?

 

You might also check this:

RegRead, D, HKCR, VBScript.RegExp
if Errorlevel ; or If D =
	MsgBox, DLL Not Registered
else
	MsgBox, DLL is Registered


Menixator
  • Members
  • 744 posts
  • Last active: Sep 01 2015 02:54 PM
  • Joined: 10 Jul 2013

COM Object: Foobar2000.Application.0.7
Purpose: Manipulate the foobar2000 media player.
System Requirements: Foobar2000(You must have the COM Automation Server installed as well.)
Documentation Link: http://www.hydrogena...showtopic=39946
Example:

Spoiler


Kibo
  • Members
  • 36 posts
  • Last active: Aug 09 2015 07:56 PM
  • Joined: 14 Mar 2012

Hello,

 

How do I get an equivalent for "using System.Management" in AHK working? (like here)

I want to read out some Hardware information.


System := ComObjCreate("System.Object")

works. but I dont get any further  :(

 

I am using AHK_L on 64 bit system.

 

regards



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

Kibo, you can probably use CLR_L.



Kibo
  • Members
  • 36 posts
  • Last active: Aug 09 2015 07:56 PM
  • Joined: 14 Mar 2012

Hi Lexikos,

 

Thanks for Response.

My Testcode looks like this:

#include CLR.ahk
#include COM.ahk

c# =
(
    using System.Windows.Forms;
    using System.Management;
    class Foo {
        public void Test() {

		  ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
		  MessageBox.Show("CPU" + searcher);

        }
    }
)


CLR_Start()

asm := CLR_CompileC#(c#, "System.dll | System.Management.dll | System.Windows.Forms.dll")
obj := CLR_CreateObject(asm, "Foo")
COM_Invoke(obj, "Test")
COM_Release(obj)
COM_Release(asm)

On execute, I get an error: 0x80020006 - unknown name

specifically: prm_

 

the error seems to be in line 553 in com.ahk

 

after that error comes "No COM Dispatch Object!" in Functionname "Test"

 

any idea?



Kibo
  • Members
  • 36 posts
  • Last active: Aug 09 2015 07:56 PM
  • Joined: 14 Mar 2012

OK, post #133 of your CLR thread solves the problem. (you answered, so thanks again!) :D

 

regards



chaidy
  • Members
  • 57 posts
  • Last active: Oct 21 2015 05:53 PM
  • Joined: 20 Apr 2010

Follow this page.