creating a new email from GUI with several attachments

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
duzymichal
Posts: 16
Joined: 31 Mar 2017, 04:11

creating a new email from GUI with several attachments

01 Apr 2017, 16:52

Hello

In the first, and perhaps the last gui before I learn proper syntax :shifty: I need to create a form that enables users to input (outlook) email address, recipient's name along with a few variables in the body. The rest of the body would be a predetermined message + attachments from set folders (with filenames changing a bit periodically). The output would be a new unsent email. I've found 2 scripts whose combination would resolve the issue. As much as I tried to combine the two, the output would show the qualities of either but not both. I do realize that I'm playing alchemist where an engineer is needed so I'd appreciate any assistance in this :oops: .

The two scripts in question are:
Synkfaze's (ideal for my purpose since there will always be two files to be attached in a folder with filenames changing a bit but always retaining core name, and I do *corename* instead of file extension in this case)

Code: Select all

mailItem :=	ComObj("Outlook.Application").CreateItem(0)	; creates new mail item
Loop, path to Excel files\*.xlsx	; or .xls, whichever
	mailItem.Attachments.Add(A_LoopFileLongPath)	; attaches each file
mailItem.GetInspector.Display()	; makes mail item visible
and stjung's (from the post I read I inferred it had a mistake but it worked for me since I had different variables)

Code: Select all

Gui, Add, Text,, RECIPIENT NAME
Gui, Add, Edit, vRecipient
Gui, Add, Text,, RECIPIENT EMAIL
Gui, Add, Edit, vEmail
Gui, Add, Text,, TOPIC
Gui, Add, Edit, vTopic
Gui, Add, Edit, vMyFile, 
GUI, Add, Button, ym gBtnBrowse, &Browse 
Gui, Show
Gui, Add, Button, gAddText, Compose
Gui, Show, AutoSize Center, Email generator
Return

AddText:
gui, submit
Outlook := ComObjActive("Outlook.Application")
email := Outlook.Application.CreateItem(0)
email.To := Email
email.Body := "Dear " Recipient ",`r`nThank you for contacting me.`r`n`Here is the file about " Topic "."
email.Subject := Your requested information about " topic
email.Attachments.Add(SelectedFile, 1, 1, SelectedFile)
email.Display(true)

Gui, Destroy
Return

BtnBrowse:
FileSelectFile,SelectedFile
If SelectedFile
  GuiControl,, MyFile, %SelectedFile% 
return
I already have a working GUI but this would have to be implemented in its several tabs.

I have another question: Would it be possible to add attachments between the paragraphs in the body? A paragraph, then an attachment, then another paragraph and attachment. The paragraphs are predetermined.
User avatar
tomoe_uehara
Posts: 213
Joined: 05 Oct 2013, 12:37
Contact:

Re: creating a new email from GUI with several attachments

01 Apr 2017, 16:58

Hello, you can find such info on AHK Documentation, it's located on your AHK installation directory.
You can search and learn about thing called as "Variable", where you can put some data on it.
For example:

Code: Select all

var1 = banana
var2 = apple
var3 = orange
msgbox Example 1: I want to eat a %var1%, I don't like %var2%s, but I only have %var3%
var4 := "Example 2: I want to eat a " . var1 . ", I don't like " . var2 . "s, but I only have " . var3
msgbox % var4
return
Or maybe I misunderstoood your question?
garry
Posts: 3763
Joined: 22 Dec 2013, 12:50

Re: creating a new email from GUI with several attachments

01 Apr 2017, 17:01

just short, an example with thunderbird

Code: Select all

filex=%a_scriptdir%\test55.txt
ifnotexist,%filex%
  Fileappend,Line1`r`nLine2`r`n,%filex%
tox     [email protected]
subject =Test55
body    =FollowedLine1`%0AFollowedLine2
dates   =%a_now%
      TO=to='%TOX%'
      PR=%A_ProgramFiles%\Mozilla Thunderbird\thunderbird.exe
      SB=subject=%subject%
      AT=attachment='%FILEX%'
      BD=body=Hallo Garry`%0A`%0AHow are you ?`%0A`%0A%body%`%0A`%0AGreetings`%2C`%0A garry`%0A`%0A`%0A%dates%
      ALL=%TO%,%SB%,%AT%,%BD%
      run,%PR% -compose "%ALL%"
exitapp
duzymichal
Posts: 16
Joined: 31 Mar 2017, 04:11

Re: creating a new email from GUI with several attachments

01 Apr 2017, 17:36

I guess I made an elongated introduction without really getting into the crux of the matter ;) .

What I mean is: how do I replace stjung's

Code: Select all

email.Attachments.Add(SelectedFile, 1, 1, SelectedFile)
email.Display(true)
with Synkfaze's

Code: Select all

mailItem :=	ComObj("Outlook.Application").CreateItem(0)	; creates new 

mail item
Loop, C:\Users\a010101\Desktop\forms\Resale\*corename*	
	mailItem.Attachments.Add(A_LoopFileLongPath)	
mailItem.GetInspector.Display()
so that it works.

I can do the email address, recipient's name and body variables using stjung's script.
And the attachments using Synkfaze's.
I need to do both.
duzymichal
Posts: 16
Joined: 31 Mar 2017, 04:11

Re: creating a new email from GUI with several attachments

02 Apr 2017, 07:39

OK I somehow combined it so that it shows everything that I want. The remaining problem is the positioning of the attachments in the body. There will always be 2 files attached: one with .xlsx and one with .png extension, both sharing *resale* core name. I'd like it so that each attachment would be shown after a specified paragraph in the body. Right now they kind of show up wherever they want.

Also I noticed that by applying Synkfaze's script only, my signature shows up in the body. In my frankenstein script (frankenscript :mrgreen: ) it no longer does :? .

This is what I've come up with:

Code: Select all

Gui, submit,




try
    IsObject(ObjOutlook := ComObjActive("Outlook.Application"))
catch
    ObjOutlook  := ComObjCreate("Outlook.Application")


mailItem :=	ComObj("Outlook.Application").CreateItem(0)

Loop, C:\Users\a631958\Desktop\forms\Resale\*resale*	 
	mailItem.Attachments.Add(A_LoopFileLongPath)
mailItem.GetInspector.Display()	

MailItem.Body := "Hello " nameresale ", I am currently processing your Purchase request " prresale "."

MailItem.Subject := "Resale form required for (" prresale ")"
MailItem.To    := emailresale
MailItem.Display
return

Gui Minimize
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot] and 119 guests