Karon
(Getting the hang of it)
2016-04-21 08:08 PM
How do you attach a file to the email?

OK - I am back!!
My next problem is getting an attachment on the email. The email sends, but no attachment.
What am I missing? Outlook is 2010.
 Code:

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$Attachments = "C:\Cognos\Data\reportbyday.pdf"
			$MailItem.Send
			$Root = 0


LonkeroAdministrator
(KiX Master Guru)
2016-04-22 04:26 AM
Re: How do you attach a file to the email?

You left another line out of your original...

Mart
(KiX Supporter)
2016-04-22 09:10 AM
Re: How do you attach a file to the email?

You need to tell Outlook to use the attachment. At the moment you are just putting a path and filename into a variable. Sure that works fine but Outlook does not know it should attach the file to the e-mail. You need to add it to $mailitem like shown below.

 Code:
....
$MailItem.Attachments.Add("c:\somefolder\somefile.txt")
....


Mart
(KiX Supporter)
2016-04-22 09:21 AM
Re: How do you attach a file to the email?

Oh and since a few months I'm trying to avoid sending anything with Outlook from scripts because it is VERRY frustrating (major understatement but let’s stay away from the bad words) that sometimes it works and sometimes without any changes it does not work. I like my health better then anything else so this has frustrated me enough and I'm moving all our scripts that send out e-mails over to BLAT and an internal SMTP relay. If you like Outlook that’s fine but I’m just throwing in an alternative. Outlook is nice but not for scripting IMHO.

Glenn BarnasAdministrator
(KiX Supporter)
2016-04-22 02:22 PM
Re: How do you attach a file to the email?

BTW - there's a full BLAT implementation in the script vault. \:\)

Karon
(Getting the hang of it)
2016-04-22 02:49 PM
Re: How do you attach a file to the email?

Lonkero - I checked and this is what I have.... If I am missing something, please let me know what it is! Thanks ALL!!!

Karon
(Getting the hang of it)
2016-04-22 02:56 PM
Re: How do you attach a file to the email?

I just ran it with the following:
 Code:

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$MailItem.Attachment.Add("C:\Cognos\Data\reportbyday.pdf")
			$MailItem.Send
			$Root = 0


Still no attachment....


Karon
(Getting the hang of it)
2016-04-22 03:01 PM
Re: How do you attach a file to the email?

BAAA HAAA! helps if it says "Attachments" instead of "Attachment" - added the s and runs like a champ now!! OMG I think I may be well on my way to getting that 2003 server GONE!!
Great start to my day!!


Mart
(KiX Supporter)
2016-04-22 03:36 PM
Re: How do you attach a file to the email?

 Originally Posted By: Karon
BAAA HAAA! helps if it says "Attachments" instead of "Attachment" - added the s and runs like a champ now!! OMG I think I may be well on my way to getting that 2003 server GONE!!
Great start to my day!!


LOL we all had that at some point. It are always the small things that get overlooked


Karon
(Getting the hang of it)
2016-04-22 03:46 PM
Re: How do you attach a file to the email?

Sometimes I just need someone to be an extra set of eyeballs!! LOL

Karon
(Getting the hang of it)
2016-04-22 04:28 PM
Re: How do you attach a file to the email?

So on a Windows 7 32 bit PC running Outlook 2010, the following email script works:

 Code:
; verify the files to e-mail exists
If Exist ("C:\Cognos\Data\reportbyday.pdf")

;main program loop

$rc = SetOption("NoMacrosInStrings","On")
$rc = SetOption("NoVarsInStrings","On")

; outlook variables

	$Root = CreateObject("Outlook.Application")
	$Session = $Root.GetNamespace("MAPI")
	$DefaultProfile = Outlook
	$Session.Logon($DefaultProfile)	
	$Folder = $Session.GetDefaultFolder(olFolderInbox)
	$Folder.Display

; launch outlook and add email information

			$MailItem = $Root.CreateItem(0)
? "Root Creation: "+@Error
			$MailItem.Importance = 1
			$MailItem.Subject = "Daily Report -- DO NOT REPLY!!!!"
			$MailItem.Sender = "sender@someplace.org"
			$MailItem.To = "receiver@someplace.org"
			$MailItem.Body = "Daily Report Attached!!!"
			$MailItem.Attachments.Add("C:\Cognos\Data\reportbyday.pdf")
			$MailItem.Send
			$Root = 0
endif


Thanks to everyone for all your assistance!!

IF there are checks and balances that anyone thinks I need, just let me know!