A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xlsAttachment, & Mailing



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old May 10th 08, 06:47 AM posted to microsoft.public.outlook.program_vba
JingleRock
external usenet poster
 
Posts: 8
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xlsAttachment, & Mailing

The Subject title succintly describes what I am doing.
I am using an OL Rule to grab a Mail Item that satisfies Rule; I am
then saving the Attachment so that I can open it and start "massaging"
the data w/ my XCL VBA Code. After massaging, I save the
modified .xls file so that I can attach it to a forwarded Mail Item.

The Code below does this:

Public Sub BIG_TICKETS(RuleSelectedMI As MailItem)

On Error GoTo PROBLEM_ERROR

Dim strID As String
Dim myPathTemp As String
Dim NewFilePathName As String

'Declare variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Dim olNs As Outlook.NameSpace
Dim Fldr As MAPIFolder
Dim olAtt As Attachment
Dim olMi As Outlook.MailItem
Dim MyForward As Outlook.MailItem

'************************* PATH NAME
************************************
'ORIGINAL ATTACHMENT SAVED (SO IT CAN BE OPENED) HERE
'ALSO, MODIFIED ATTACHMENT SAVED (SO IT CAN BE ATTACHED TO
MailItem) HERE
myPathTemp = "C:\Documents and Settings\userID\Local Settings\Temp
\"

strID = RuleSelectedMI.EntryID

'Set variables
Set xlApp = CreateObject("Excel.Application")
Set olNs = Application.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olMi = olNs.GetItemFromID(strID)

Set olAtt = olMi.Attachments(1)

'SAVE ORIGINAL ATTACHMENT IN THE SPECIFIED FOLDER USING
SAME FILENAME
olAtt.SaveAsFile (myPathTemp & olAtt.FileName)

Set xlBook = xlApp.Workbooks.Open(myPathTemp &
olAtt.FileName)
xlApp.Visible = True
Set xlSheet = xlBook.Sheets(1) ' IS THIS NEEDED?

'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+
'INSERT EXCEL VBA CODE THAT WILL "MASSAGE" DATA IN ORIGINAL ATTACHMENT
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+
'END OF EXCEL VBA CODE THAT "MASSAGED" DATA IN ORIGINAL ATTACHMENT
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

'SAVE ATTACHMENT (NOW, A MODIFIED FILE) IN THE SPECIFIED FOLDER
USING NEW FILENAME;
'THEN CLOSE WB
NewFilePathName = myPathTemp & "SUMMARY.xlS"

xlApp.ActiveWorkbook.SaveAs NewFilePathName
xlApp.ActiveWorkbook.Close

'DELETE ORIGINAL (UNMODIFIED) ATTACHMENT FILE
'Kill (myPathTemp & olAtt.FileName) '==== DO NOT KILL
DURING CODE TEST

olMi.Attachments.Remove 1
olMi.Attachments.Add NewFilePathName

olMi.Recipients.Remove 1

Set MyForward = olMi.Forward
MyForward.Recipients.Add "THE WORLD"

If MyForward.Recipients.ResolveAll Then

MyForward.Subject = "Weekly Wholesaler Report:
SUMMARY"
MyForward.Body = ""
MyForward.Send
olMi.Delete

Else
MsgBox "PROB w/ Address Book Name"
End If

'DELETE MODIFIED ATTACHMENT FILE
'Kill NewFilePathName '============ DO NOT
KILL DURING CODE TEST

BIG_TICKETS_EXIT:
Set MyForward = Nothing
Set olMi = Nothing
Set olAtt = Nothing
Set Fldr = Nothing
Set olNs = Nothing

Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing

Set RuleSelectedMI = Nothing

Exit Sub

PROBLEM_ERROR:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: BIG_TICKETS" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description, vbCritical,
"Error AGAIN!"
Resume BIG_TICKETS_EXIT

End Sub
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

The above Code satisfies all of the objectives stated above, except:
about 30 seconds after the Code finishes processing, including
shutting down XCL, the second saved file (the Modified Attachment)
pops open; also, I get Error #1004 and Err.Description of "Method
'Range' of object'_Global' failed".

When I step thru the Code, there is no .xls file pop-up and there is
no error message.

I would like to have the Code delete the two saved files, but I am not
there yet.

Please help.
  #2  
Old May 12th 08, 03:08 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xls Attachment, & Mailing

I have no idea which lines are causing the problem but if stepping the code
fixes it then throw in one or more DoEvents calls at that point and see if
that helps.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"JingleRock" wrote in message
...
The Subject title succintly describes what I am doing.
I am using an OL Rule to grab a Mail Item that satisfies Rule; I am
then saving the Attachment so that I can open it and start "massaging"
the data w/ my XCL VBA Code. After massaging, I save the
modified .xls file so that I can attach it to a forwarded Mail Item.

The Code below does this:

Public Sub BIG_TICKETS(RuleSelectedMI As MailItem)

On Error GoTo PROBLEM_ERROR

Dim strID As String
Dim myPathTemp As String
Dim NewFilePathName As String

'Declare variables
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Dim olNs As Outlook.NameSpace
Dim Fldr As MAPIFolder
Dim olAtt As Attachment
Dim olMi As Outlook.MailItem
Dim MyForward As Outlook.MailItem

'************************* PATH NAME
************************************
'ORIGINAL ATTACHMENT SAVED (SO IT CAN BE OPENED) HERE
'ALSO, MODIFIED ATTACHMENT SAVED (SO IT CAN BE ATTACHED TO
MailItem) HERE
myPathTemp = "C:\Documents and Settings\userID\Local Settings\Temp
\"

strID = RuleSelectedMI.EntryID

'Set variables
Set xlApp = CreateObject("Excel.Application")
Set olNs = Application.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set olMi = olNs.GetItemFromID(strID)

Set olAtt = olMi.Attachments(1)

'SAVE ORIGINAL ATTACHMENT IN THE SPECIFIED FOLDER USING
SAME FILENAME
olAtt.SaveAsFile (myPathTemp & olAtt.FileName)

Set xlBook = xlApp.Workbooks.Open(myPathTemp &
olAtt.FileName)
xlApp.Visible = True
Set xlSheet = xlBook.Sheets(1) ' IS THIS NEEDED?

'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+
'INSERT EXCEL VBA CODE THAT WILL "MASSAGE" DATA IN ORIGINAL ATTACHMENT
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+
'END OF EXCEL VBA CODE THAT "MASSAGED" DATA IN ORIGINAL ATTACHMENT
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

'SAVE ATTACHMENT (NOW, A MODIFIED FILE) IN THE SPECIFIED FOLDER
USING NEW FILENAME;
'THEN CLOSE WB
NewFilePathName = myPathTemp & "SUMMARY.xlS"

xlApp.ActiveWorkbook.SaveAs NewFilePathName
xlApp.ActiveWorkbook.Close

'DELETE ORIGINAL (UNMODIFIED) ATTACHMENT FILE
'Kill (myPathTemp & olAtt.FileName) '==== DO NOT KILL
DURING CODE TEST

olMi.Attachments.Remove 1
olMi.Attachments.Add NewFilePathName

olMi.Recipients.Remove 1

Set MyForward = olMi.Forward
MyForward.Recipients.Add "THE WORLD"

If MyForward.Recipients.ResolveAll Then

MyForward.Subject = "Weekly Wholesaler Report:
SUMMARY"
MyForward.Body = ""
MyForward.Send
olMi.Delete

Else
MsgBox "PROB w/ Address Book Name"
End If

'DELETE MODIFIED ATTACHMENT FILE
'Kill NewFilePathName '============ DO NOT
KILL DURING CODE TEST

BIG_TICKETS_EXIT:
Set MyForward = Nothing
Set olMi = Nothing
Set olAtt = Nothing
Set Fldr = Nothing
Set olNs = Nothing

Set xlSheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing

Set RuleSelectedMI = Nothing

Exit Sub

PROBLEM_ERROR:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: BIG_TICKETS" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description, vbCritical,
"Error AGAIN!"
Resume BIG_TICKETS_EXIT

End Sub
'+++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
+

The above Code satisfies all of the objectives stated above, except:
about 30 seconds after the Code finishes processing, including
shutting down XCL, the second saved file (the Modified Attachment)
pops open; also, I get Error #1004 and Err.Description of "Method
'Range' of object'_Global' failed".

When I step thru the Code, there is no .xls file pop-up and there is
no error message.

I would like to have the Code delete the two saved files, but I am not
there yet.

Please help.


  #3  
Old May 12th 08, 08:02 PM posted to microsoft.public.outlook.program_vba
JingleRock
external usenet poster
 
Posts: 8
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xlsAttachment, & Mailing

Thanks for your post, Ken.
I have a partial solution.
The problem has to do with creating two instances of Excel. Remember,
I am grabbing a mail item, saving the Excel attachment to my local
hard drive using the same filename, opening it and using Excel VBA
code to modify it, then saving the modified file, using a new name, to
the same folder used in saving the original attachment.
By keeping Task Manager open, I am able to monitor the Excel.exe
process while the macro runs.
I inserted an extra 'xlApp.Quit' in my Code (therefore, a total of
two) (and I am still using the stmt 'xlApp.ActiveWorkbook.Close' in my
Code). The result is that I no longer get an error message and I no
longer get the pop-up modified file spreadsheet. Also, I am able to
have my Code 'Kill" each of the two saved files. The only drawback is
that Excel.exe remains open in the background until I close Outlook,
and then Excel.exe disappears from Task Manager immediately.
Any thoughts?
  #4  
Old May 12th 08, 09:32 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xls Attachment, & Mailing

After you quit Excel are you releasing all references to it by setting them
to Nothing?

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"JingleRock" wrote in message
...
Thanks for your post, Ken.
I have a partial solution.
The problem has to do with creating two instances of Excel. Remember,
I am grabbing a mail item, saving the Excel attachment to my local
hard drive using the same filename, opening it and using Excel VBA
code to modify it, then saving the modified file, using a new name, to
the same folder used in saving the original attachment.
By keeping Task Manager open, I am able to monitor the Excel.exe
process while the macro runs.
I inserted an extra 'xlApp.Quit' in my Code (therefore, a total of
two) (and I am still using the stmt 'xlApp.ActiveWorkbook.Close' in my
Code). The result is that I no longer get an error message and I no
longer get the pop-up modified file spreadsheet. Also, I am able to
have my Code 'Kill" each of the two saved files. The only drawback is
that Excel.exe remains open in the background until I close Outlook,
and then Excel.exe disappears from Task Manager immediately.
Any thoughts?


  #5  
Old May 12th 08, 10:37 PM posted to microsoft.public.outlook.program_vba
JingleRock
external usenet poster
 
Posts: 8
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xlsAttachment, & Mailing

Yes, see all 'Nothing's above.
By the way, I deleted the 3 stmts involving 'xlSheet'.
  #6  
Old May 12th 08, 10:53 PM posted to microsoft.public.outlook.program_vba
Ken Slovak - [MVP - Outlook]
external usenet poster
 
Posts: 5,848
Default Grabbing OL Mail Attachment, Using XCL VBA Code, Replacing .xls Attachment, & Mailing

Well, something's holding it open and that's usually the cause.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"JingleRock" wrote in message
...
Yes, see all 'Nothing's above.
By the way, I deleted the 3 stmts involving 'xlSheet'.


 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compress email by compressing/replacing attachment [email protected] Outlook Express 3 December 4th 07 04:32 PM
How can I find the get ZIP+4 code for Outlook mailing addresses WDR Outlook - Using Contacts 0 December 15th 06 07:06 AM
Removing headers from email, replacing rules with code Tony Gravagno Add-ins for Outlook 2 November 14th 06 11:34 PM
Create a Snail Mail Mailing List to Be used with Mail Merge rllngriver Outlook - Using Contacts 7 October 4th 06 12:09 AM
Outlook not displaying attachment from Access via code [email protected] Outlook - General Queries 4 February 28th 06 12:30 AM


All times are GMT +1. The time now is 09:28 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.