r/vba 6d ago

Solved VBA Embed PDFs in Excel causing corruption

Im trying to build a tool that lets you select a folder of PDFs and embed each one in a separate sheet in an Excel.

It works amazingly well except when you go to save the file Excel says it's corrupt and can't be saved/error saving. I've tried tweaking it so many times but nothing works. Even just 1 pdf embedded causes the corruption.

When I manually embed the PDF there is no issue.

Does anyone know a fix? Or is programmatically embedding PDFs just not possible?

2 Upvotes

41 comments sorted by

5

u/zlmxtd 6d ago

Dont do that. But if you're going to anyway, try saving the workbook before adding the first pdf, and save again after each

1

u/AlpsInternal 6d ago

I was thinking the save process could be failing, I am not a VBA expert but us it a lot. Embedded PDFs do seem a bit wonky, so maybe saving the documents as word, then having a button to print them as PDFs could be a work around.

1

u/audit157 6d ago

Good idea but unfortunately the second I add 1 embedded document the entire workbook is corrupted

2

u/carnasaur 3 6d ago

Have you tested the code in a fresh wb with nothing else in it while configuring it to save the wb to a folder on your local drive so you can rule out UNC, sharepoint and conflicting module errors?

2

u/diesSaturni 41 6d ago

Why would you even do that. I always dislike when people send me excel with embedded stuff , even images

1

u/audit157 6d ago

It's required. You can only upload 1 Excel file but you need to have all of the PDFs too. Solution is to have all PDFs in the Excel file

1

u/jshine13371 6d ago

You can only upload 1 Excel file but you need to have all of the PDFs too.

Upload to where...?

1

u/audit157 6d ago

Accounting software

2

u/jshine13371 6d ago

So presumably the correct thing to do to solve the problem is ask the makers of the software how to upload these required documents if the tool only allows one upload, instead of hacking together your own solution.

0

u/audit157 6d ago

That's not possible. The real issue is why VBA embedding even 1 document fails when manually doing it is fine

2

u/jshine13371 6d ago

That's not possible.

It's not possible to ask a question?

1

u/audit157 6d ago

Already have. This is the only solution. It's just whether or not is manual or with VBA. After expentsive research it appears it's not possible with VBA as it automatically corrupts all workbooks after 1 off embedding

1

u/jshine13371 6d ago

This is the only solution.

Negative, there's always more than 1 solution.

For example, a better possible solution would be to just zip up the PDFs and Excel file into a single zipped archive file.

1

u/audit157 6d ago

I already tried that. Each PDF needs its own sheet and even if I do a zipped folder for each PDF for each sheet it would require a click to enter the folder, clicking yes on a pop up and then clicking the file which is too much

→ More replies (0)

1

u/diesSaturni 41 6d ago

I'd then export excel to PDF, then merge all PDF's to a single one.

1

u/chiibosoil 1 5d ago

It is common in network DDP documentation.

BOM, Rack diagram etc are embedded and sent to contractors to execute as single package.

1

u/daishiknyte 7 6d ago

How large are the PDFs? Might be better to JPG them then add the images to the page. 

1

u/audit157 6d ago

The users need access to the original PDFs. They are probably 50 pages each or 1MB

1

u/daishiknyte 7 6d ago

Would a .zip of the PDFs work?

1

u/Sammo223 6d ago

The users don't have access to SharePoint or a shared information management system? What kind of info is contain in the pdfs and can you convert them into a better format for excel first?

Is embedding the pdf just inserting a picture?

1

u/audit157 6d ago

Tons of contracts and no it has to be done this way. If VBA can't embed PDFs people will just need to continue manually embedding

1

u/HFTBProgrammer 204 5d ago

The following code creates a perfectly acceptable Excel workbook when I start with an empty workbook:

ActiveSheet.OLEObjects.Add(Filename:="A:\Binder1.pdf", Link:=False, DisplayAsIcon:=False).Select
Sheets.Add
ActiveSheet.OLEObjects.Add(Filename:="A:\Binder2.pdf", Link:=False, DisplayAsIcon:=False).Select
Sheets.Add
ActiveSheet.OLEObjects.Add(Filename:="A:\Binder3.pdf", Link:=False, DisplayAsIcon:=False).Select

So I wonder what you're doing differently.

2

u/audit157 5d ago

That works, thank you! I was using Shapes.AddOLEObject.Add instead of just oleobjects.add

Solution Verified!

1

u/reputatorbot 5d ago

You have awarded 1 point to HFTBProgrammer.


I am a bot - please contact the mods with any questions

1

u/HFTBProgrammer 204 4d ago

Splendid! Come back any time.

1

u/MaxHubert 1 2d ago

Glad you found a solution, I was about to give it a try myself because from experience i knew there was a solution. If you can do it manually VBA can do it for sure.

1

u/kay-jay-dubya 17 4d ago

Are you not interested as to why b you were getting these errors?