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

(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")

Range("C4:E19").Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
WordApp.Visible = True
WordApp.ActiveDocument.Bookmarks("here").Select
Set objSelection = WordApp.Selection
objSelection.Paste

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

End Sub

Leave a Reply

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