(Solved) – Exporting a range of cells in Excel to Word and saving the Word document as both a Word-file AND a PDF-file


What I am trying to do:
Export a range of cells from an Excel worksheet as an image in an existing Word document, and then saving this Word document as both a Word document and a PDF file. The name both the Word file and the PDF file should get, is in a cell in the Excel worksheet.

The problem:
Almost everything works, except for the .pdf-file. It is generated, but when trying to open it I get an error, saying the file is unreadable.

Can someone help with this? The code I use is below – I assembled it from different examples on this and other forums (I really am a VBA beginner)…

Thank you so much!

The Code:

Sub SaveAsWord()

Dim WordApp As Object
Set WordApp = CreateObject("Word.Application")

Dim WordDoc As Object
Set WordDoc = WordApp.Documents.Open("C:UsersJurgenDocumentsremake.docx")

Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
Set objSelection = WordApp.Selection

Dim myfilename As String
myfilename = Sheets("Blad1").Range("G15")
WordApp.ActiveDocument.SaveAs2 Filename:="C:UsersJurgenDocuments" & myfilename & ".pdf", _
WordApp.ActiveDocument.SaveAs2 Filename:="C:UsersJurgenDocuments" & myfilename & ".docx", _

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *