VBA in Outlook.

TFCJamieFay

Active Member
Joined
Oct 3, 2007
Messages
480
Hi all,

I have a marco in MS Outlook that has a variable called "Atmt". This the filename of an email attachment (ie. "MyWorkbook.xls"). How do I extract all the letters up to the ".XLS" bearing in mind the filename length will vary? Also, sometimes the file may be a ".CSV" file, so I need to remove this as well.

Many thanks guys,

Jay
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

TFCJamieFay

Active Member
Joined
Oct 3, 2007
Messages
480
Thanks for your quick response Richard, but I keep getting an error message:

Run Time Error "5" - Invalid procedure call or argument.

This is a section of the code:

If SubFolder.Items.Count > 0 Then
For Each Item In SubFolder.Items
For Each Atmt In Item.Attachments
If Right(Atmt.FileName, 3) = "xls" Or Right(Atmt.FileName, 3) = "csv" Then
'fName = Left$(Atmt, InStrRev(".", Atmt) - 1)
FileName = "C:\Email Attachments\" & fName & Item.EntryID & _
Format(Item.CreationTime, "yyyymmdd_hhnnss_") & Atmt.FileName
Atmt.SaveAsFile FileName
MsgBox Left$(Atmt, InStrRev(".", Atmt) - 1)
i = i + 1
End If
Next Atmt
Next Item
End If

It errors out on the MsgBox line. I inserted it there just to see what it was returning. What I want to do is assigned the new shorter filename to the variable called "fName" and then use this as the first item when creating the FileName.

Thanks again.
 
Upvote 0

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
You need to access the filename property of the Atmt object:

Code:
Dim strFileName As String
 
strFileName = Atmt.FileName
 
'then process strFileName:
 
MsgBox Left(strFileName,InstrRev(strFileName,".")-1)

However, you have already confirmed that the filename contains xls or csv - you if you already know this, you just need to remove the last 4 characters of the filename.
 
Upvote 0

Forum statistics

Threads
1,190,657
Messages
5,982,137
Members
439,757
Latest member
85Sarah2005

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top