0

I have an email message with an image attachment that I want to save with a VBA macro. The file name and the display name show French accents in the attachment name (e.g. "Événement.jpg").

Saving the attachment with Outlook VBA works:

Dim fso As Object
Dim sFileName As String
Dim oAttachment As Outlook.attachment

set fso = CreateObject("Scripting.FileSystemObject")
' Edit the folder location accordingly:
sFileName = "C:\Users\YOUR_ACCOUNT_HERE\Desktop\" & oAttachment.getFileName
oAttachment.SaveAsFile sFileName

I can see the file correctly named on the file system.

Trying to access this file within VBA later on fails. The following code always returns FALSE:

' Returns False
MsgBox "File [" & sFileName & "] exists? " & sfo.fileexists(sFileName), vbInformation
Dim bFileExists as Boolean
If lenB (Dir(sFileName)  > 0 Then
    bFileExists = True
Else
    bFileExists = True
EndIf
' Also returns False
MsgBox "File [" & sFileName & "] exists? " & bFileExists, vbInformation

What am I doing wrong?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • `lenB (Dir(sFileName)` misses a `)` too. – Noldor130884 Jul 23 '14 at 11:54
  • You could rewrite your test as `If Dir(sFileName) <> ""`. Also, just for kicks, write a `Debug.Print sFileName` statement before the `If`, to be sure that you're having the same file. –  Jul 23 '14 at 11:55
  • I see that I made a few obvious errors in my sample code, thanks for pointing out. Is it okay to edit the original question? Irrespective of these typing errors, the problem still exists (although FileExists() returns True and Dir() returns an empty string). – ShutterFreak Jul 23 '14 at 13:37

1 Answers1

0

I eventually came upon a workaround, thanks to the MS-DOS "8.3" file naming legacy of Windows. Converting the file name to its short file name makes Dir() and Open() happy:

Dim sFileShortName As String
sFileShortName = fso.Getfile(sTempFileLocation).shortpath
bFileExists = (Dir(sFileShortName) <> "")       ' Now returns True at last!

Now fso.FileExists(sFileShortName) as well as bFileExists (based on Dir()) return True and Open sFileShortName For Binary Access Read As lFileNum works as well.

I hope that this will be beneficial to others.