0

I made a script to save attachments automatically and print them.

Sub SaveAttachment(Item As MailItem)
    If Item.Class = olMail Then
        If Item.Attachments.Count > 0 Then
            Dim objAtt As Outlook.Attachments
            Set objAtt = Item.Attachments
            For Each objAttach In objAtt
                objAttach.SaveAsFile "C:\PDFInvoices\" & _
                        Item.Subject & "_" & objAttach.FileName '
            Next
            Set objAtt = Nothing
        End If
    End If
End Sub

An attachment containing special characters such as # or & makes the script crash.

I want a way to replace, the special characters by something else.

Community
  • 1
  • 1
No 0ne
  • 61
  • 10
  • It's not VBA doing it. You simply cannot create a file with [certain characters](https://stackoverflow.com/q/1976007/11683) in its name. It is a file system restriction. – GSerg Dec 03 '18 at 09:49
  • Thanks for the answer, but I know that. I want a way to replace, the special characters by something else w.e – No 0ne Dec 03 '18 at 10:02
  • In your question you said you don't want to rename your attachments, and asked for a way to ignore special characters. If you are happy to replace them after all, then... – GSerg Dec 03 '18 at 10:06
  • Possible duplicate of [Replace invalid characters when saving excel as PDF](https://stackoverflow.com/q/45458954/11683) – GSerg Dec 03 '18 at 10:06
  • @GSerg If I can ignore or replace the special Characters, if I can replace just the special characters and not the entire String I'm happy with it. Sorry for the miss understanding – No 0ne Dec 03 '18 at 10:13

2 Answers2

2

I have recently constructed a function which removes all vowels from a string.. Perhaps this suits you

Function REMOVEVOWELS(Txt) As String
'Removes all vowels from the Txt argument
Vowels = Array("A", "E", "I", "O", "U") 'Replace vowels with special chars

For Each a In Vowels
    Txt = Replace(Txt, a, "")
Next a
REMOVEVOWELS = Txt
 End Function

Then you could try setting the file name in your Sub

FileNameNoSpecChars = REMOVEVOWELS(objAttach.FileName)

Next, save the file with the new variable

        For Each objAttach In objAtt
            objAttach.SaveAsFile "C:\PDFInvoices\" & _
                    Item.Subject & "_" & FileNameNoSpecChars  '
        Next

Hope this helps.

Tim Stack
  • 3,209
  • 3
  • 18
  • 39
  • Thanks, but seems that I can't call the function on (Obj.attach and item.subject) :/ – No 0ne Dec 03 '18 at 10:49
  • I'm not familiar with working with Outlook in VBA. Does objAttach.FileName not return a string? Have you tried writing the filename to a variable first and using that variable in the function? – Tim Stack Dec 03 '18 at 10:52
  • Neither do I, been 40 mins that I'm trying to play with your function, Yeah i've tried. It does work with a simple String, but I can't call the function on ('item.subject') – No 0ne Dec 03 '18 at 10:59
  • objAtt.FileName should, according to MS Docs, return a string, so I am unsure why the function won't work on that. The same counts for item.Subject – Tim Stack Dec 03 '18 at 11:06
1

I was able to fix the problem using the following code :

Public Sub saveAttachtoDiskRule(itm As Outlook.MailItem)
Dim strSubject As String, strExt As String
Dim objAtt As Outlook.Attachment
Dim saveFolder As String

Dim enviro As String
enviro = CStr(Environ("ngallouj"))
saveFolder = enviro & "C:\PDFInvoices\"

For Each objAtt In itm.Attachments
DateFormat = Format(Date, "yyyy-mm-dd ")

file = saveFolder & DateFormat & objAtt.DisplayName
 objAtt.SaveAsFile file
 Next

 Set objAtt = Nothing
 End Sub
No 0ne
  • 61
  • 10
  • That part with `Environ` does not make sense (and only works because it returns an empty string), otherwise you have solved nothing, because `DisplayName` still may contain invalid characters. – GSerg Dec 03 '18 at 11:42
  • Since the Display name is converted to a String. I'm able to save, the files even if it contains special Character, which was the problem, That's just a piece you don't have the rest. Since it's save correctly, I'm able to remove the Special Char throw my Batch Script. – No 0ne Dec 03 '18 at 12:30