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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strip Attachments from Outlook?

Status
Not open for further replies.

ICTECH

Technical User
Jun 5, 2002
131
0
0
CA
Hi All...

I have been working on a project that incorperates having to strip specific attachments from incomming E-Mails and save them to the Hard Drive. The code I created works.. But when I incorporate it into the main project that has Strict set to On.. But I get two error messages.. Setting Strict to Off isn't a current option..

1) Option Strict On disallows Late bindings.
2) Option Strict On disallows implicit Conversions from 'Outlook9.items' to 'Outlook9.ItemsClass'

Any help would be great.. Thanks..

Private Sub cleanup()
Dim myOlApp As Outlook.Application = New Outlook.Application()
Dim myOlExp As Outlook.Explorer
Dim myOLSel As Outlook.ItemsClass
Dim atmt As Object
Dim x As Integer
Dim lngCount As Long
Dim i As Long
Dim objAttach As Outlook.Attachments
Dim AttachmentsFolder As String
Dim strFileName As String
Dim atmtFileName As String
Dim msgAttachmentFiles As String

myOlExp = myOlApp.ActiveExplorer
myOLSel = myOlExp.CurrentFolder.Items

AttachmentsFolder = "C:\test\"

For x = 1 To myOLSel.Count
' Count the number of Attachments
lngCount = myOLSel.Item(x).attachments.count
' Handle the attachments
For Each atmt In myOLSel.Item(x).Attachments
' Get the attachment name and remove spaces, colons and underscores
If atmt.FileName.ToUpper Like "SPO*.PDF" Then

atmtFileName = atmt.FileName

' Save the attachment
atmt.SaveAsFile(AttachmentsFolder & atmtFileName)

End If

Next atmt

Next x

For x = 1 To myOLSel.Count
' Count the number of Attachments
lngCount = myOLSel.Item(x).attachments.count
' Handle the attachments
For Each atmt In myOLSel.Item(x).Attachments
If atmt.FileName.ToUpper Like "SPO*.PDF" Then
objAttach = myOLSel.Item(x).attachments
For i = lngCount To 1 Step -1
objAttach.Remove(i)
myOLSel.Item(x).Save()
lngCount = myOLSel.Item(x).attachments.count
Next
End If
Next
Next x

' Clear memory
myOlApp = Nothing
myOlExp = Nothing
myOLSel = Nothing
atmt = Nothing
objAttach = Nothing
End Sub
 
try creating a job that performs command line arguments then running the job in your code. This will probably get around the "Strict" problems, which I don't know a thing about.
 
Thanks WMthompson...

I'll try that.. Do you have an example by chance?

 
Careful using this. There are security implications. This tool is processing and moving a file that is emailed to an account dedicated to the SQL box. It utilizes SQL Mail, and, that means outlook has to be installed on the machine. SQL Mail must also be installed. Once you get SQL Mail functioning, you create a job that calls this stored procedure with a timer set to evey 10 minutes or so.


CREATE procedure usp_GetMyMail
as
declare @hmessage varchar(255)
declare @messagetext varchar(1000)
declare @message varchar(1000)
declare @message2 varchar(1000)
declare @skip_bytes int
declare @msg_length int
declare @M_attachments varchar(1000)
declare @cc_lists varchar(1000)
declare @msgSubject varchar(1000)
declare @formsql varchar(3000)
declare @unread2 varchar(20)
declare @start int
declare @len int
declare @sendmailto varchar(128)
declare @originator varchar(128)

exec master.dbo.xp_findnextmsg @unread_only='true',@msg_id=@hmessage OUTPUT

if @hmessage is not null
begin

/*set @skip_bytes=0
*/

exec xp_readmail
@msg_id=@hmessage,
@unread=@unread2 output,
@message=@messagetext OUTPUT,
/*@skip_bytes=@skip_bytes OUTPUT,*/
@msg_length=@msg_length OUTPUT,
@attachments=@M_attachments OUTPUT,
@subject=@msgSubject OUTPUT,
@cc_list=@cc_lists OUTPUT,
@originator_address=@originator output


print @messagetext
print @msgsubject
print 2

if @unread2 ='true'

if @msgSubject='Subject line identifier'
begin
set @formsql='move ' + rtrim(@M_attachments) + ' filename here make sure space follows the first tick'

set @formsql='master.dbo.xp_cmdshell ''' + @formsql+''''

print @formsql
exec (@formsql)

exec master.dbo.xp_sendmail @recipients=@originator,@subject='Unique Subject Line Here to sender'
exec msdb.dbo.sp_start_job JobName
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top