1
votes

I have been trying to modify this script to take the pdf file and insert it as an image into the active worksheet. but it keeps creating a new workbook and inserting into the new book.

Can someone help me modify this script so it inserts the pdf as an image to the worksheet called "report".

Sub insert_pdf_to_report()

Dim Xl Dim Wb Dim Ws Dim Ol

Sheets("Report").Activate

Set Xl = CreateObject("Excel.Application")
Set Wb = Xl.Workbooks.Add
Set Ws = Wb.Worksheets.Add
Set Ol = Ws.OLEObjects.Add(, "C:\QGC_HSSE\template.pdf", True, False)

    With Ol
        .Left = Ws.Range("A1").Left
        .Height = Ws.Range("A1").Height
        .Width = Ws.Range("A1").Width
        .Top = Ws.Range("A1").Top
    End With

    Sheets("Report").Activate
Xl.Visible = True

End Sub

1

1 Answers

1
votes

Can you try this?

Sheets("Report").Activate

Set Ws = ActiveWorkbook.Worksheets("Report")
Set Ol = Ws.OLEObjects.Add(, "/Users/tcan/Farewell-dinner.jpg", True, False)

...

The problem with your code is that these statements:

Set Xl = CreateObject("Excel.Application")
Set Wb = Xl.Workbooks.Add
Set Ws = Wb.Worksheets.Add

create a new workbook and a worksheet inside the new WB.

I do not have excel to test this but I think my code may work as your expectation by removing the commands that create new workbook.

Cheers.