Dejan Dozet
Dejan Dozet

Reputation: 1009

Outlook.AppointmentItem change attachment file path

I want to change the attachment file path. So far I've managed this:

Public WithEvents aaa As Outlook.AppointmentItem

Private Sub aaa_BeforeAttachmentAdd(ByVal Attachment As Attachment, Cancel As Boolean)

  Dim newPath
  'path where copy will be saved
  newPath = "D:\Test\" & Attachment.fileName
  
  Dim oFSO As Object
 
  Set oFSO = CreateObject("Scripting.FileSystemObject")
   
  Call oFSO.CopyFile(Attachment.PathName, newPath, True)
  
  Dim att As Attachment
  'add the copy to AppointmentItem
  Set att = aaa.Attachments.Add(newPath, 7)
  'remove original attachment
  Attachment.Delete
  
End Sub

Private Sub Application_ItemLoad(ByVal Item As Object)
    If (TypeOf Item Is AppointmentItem) Then
        Set aaa = Item
    End If
End Sub

Now, this doesn't change the path of the file. I have also noticed OlAttachmentType enumeration and by inspecting the original Attachment I see that it has value 7, but how so when I don't have that value in OlAttachmentType enumeration?

Here is the image, the first one is the original attachment, second is after CopyFile of the original.

enter image description here

I can remove the original, that is easy, but how to force outlook to reference a new one by its path and not to copy it to C:\Users\sogrb\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook\5UUI14T4...

It is interesting that when you drag a file to the Outlook calendar Outlook references it like an external file no matter where that file resides. But when you try to add a file from VBA it can't be referenced outside Outlook cache (that folder above I've noted).

Upvotes: 0

Views: 245

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

In the code I see the following file location:

newPath = "D:\Test\" & Attachment.fileName

And then you are trying to attach this file specifying it as a web attachment:

Set att = aaa.Attachments.Add(newPath, 7)

You can't add local files as web attachments. Instead, you must upload the file to OneDrive and then attach a reference to it. Otherwise, your magical 7 value passed as an attachment type parameter will not work.

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

OlAttachmentType == 7 is the new web attachment type (OneDrive).

If you want to replace the attachment, cancel the operation by setting the Cancel parameter to true and add the new attachment.

Upvotes: 1

Related Questions