Hi There,
I have some VBA code which takes a range within an Excel spreadsheet and adds the range into a Word document and then prompts the user for a name and destination to save to and that works well. At the same time I save the now newly created Word document I also save it as a pdf document which again prompts the user for a name and destination to save to. I can see both files created in the destination I selected, however when I open the pdf file I get the following message:
"Adobe Acrobat could not open 'Test1.PDF' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
To create an Adobe PDF document, go to the source application. Then print the document to Adobe PDF."
Is there some VBA code to use to circumvent me reopening the word document and resaving as a PDF?
Can someone assist please?
Here is my code:
Sub ExcelWordToPDF()
Dim objWord As Object
Dim objDoc As Object
Dim mytable As Object
Dim filename As String
Dim fname As String, fname2 As String, fPath As String, fPath2 As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
ThisWorkbook.Worksheets("Test").Range("A1:D150").Copy
Set objDoc = objWord.documents.Add
With objDoc.Range.Characters.Last
.PasteExcelTable False, False, False
With .tables(1)
.Range.ParagraphFormat.SpaceBefore = 0
.Range.ParagraphFormat.SpaceAfter = 0
End With
.InsertAfter Chr(12) 'vbCr
End With
fname = InputBox("Enter the file name to use, including file extension.")
fPath = Application.GetSaveAsFilename(fname, "Word Files (*.docx), *.docx")
fPath2 = Application.GetSaveAsFilename(fname2, "PDF Files (*.pdf), *.pdf")
objWord.activedocument.SaveAs fPath
objWord.activedocument.SaveAs2 fPath2
objWord.activedocument.Close
End Sub
I have some VBA code which takes a range within an Excel spreadsheet and adds the range into a Word document and then prompts the user for a name and destination to save to and that works well. At the same time I save the now newly created Word document I also save it as a pdf document which again prompts the user for a name and destination to save to. I can see both files created in the destination I selected, however when I open the pdf file I get the following message:
"Adobe Acrobat could not open 'Test1.PDF' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
To create an Adobe PDF document, go to the source application. Then print the document to Adobe PDF."
Is there some VBA code to use to circumvent me reopening the word document and resaving as a PDF?
Can someone assist please?
Here is my code:
Sub ExcelWordToPDF()
Dim objWord As Object
Dim objDoc As Object
Dim mytable As Object
Dim filename As String
Dim fname As String, fname2 As String, fPath As String, fPath2 As String
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
ThisWorkbook.Worksheets("Test").Range("A1:D150").Copy
Set objDoc = objWord.documents.Add
With objDoc.Range.Characters.Last
.PasteExcelTable False, False, False
With .tables(1)
.Range.ParagraphFormat.SpaceBefore = 0
.Range.ParagraphFormat.SpaceAfter = 0
End With
.InsertAfter Chr(12) 'vbCr
End With
fname = InputBox("Enter the file name to use, including file extension.")
fPath = Application.GetSaveAsFilename(fname, "Word Files (*.docx), *.docx")
fPath2 = Application.GetSaveAsFilename(fname2, "PDF Files (*.pdf), *.pdf")
objWord.activedocument.SaveAs fPath
objWord.activedocument.SaveAs2 fPath2
objWord.activedocument.Close
End Sub