Shorten file name

  • Thread starter Thread starter hlock
  • Start date Start date
Status
Not open for further replies.
H

hlock

I have vba in Outlook 2007 that saves each attachment on an email and imports

it to our document repository system. However, I need to remove spaces in

the file name because if the file path string with embedded spaces is passed

to the import program, it thinks each space delimits a new file name. I need

to get the 8.3 format of the file path string. Here is what I have currently:

For Each objMsg In objSelection

' This code only strips attachments from mail items.

If objMsg.Class = olMail Then

' Get the Attachments collection of the item.

Set objAttachments = objMsg.Attachments

lngCount = objAttachments.Count

If lngCount > 0 Then

' We need to use a count down loop for

' removing items from a collection. Otherwise,

' the loop counter gets confused and only every

' other item is removed.

For i = lngCount To 1 Step -1

' Save attachment before deleting from item.

' Get the file name.

strfile = objAttachments.Item(i).FileName

' Combine with the path to the Temp folder.

strfile = strFolder & strfile

' Save the attachment as a file.

objAttachments.Item(i).SaveAsFile strfile

' Delete the attachment.

'objAttachments.Item(i).Delete

ExecCmd "ttimport.exe " & app & " " & strfile

Next i

End If

objMsg.Save

End If

Next

I know that I need to put something in here to shorten the file name, but

I'm not sure where and how. Any help is appreciated. Thanks!
 
Why not just strip the spaces with the Replace() function?

Sue Mosher

"hlock" <hlock> wrote in message

news:3C2CA6DD-04BA-4952-91B4-E9502E08A570@microsoft.com...
> I have vba in Outlook 2007 that saves each attachment on an email and
> imports
> it to our document repository system. However, I need to remove spaces in
> the file name because if the file path string with embedded spaces is
> passed
> to the import program, it thinks each space delimits a new file name. I
> need
> to get the 8.3 format of the file path string. Here is what I have
> currently:

> For Each objMsg In objSelection
> ' This code only strips attachments from mail items.
> If objMsg.Class = olMail Then
> ' Get the Attachments collection of the item.
> Set objAttachments = objMsg.Attachments
> lngCount = objAttachments.Count
> If lngCount > 0 Then
> ' We need to use a count down loop for
> ' removing items from a collection. Otherwise,
> ' the loop counter gets confused and only every
> ' other item is removed.
> For i = lngCount To 1 Step -1
> ' Save attachment before deleting from item.
> ' Get the file name.
> strfile = objAttachments.Item(i).FileName
> ' Combine with the path to the Temp folder.
> strfile = strFolder & strfile
> ' Save the attachment as a file.
> objAttachments.Item(i).SaveAsFile strfile
> ' Delete the attachment.
> 'objAttachments.Item(i).Delete

> ExecCmd "ttimport.exe " & app & " " & strfile
> Next i
> End If
> objMsg.Save
> End If
> Next

> I know that I need to put something in here to shorten the file name, but
> I'm not sure where and how. Any help is appreciated. Thanks!
 
Perfect!!! If I may... my current code works only for an email in explorer.

How do I modify it so that it will either work for the email selected in a

folder or an open email? I tried using what you gave me the other day, but

it's not working. Here is the entire code:

Public Sub StripAttachments()

Dim objOL As Outlook.Application

Dim objMsg As Object

Dim objAttachments As Outlook.Attachments

Dim objSelection As Outlook.Selection

Dim i As Long

Dim lngCount As Long

Dim strfile As String

Dim strFolder As String

Dim del As String ' ttimport delete parameter

Dim app As String ' ttimport application parameter

Dim result

On Error Resume Next

result = MsgBox("Do you want to remove attachments from selected

email(s)?", vbYesNo + vbQuestion)

If result = vbNo Then

Exit Sub

End If

' Instantiate an Outlook Application object.

Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.

Set objSelection = objOL.ActiveExplorer.Selection

' Get the Temp folder.

strFolder = GetTempDir()

If strFolder = "" Then

MsgBox "Could not get Temp folder", vbOKOnly

GoTo ExitSub

End If

app = "/a=clmdoc"

' Check each selected item for attachments.

' If attachments exist, save them to the Temp

' folder and strip them from the item.

For Each objMsg In objSelection

' This code only strips attachments from mail items.

If objMsg.Class = olMail Then

' Get the Attachments collection of the item.

Set objAttachments = objMsg.Attachments

lngCount = objAttachments.Count

If lngCount > 0 Then

' We need to use a count down loop for

' removing items from a collection. Otherwise,

' the loop counter gets confused and only every

' other item is removed.

For i = lngCount To 1 Step -1

' Save attachment before deleting from item.

' Get the file name.

strfile = objAttachments.item(i).FileName

strfile = Replace(strfile, " ", "_")

'Combine with the path to the Temp folder.

strfile = strFolder & strfile

' Save the attachment as a file.

objAttachments.item(i).SaveAsFile strfile

' Delete the attachment.

'objAttachments.Item(i).Delete

ExecCmd "ttimport.exe " & app & " " & strfile

Next i

End If

objMsg.Save

End If

Next

Call ImportAttMacro

ExitSub:

Set objAttachments = Nothing

Set objMsg = Nothing

Set objSelection = Nothing

Set objOL = Nothing

End Sub

You don't know how much we appreciate this community's help. There is a lot

of things that we wouldn't be able to do without it.

"Sue Mosher [MVP]" wrote:


> Why not just strip the spaces with the Replace() function?

> > Sue Mosher
> > >

> "hlock" <hlock> wrote in message
> news:3C2CA6DD-04BA-4952-91B4-E9502E08A570@microsoft.com...
> >I have vba in Outlook 2007 that saves each attachment on an email and
> >imports
> > it to our document repository system. However, I need to remove spaces in
> > the file name because if the file path string with embedded spaces is
> > passed
> > to the import program, it thinks each space delimits a new file name. I
> > need
> > to get the 8.3 format of the file path string. Here is what I have
> > currently:
> > For Each objMsg In objSelection
> > ' This code only strips attachments from mail items.
> > If objMsg.Class = olMail Then
> > ' Get the Attachments collection of the item.
> > Set objAttachments = objMsg.Attachments
> > lngCount = objAttachments.Count
> > If lngCount > 0 Then
> > ' We need to use a count down loop for
> > ' removing items from a collection. Otherwise,
> > ' the loop counter gets confused and only every
> > ' other item is removed.
> > For i = lngCount To 1 Step -1
> > ' Save attachment before deleting from item.
> > ' Get the file name.
> > strfile = objAttachments.Item(i).FileName
> > ' Combine with the path to the Temp folder.
> > strfile = strFolder & strfile
> > ' Save the attachment as a file.
> > objAttachments.Item(i).SaveAsFile strfile
> > ' Delete the attachment.
> > 'objAttachments.Item(i).Delete
> > ExecCmd "ttimport.exe " & app & " " & strfile
> > Next i
> > End If
> > objMsg.Save
> > End If
> > Next
> > I know that I need to put something in here to shorten the file name, but
> > I'm not sure where and how. Any help is appreciated. Thanks!


> .
>
 
See http://www.outlookcode.com/codedetail.aspx?id=50 for a function that

will return the currently open or selected item.

If something isn't working, you need to tell us exactly what the symptoms

are.

Sue Mosher

"hlock" <hlock> wrote in message

news:19EC95F4-6D03-4D84-98F4-179551AC2B3E@microsoft.com...
> Perfect!!! If I may... my current code works only for an email in
> explorer.
> How do I modify it so that it will either work for the email selected in a
> folder or an open email? I tried using what you gave me the other day,
> but
> it's not working. Here is the entire code:

> Public Sub StripAttachments()
> Dim objOL As Outlook.Application
> Dim objMsg As Object
> Dim objAttachments As Outlook.Attachments
> Dim objSelection As Outlook.Selection
> Dim i As Long
> Dim lngCount As Long
> Dim strfile As String
> Dim strFolder As String
> Dim del As String ' ttimport delete parameter
> Dim app As String ' ttimport application parameter
> Dim result

> On Error Resume Next

> result = MsgBox("Do you want to remove attachments from selected
> email(s)?", vbYesNo + vbQuestion)
> If result = vbNo Then
> Exit Sub
> End If

> ' Instantiate an Outlook Application object.
> Set objOL = CreateObject("Outlook.Application")
> ' Get the collection of selected objects.
> Set objSelection = objOL.ActiveExplorer.Selection

> ' Get the Temp folder.
> strFolder = GetTempDir()
> If strFolder = "" Then
> MsgBox "Could not get Temp folder", vbOKOnly
> GoTo ExitSub
> End If

> app = "/a=clmdoc"

> ' Check each selected item for attachments.
> ' If attachments exist, save them to the Temp
> ' folder and strip them from the item.
> For Each objMsg In objSelection
> ' This code only strips attachments from mail items.
> If objMsg.Class = olMail Then
> ' Get the Attachments collection of the item.
> Set objAttachments = objMsg.Attachments
> lngCount = objAttachments.Count
> If lngCount > 0 Then
> ' We need to use a count down loop for
> ' removing items from a collection. Otherwise,
> ' the loop counter gets confused and only every
> ' other item is removed.
> For i = lngCount To 1 Step -1
> ' Save attachment before deleting from item.
> ' Get the file name.
> strfile = objAttachments.item(i).FileName
> strfile = Replace(strfile, " ", "_")
> 'Combine with the path to the Temp folder.
> strfile = strFolder & strfile
> ' Save the attachment as a file.
> objAttachments.item(i).SaveAsFile strfile
> ' Delete the attachment.
> 'objAttachments.Item(i).Delete

> ExecCmd "ttimport.exe " & app & " " & strfile
> Next i
> End If
> objMsg.Save
> End If
> Next
> Call ImportAttMacro

> ExitSub:
> Set objAttachments = Nothing
> Set objMsg = Nothing
> Set objSelection = Nothing
> Set objOL = Nothing
> End Sub




> "Sue Mosher [MVP]" wrote:
>
> > Why not just strip the spaces with the Replace() function?



>

>> "hlock" <hlock> wrote in message
> > news:3C2CA6DD-04BA-4952-91B4-E9502E08A570@microsoft.com...
> > >I have vba in Outlook 2007 that saves each attachment on an email and
> > >imports
> > > it to our document repository system. However, I need to remove spaces
> > > in
> > > the file name because if the file path string with embedded spaces is
> > > passed
> > > to the import program, it thinks each space delimits a new file name.
> > > I
> > > need
> > > to get the 8.3 format of the file path string.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
O How will a new Exchange profile handle existing .ost file stored elsewhere? Using Outlook 4
P Windows 11/Office 365 - can't forward calendar entry as ics file Using Outlook 3
A How to open Excel file saved in Outlook folder? Outlook VBA and Custom Forms 4
R File not found when sending email with attached pdf file Using Outlook 2
Victor_50 Outlook 365 Extract all mail addresses and names from a pst file to csv. Using Outlook 3
M Why oft-file opens as outbox not as draft Outlook VBA and Custom Forms 4
E "Cannot display the folder. MS Outlook cannot access the specified file location" Using Outlook 8
M Outlook 365 macro - automatically attach file based on subject line Outlook VBA and Custom Forms 0
S Import pst file into 365 desktop and have it sync one Using Outlook.com accounts in Outlook 2
T Cannot open .pst file Using Outlook 2
D DB/Object/Record vs File/List/Range Terminology Outlook VBA and Custom Forms 0
Rob Can't save MailItem because the message changed in .pst file Outlook VBA and Custom Forms 0
C Advanced search terms for "Outlook Data File" Using Outlook 1
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 0
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
E Save Selected Email Message as .msg File - digitally sign email doesn't works Outlook VBA and Custom Forms 1
W Outlook 365 File access denied attempting to import .pst Using Outlook 6
P Copying ALL calendar entries from Calender in PST file to IMAP OST file? Using Outlook 1
R Saving Emails and Attachments as .msg file Using Outlook 3
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
W Create a Quick Step or VBA to SAVE AS PDF in G:|Data|Client File Outlook VBA and Custom Forms 1
Wotme create email only data file Using Outlook 1
C "The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file location" Using Outlook 1
L Restoring Outlook from backup pst file Using Outlook 5
W Transfer Outlook 2016 autocomplete file to Outlook 2007 Using Outlook 1
V Backup Calendar, Contacts, Tasks in an POP3 data file Using Outlook 3
DoctorJellybean Use OST file location for fresh installation? Using Outlook 1
HarvMan Exporting IMAP OST file to PST Using Outlook 5
P File Picker for attachment Outlook VBA and Custom Forms 0
D Forwarding email based on the attachment file type and specific text found on the attachment file name Outlook VBA and Custom Forms 1
N File Picker for attachment Outlook VBA and Custom Forms 2
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
L Article on merging pst file Using Outlook 1
J Deliver new messages to New or Existing Data File? Using Outlook 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
T vba extract data from msg file as attachment file of mail message Outlook VBA and Custom Forms 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
O Comma Separated Values.ADR and A file error has occurred in the translator Using Outlook 6
R Problem moving file “Email folders.pst” to new PC Using Outlook 5
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
V Pin a document to the "Attach file" Recent Documents list Using Outlook 0
D Outlook 2016 Unable to load Outlook data pst file Using Outlook 5
K Need to convert .mmf file to .pst format Outlook VBA and Custom Forms 7
S save attachment with date & time mentioned inside the file Outlook VBA and Custom Forms 0
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
B Can I create a local PST file for SPAM on a drive that is usually disconnected? Using Outlook 3
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
P Outlook pst file is too huge with POP3. How to save more space? Using Outlook 4
G Cannot Move Autocomplete File to New Computer Using Outlook 15
B Outlook 2013 erratically deleting original file that is attached Using Outlook 0

Similar threads

Back
Top