PDA

View Full Version : renaming an attachment



dsymons
03-24-2010, 08:29 AM
I am creating a script in Outlook to strip attachments out of an email, and I have all the code working; I have a form that lets the user type in a new name for the file, however, when you click save it save the attachment with the new file name and the old file name—it looks like this: newfilesnameoldfilename.doc; however, I want to take of the old file name… but I cannot figure it out… here is my code:
I think I need to have a loop or something before saving the new file that tells outlook to remove all the characters from the left of the period… not sure how to do that… any thoughts or ideas?

Private Sub exitForm_Click()
Unload ResumeForm
End Sub

Private Sub fNameBox_Change()

End Sub

Private Sub Label1_Click()

End Sub

Private Sub lNameBox_Change()

End Sub

Private Sub Save_Click()

Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim myOrt As String
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim FindTerm(13)
Dim Extension As Long

'Set invalid characters to replace
FindTerm(0) = "*"
FindTerm(1) = "@"
FindTerm(2) = "\"
FindTerm(3) = "("
FindTerm(4) = ")"
FindTerm(5) = "["
FindTerm(6) = "]"
FindTerm(7) = "?"
FindTerm(8) = "<"
FindTerm(9) = ">"
FindTerm(10) = "!"
FindTerm(11) = "{"
FindTerm(12) = "}"
FindTerm(13) = ":"

'if null statements
'lNameBox
If IsNull(lNameBox) Then

Else
If lNameBox > "" Then
lNameBox = lNameBox + "_"

Else
MsgBox ("MUST ENTER A LAST NAME!")
Exit Sub
End If

End If

'fNameBox
If IsNull(fNameBox) Then

Else
If fNameBox > "" Then
fNameBox = fNameBox + "_"

Else
fNameBox = ""

End If

End If
' sourceBox
If IsNull(sourceBox) Then

Else
If sourceBox > "" Then
sourceBox = sourceBox + "_"

Else
sourceBox = ""

End If

End If
'typeBox
If IsNull(typeBox) Then

Else
If typeBox > "" Then
typeBox = typeBox + "_"

Else
typeBox = ""

End If

End If
' dateBox
If IsNull(dateBox) Then

Else
If dateBox > "" Then
dateBox = dateBox

Else
dateBox = Format(Date, "mmyy")

End If

End If

stringName = lNameBox + fNameBox + sourceBox + typeBox + dateBox

For i = 1 To 13
newstr = Replace(stringName, FindTerm(i), "")
stringName = newstr
Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' 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

' striping off the file extension
Extension = InStr(strFile, ".")
If Extension > 0 Then newString = Left$(strFile, k - 1)


' Combine with the path to the Temp folder.
strFile = "S:\chairofc\Resumes\" _
& stringName & " - " & strFile

' Save the attachment as a file.
objAttachments.item(i).SaveAsFile strFile

' Delete the attachment.
'objAttachments.item(i).Delete

'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If

Next i
End If

' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = objMsg.Body & vbCrLf & _
"The file(s) were saved to " & strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody & "<p>" & _
"The file(s) were saved to " & strDeletedFiles & "</p>"
End If
objMsg.Save
Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing


Unload ResumeForm

End Sub

Private Sub sourceBox_Change()

End Sub

Private Sub typeBox_Change()

End Sub

Private Sub UserForm_Click()

End Sub

dsymons
03-24-2010, 08:31 AM
I am creating a script in Outlook to strip attachments out of an email, and I have all the code working; I have a form that lets the user type in a new name for the file, however, when you click save it save the attachment with the new file name and the old file name—it looks like this: newfilesnameoldfilename.doc; however, I want to take of the old file name… but I cannot figure it out… here is my code:
I think I need to have a loop or something before saving the new file that tells outlook to remove all the characters from the left of the period… not sure how to do that… any thoughts or ideas?

Private Sub exitForm_Click()
Unload ResumeForm
End Sub

Private Sub fNameBox_Change()

End Sub

Private Sub Label1_Click()

End Sub

Private Sub lNameBox_Change()

End Sub

Private Sub Save_Click()

Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim i As Long
Dim myOrt As String
Dim lngCount As Long
Dim strFile As String
Dim strFolderpath As String
Dim strDeletedFiles As String
Dim FindTerm(13)
Dim Extension As Long

'Set invalid characters to replace
FindTerm(0) = "*"
FindTerm(1) = "@"
FindTerm(2) = "\"
FindTerm(3) = "("
FindTerm(4) = ")"
FindTerm(5) = "["
FindTerm(6) = "]"
FindTerm(7) = "?"
FindTerm(8) = "<"
FindTerm(9) = ">"
FindTerm(10) = "!"
FindTerm(11) = "{"
FindTerm(12) = "}"
FindTerm(13) = ":"

'if null statements
'lNameBox
If IsNull(lNameBox) Then

Else
If lNameBox > "" Then
lNameBox = lNameBox + "_"

Else
MsgBox ("MUST ENTER A LAST NAME!")
Exit Sub
End If

End If

'fNameBox
If IsNull(fNameBox) Then

Else
If fNameBox > "" Then
fNameBox = fNameBox + "_"

Else
fNameBox = ""

End If

End If
' sourceBox
If IsNull(sourceBox) Then

Else
If sourceBox > "" Then
sourceBox = sourceBox + "_"

Else
sourceBox = ""

End If

End If
'typeBox
If IsNull(typeBox) Then

Else
If typeBox > "" Then
typeBox = typeBox + "_"

Else
typeBox = ""

End If

End If
' dateBox
If IsNull(dateBox) Then

Else
If dateBox > "" Then
dateBox = dateBox

Else
dateBox = Format(Date, "mmyy")

End If

End If

stringName = lNameBox + fNameBox + sourceBox + typeBox + dateBox

For i = 1 To 13
newstr = Replace(stringName, FindTerm(i), "")
stringName = newstr
Next

' Instantiate an Outlook Application object.
Set objOL = CreateObject("Outlook.Application")

' Get the collection of selected objects.
Set objSelection = objOL.ActiveExplorer.Selection

' 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

' striping off the file extension
Extension = InStr(strFile, ".")
If Extension > 0 Then newString = Left$(strFile, k - 1)


' Combine with the path to the Temp folder.
strFile = "S:\chairofc\Resumes\" _
& stringName & " - " & strFile

' Save the attachment as a file.
objAttachments.item(i).SaveAsFile strFile

' Delete the attachment.
'objAttachments.item(i).Delete

'write the save as path to a string to add to the message
'check for html and use html tags in link
If objMsg.BodyFormat <> olFormatHTML Then
strDeletedFiles = strDeletedFiles & vbCrLf & "<file://" & strFile & ">"
Else
strDeletedFiles = strDeletedFiles & "<br>" & "<a href='file://" & _
strFile & "'>" & strFile & "</a>"
End If

Next i
End If

' Adds the filename string to the message body and save it
' Check for HTML body
If objMsg.BodyFormat <> olFormatHTML Then
objMsg.Body = objMsg.Body & vbCrLf & _
"The file(s) were saved to " & strDeletedFiles
Else
objMsg.HTMLBody = objMsg.HTMLBody & "<p>" & _
"The file(s) were saved to " & strDeletedFiles & "</p>"
End If
objMsg.Save
Next

ExitSub:

Set objAttachments = Nothing
Set objMsg = Nothing
Set objSelection = Nothing
Set objOL = Nothing


Unload ResumeForm

End Sub

Private Sub sourceBox_Change()

End Sub

Private Sub typeBox_Change()

End Sub

Private Sub UserForm_Click()

End Sub