1
votes

I have a VBA script in Excel 2016 that exports a worksheet to PDF, and then creates an email in Outlook 2016:

Tabelle8.ExportAsFixedFormat Type:=xlTypePDF, Filename:= 
  ThisWorkbook.Path & "\" & ExportFilename, Quality:=xlQualityStandard 
  , IncludeDocProperties:=False, IgnorePrintAreas:=False, OpenAfterPublish _
  :=False
strPDF = ThisWorkbook.Path & "\" & ExportFilename & ".pdf"
Set OutlookApp = CreateObject("Outlook.Application")
Set strEmail = OutlookApp.CreateItem(0)


With strEmail
  .To = recipient
  .CC = ""
  .Subject = subject
  .HTMLBody = text
  .Attachments.Add strPDF
  .Display
EndWith

This works correctly as long as the workbook is on the local drive, but fails once it is in OneDrive. On OneDrive,

MsgBox strPDF

returns

and somehow, this causes the error "Download failed":

Why is this and how to fix?

1
What happens if you paste the [complete] URL https://d.docs.live.net.... into your browser's address bar? If it works fine from there, then I bet it's something to do with Excel not having permission on your OneDrive. If so, you could remove permissions from the OneDrive folder...?ashleedawg
@ashleedawg Yes, that works. What permissions would I have to remove? Excel is running as the local user, as is the Windows Explorer, and I have no problem accessing the resulting PDF through Windows Explorer. By the way, I tested on a different computer with Office 2013 (both Windows 10); the MsgBox there says "C:\Users\alexander\OneDrive\..." and it works like a charm.Alexander
Well I'm not entirely clear on what your "end goal" is, but you said this works correctly as long as the workbook is on the local drive, but fails once it is in OneDrive. I'm not an expert in network permissions, but that sounded like it must be permissions-related. So, in both cases you're running the same [locally installed] copy of Excel, and the only difference is where the file is located that you're opening, is that correct (Not sure if it's related but want to confirm that you're not running a a cloud-based install of Office 365, right?)ashleedawg
...also, if it works fine locally, and is being run locally, then what's your reasoning behind moving the Excel file to the OneDrive cloud?ashleedawg
There's no reason "it must be permissions-related" just because it works locally. There are lots of potential points of failure here. I can reproduce this with SharePoint Online, and can rule out permissions by having Excel add the file as an OLE object on a worksheet using OLEObjects.Add Filename:="myURL".AjimOthy

1 Answers

3
votes

Using Temp folder or Application path to export the .PDF:

If the goal just to attach the .PDF file to the Outlook mail item, then instead of exporting the .PDF to:

ThisWorkbook.Path

...(which returns the path where the current workbook is saved), you could instead export it to:

Application.Path

...which returns the path to the Excel installation; in my case it's:

C:\Program Files (x86)\Microsoft Office\root\Office16

so you'd change this line as follows:

strPDF = Application.Path & "\" & ExportFilename & ".`.PDF`"

...or, export it to the Windows temp folder:

 strPDF = Environ("temp") & "\" & ExportFilename & ".pdf"

especially if the .PDF's only purpose is to attach to the email. In my case, the Windows Temp folder is:

C:\Users\[WindowsLoginName]\AppData\Local\Temp

Either way, you still have (at least temporary) access to the file in whichever destination you choose.


Keep a Copy:

If you also need to keep a copy of the file on OneDrive, then you have a few options.

If the .PDF was previously saving properly to OneDrive, but Excel couldn't attach it to the Outlook mail item, then you could export the file, with something like:

'export PDF to workbook path
strPDF_save = ThisWorkbook.Path & "\" & ExportFilename & ".pdf"
Tabelle8.ExportAsFixedFormat xlTypePDF, strPDF

'export PDF to temp folder
strPDF_temp = Environ("temp") & "\" & ExportFilename & ".pdf"
Tabelle8.ExportAsFixedFormat xlTypePDF, strPDF

'create Outlook object and send email as attachment
Set OutlookApp = CreateObject("Outlook.Application")
With OutlookApp.CreateItem(0)
  .To = recipient
  .Subject = Subject
  .HTMLBody = Text
  .Attachments.Add strPDF
  .Display 'display the email before sending
End With

(I also removed some extraneous code leftover from recording the macro.)


Map a Drive Letter to OneDrive:

If you're going to be using OneDrive to save/retrieve files on a regular basis, I'd suggest mapping a drive letter to the OneDrive folder.

  1. Go to https://onedrive.live.com.  

  2. Write down or Copy the CID number in the address bar:

map1

  1. Hit the Windows Key Windows Key, then Right-click Computer, and click "Map Network Drive".

map2

  4. In the Map Network Drive dialog, choose a drive letter that you will use to refer to OneDrive (perhaps O:). In the Folder textbox, enter:

https://d.docs.live.net/ Your CID Number

Click Reconnect at Logon, and then click Finish.

map3

  1. You will be prompted to enter your Microsoft Account User ID & Password.

Map4

The drive will be mapped! At this point you can create a shortcut to the drive letter on your desktop, etc, and you can use the drive letter like a local drive for Saving/Opening files, etc.


Links, Not Attachments!

Final Thought:

I have to point out that you're kinda defeating the purpose of Cloud Storage by saving the file on OneDrive and then emailing it as an attachment.

Best practice now tells us to save the file in a shared or sharable location, then email a link to it instead – all of which can be done with the power of Office 365.

Doing so reduces the risk of:

Raising spam flags

Some email clients will mark emails containing large files as spam and drop the incoming message into a junk mail folder.

Delivery failure

Even in the cloud age, some email clients have strict file size limits. Sending a link instead of a bulky file ensures a smooth delivery to the intended recipient.

Consuming space

Managing your organization’s data and storage keeps your IT staff hopping. Sending and receiving large files – especially when there’s a lighter alternative – makes their lives more difficult. (Source)


More Information: