Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if the e-mail has no attachment it throws an error

Status
Not open for further replies.

mikeisvfp

Programmer
Mar 5, 2011
91
CA
Hello Experts,

Ive Created a small program that scans through my email and automatically converts word docs to pdf and sends email back to sender. However, if there are no emails with subject = Convert2PDF found it results in error and crashes my application.

Ive googled around and came accross a similar situation, but i get a nesting error
URL:
Code:
local nFolder
nFolder = 6  &&inbox folder
lbOpenOutlook  = .f.

on error lbOpenOutlook =.t.
oOutLook = getobject(,"Outlook.Application")  &&if outlook is not open this will cause an error and thus set lbOpenOutlook to true
on error

if lbOpenOutlook  && if true Outlook needs to be opened
	oOutLook=createobject("Outlook.Application")
endif	

onamespace = oOutLook.getnamespace("MAPI")
oinbox = onamespace.getdefaultfolder(nFolder)
loitems = oinbox.items

for each loitem in loitems
	if loitem.subject = 'Convert2PDF'
		loitem.subject = 'Convert2PDF'
		loitem.sender
		?loitem.sender.address
		lcSenderAddress = loitem.sender.address
		lnNumberofAttachments=loitem.attachments.count
		for nAttachmentLoop = 1 to lnNumberofAttachments
			lcAttachmentname= "c:\"+loitem.attachments.item(nAttachmentLoop).filename
			loitem.attachments.item(nAttachmentLoop).SaveAsFile (lcAttachmentname)
			loitem.delete
		NEXT
	ENDIF
NEXT

Any help is much appreciated
 
Where does the error occur? What error?

Have you tried stepping through the code in the Debugger?

Tamar
 
I tested this with my mail (and I have no mail with subject 'Convert2PDF'), and there is no error happening. I alo never get into the inner code within IF loitem.subject = 'Convert2PDF' ... ENDIF.

I saw your question in MSDN and PAvel gave you part of this code, but he suggested inspecting values at the commandline, using intellisense to investigate more. To discover something on your own. He didn't wrote loitem.sender, he wrote ? loitem.sender.

And these lines printing something on screen via ? are not needed, they are just there to display values, so you can learn, what this is.

You also have a totally unneccessary line. Do you see it yourself?
Code:
if loitem.subject = 'Convert2PDF'
		loitem.subject = 'Convert2PDF'

Well, if some property named "subject" has some value 'Convert2PDF', then it has that value. There is no need to set it again, you could also do IF A=1 A=1. Next line i loItem.sender, as I already said it's missing a "?" in front or you can delete it, like you can delete the assignment loitem.subject = 'Convert2PDF'. That code line (loitem.sender) would cause an error. Why? It's just a property name. A variable name, object name or property name on it's own, is no complete command. You have to do something with it.

It's time for holidays, perhaps. At least the weekend is near. I don't want to discourage you, but the rest of the code also isn't converting to PDF, you just save attachments and delete the mail from the inbox. You have the lcSenderAddress, but you don't do anything with it, you don't relate it in any way to the saved attachment files, so even if you later convert to pdf and send the converted file back, which file would go for which sender? You CAN do better, you already proved you can.

Bye, Olaf.

 
Hi Olaf,

I just copied an pasted a portion of my code, not everything, so yes you wouldnt find any code that converts to pdf
the portion which i copied and pasted scans for subject that is equal to "Convert2PDF"
My question was how to trap it if no results are found in subject = "Conver2PDF"
 
I modified it, I noticed uneccarry code, sorry about that

Code:
local nFolder
nFolder = 6  &&inbox folder
lbOpenOutlook  = .f.

on error lbOpenOutlook =.t.
oOutLook = getobject(,"Outlook.Application")  &&if outlook is not open this will cause an error and thus set lbOpenOutlook to true
on error

if lbOpenOutlook  && if true Outlook needs to be opened
	oOutLook=createobject("Outlook.Application")
endif	

onamespace = oOutLook.getnamespace("MAPI")
oinbox = onamespace.getdefaultfolder(nFolder)
loitems = oinbox.items

for each loitem in loitems
	if loitem.subject = 'Convert2PDF'
		lcSenderAddress = loitem.sender.address
		lnNumberofAttachments=loitem.attachments.count
		for nAttachmentLoop = 1 to lnNumberofAttachments
			lcAttachmentname= "c:\"+loitem.attachments.item(nAttachmentLoop).filename
			loitem.attachments.item(nAttachmentLoop).SaveAsFile (lcAttachmentname)
			loitem.delete
		NEXT
	ENDIF
NEXT
 
My question was how to trap it if no results are found in subject = "Conver2PDF"

If I've understood you right, why not just increment a counter each time you find an item with that subject? Then, at the end of the loop, check to see if that's zero. (If I've misunderstood, perhaps you could clarify the question.)

By the way, I don't much like your loitem.delete in the inner loop. It seems to me that you are looping on the items within the inbox, but then deleting that same item before the loop has terminated. And you're doing that when you find the first attachment. What if there are more attachments to the item you have just deleted?

Mike





__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
You said "if there are no emails with subject = Convert2PDF found it results in error and crashes my application" now you say "My question was how to trap it if no results are found in subject Conver2PDF".
Mike already answered that, simply count. init a counter at 0, increment it within the IF statement. If you have no such mail, your counter will still be 0, won't it?
If you still also get an error, it's probably not what you think, do you have an ON ERROR routine showing a line number of the error? If not, you should at least do ON ERROR ? LINENO() to see where it happens, it might simply be, that loitems = oinbox.items results in loitems being NULL, if there is no mail in the inbox at all, which of course would result in an error in the line for each loitem in loitems, as you can't do for each ... in .NULL., for each ... in needs a collection object or array.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top